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;
if (lower)
*lower = NULL;
if (!str)
return PSL_ERR_INVALID_ARG;
/* shortcut to avoid costly conversion */
if (_str_is_ascii(str)) {
if (lower) {
char *p;
char *p, *tmp;
if (!(*lower = strdup(str)))
if (!(tmp = strdup(str)))
return PSL_ERR_NO_MEM;
*lower = tmp;
/* convert ASCII string to lowercase */
for (p = *lower; *p; 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;
if (lower) {
if (str_length < 256) {
if (!(*lower = strdup(utf8_lower)))
char *tmp = strdup(utf8_lower);
if (tmp)
*lower = tmp;
else
ret = PSL_ERR_NO_MEM;
} else {
*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)
{
const char *result;
char *lower;
char *lower = NULL;
int rc;
/* 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 */
failed++;
printf("psl_str_to_utf8lower(%s)=%d\n", domain ? domain : "NULL", rc);
free(lower);
return;
}
#endif

View File

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