Don't taint out variable on error in psl_str_to_utf8lower()

Fixes #71
This commit is contained in:
Tim Rühsen 2016-12-05 16:28:39 +01:00
parent b9e04d6958
commit 6490b8214b
3 changed files with 16 additions and 15 deletions

View File

@ -1594,20 +1594,19 @@ psl_error_t psl_str_to_utf8lower(const char *str, const char *encoding _UNUSED,
{ {
int ret = PSL_ERR_INVALID_ARG; int ret = PSL_ERR_INVALID_ARG;
if (lower)
*lower = NULL;
if (!str) if (!str)
return PSL_ERR_INVALID_ARG; return PSL_ERR_INVALID_ARG;
/* shortcut to avoid costly conversion */ /* shortcut to avoid costly conversion */
if (_str_is_ascii(str)) { if (_str_is_ascii(str)) {
if (lower) { if (lower) {
char *p; char *p, *tmp;
if (!(*lower = strdup(str))) if (!(tmp = strdup(str)))
return PSL_ERR_NO_MEM; return PSL_ERR_NO_MEM;
*lower = tmp;
/* convert ASCII string to lowercase */ /* convert ASCII string to lowercase */
for (p = *lower; *p; p++) for (p = *lower; *p; p++)
if (isupper(*p)) if (isupper(*p))
@ -1654,7 +1653,11 @@ psl_error_t psl_str_to_utf8lower(const char *str, const char *encoding _UNUSED,
ret = PSL_SUCCESS; ret = PSL_SUCCESS;
if (lower) { if (lower) {
if (str_length < 256) { if (str_length < 256) {
if (!(*lower = strdup(utf8_lower))) char *tmp = strdup(utf8_lower);
if (tmp)
*lower = tmp;
else
ret = PSL_ERR_NO_MEM; ret = PSL_ERR_NO_MEM;
} else { } else {
*lower = utf8_lower; *lower = utf8_lower;

View File

@ -53,7 +53,7 @@ static int
static void testx(const psl_ctx_t *psl, const char *domain, const char *encoding, const char *lang, const char *expected_result) static void testx(const psl_ctx_t *psl, const char *domain, const char *encoding, const char *lang, const char *expected_result)
{ {
const char *result; const char *result;
char *lower; char *lower = NULL;
int rc; int rc;
/* just to cover special code paths for valgrind checking */ /* just to cover special code paths for valgrind checking */
@ -67,8 +67,6 @@ static void testx(const psl_ctx_t *psl, const char *domain, const char *encoding
/* if we do not runtime support, test failure have to be skipped */ /* if we do not runtime support, test failure have to be skipped */
failed++; failed++;
printf("psl_str_to_utf8lower(%s)=%d\n", domain ? domain : "NULL", rc); printf("psl_str_to_utf8lower(%s)=%d\n", domain ? domain : "NULL", rc);
free(lower);
return; return;
} }
#endif #endif

View File

@ -164,15 +164,15 @@ int main(int argc, const char *const *argv)
else if (mode == 4) { else if (mode == 4) {
char *cookie_domain_lower; char *cookie_domain_lower;
if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &cookie_domain_lower)) != PSL_SUCCESS) if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &cookie_domain_lower)) == PSL_SUCCESS) {
fprintf(stderr, "%s: Failed to convert cookie domain '%s' to lowercase UTF-8 (%d)\n", domain, cookie_domain, rc);
else
printf("%s: %d\n", domain, psl_is_cookie_domain_acceptable(psl, lower, cookie_domain)); printf("%s: %d\n", domain, psl_is_cookie_domain_acceptable(psl, lower, cookie_domain));
free(cookie_domain_lower);
free(cookie_domain_lower); } else
fprintf(stderr, "%s: Failed to convert cookie domain '%s' to lowercase UTF-8 (%d)\n", domain, cookie_domain, rc);
} }
free(lower); if (rc == PSL_SUCCESS)
free(lower);
} }
psl_free(psl); psl_free(psl);