Avoid unneeded memory allocactions in psl_str_to_utf8lower()

Reported-by: https://github.com/daurnimator
This commit is contained in:
Tim Rühsen 2016-09-25 12:49:56 +02:00
parent 069c6ff091
commit 32543dd5a5
1 changed files with 10 additions and 8 deletions

View File

@ -1693,7 +1693,7 @@ out:
if (cd != (iconv_t)-1) { if (cd != (iconv_t)-1) {
char *tmp = (char *)str; /* iconv won't change where str points to, but changes tmp itself */ char *tmp = (char *)str; /* iconv won't change where str points to, but changes tmp itself */
size_t tmp_len = strlen(str); size_t tmp_len = strlen(str) + 1;
size_t dst_len = tmp_len * 6, dst_len_tmp = dst_len; size_t dst_len = tmp_len * 6, dst_len_tmp = dst_len;
char *dst = malloc(dst_len + 1), *dst_tmp = dst; char *dst = malloc(dst_len + 1), *dst_tmp = dst;
@ -1710,10 +1710,11 @@ out:
/* u8_tolower() does not terminate the result string */ /* u8_tolower() does not terminate the result string */
ret = PSL_SUCCESS; ret = PSL_SUCCESS;
if (lower) { if (lower) {
if ((*lower = malloc(len + 1))) { if (tmp != (char *)resbuf) {
/* tmp is not 0 terminated */ *lower = tmp;
tmp = NULL;
} else if ((*lower = malloc(len))) {
memcpy(*lower, tmp, len); memcpy(*lower, tmp, len);
(*lower)[len] = 0;
} else } else
ret = PSL_ERR_NO_MEM; ret = PSL_ERR_NO_MEM;
} }
@ -1742,13 +1743,14 @@ out:
ret = PSL_SUCCESS; ret = PSL_SUCCESS;
/* we need a conversion to lowercase */ /* we need a conversion to lowercase */
if ((tmp = u8_tolower((uint8_t *)str, u8_strlen((uint8_t *)str), 0, UNINORM_NFKC, resbuf, &len))) { if ((tmp = u8_tolower((uint8_t *)str, u8_strlen((uint8_t *)str) + 1, 0, UNINORM_NFKC, resbuf, &len))) {
/* u8_tolower() does not terminate the result string */ /* u8_tolower() does not terminate the result string */
if (lower) { if (lower) {
if ((*lower = malloc(len + 1))) { if (tmp != resbuf) {
/* tmp is not 0 terminated */ *lower = tmp;
tmp = NULL;
} else if ((*lower = malloc(len))) {
memcpy(*lower, tmp, len); memcpy(*lower, tmp, len);
(*lower)[len] = 0;
} else } else
ret = PSL_ERR_NO_MEM; ret = PSL_ERR_NO_MEM;
} }