Check malloc/realloc results in src/psl.c

Fixes #57
Reported-by: https://github.com/daurnimator
This commit is contained in:
Tim Rühsen 2016-09-21 11:13:02 +02:00
parent 01d3f53321
commit 1ab7be5641
2 changed files with 34 additions and 14 deletions

View File

@ -53,10 +53,11 @@ extern "C" {
* psl_error_t: * psl_error_t:
* @PSL_SUCCESS: Successful return. * @PSL_SUCCESS: Successful return.
* @PSL_ERR_INVALID_ARG: Invalid argument. * @PSL_ERR_INVALID_ARG: Invalid argument.
* @PSL_ERR_CONVERTER: Failed to open libicu utf-16 converter * @PSL_ERR_CONVERTER: Failed to open libicu utf-16 converter.
* @PSL_ERR_TO_UTF16: Failed to convert to utf-16. * @PSL_ERR_TO_UTF16: Failed to convert to utf-16.
* @PSL_ERR_TO_LOWER: Failed to convert utf-16 to lowercase. * @PSL_ERR_TO_LOWER: Failed to convert utf-16 to lowercase.
* @PSL_ERR_TO_UTF8: Failed to convert utf-16 to utf-8. * @PSL_ERR_TO_UTF8: Failed to convert utf-16 to utf-8.
* @PSL_ERR_NO_MEM: Failed to allocate memory.
* *
* Return codes for PSL functions. * Return codes for PSL functions.
* Negative return codes mean failure. * Negative return codes mean failure.
@ -68,7 +69,8 @@ typedef enum {
PSL_ERR_CONVERTER = -2, /* failed to open libicu utf-16 converter */ PSL_ERR_CONVERTER = -2, /* failed to open libicu utf-16 converter */
PSL_ERR_TO_UTF16 = -3, /* failed to convert to utf-16 */ PSL_ERR_TO_UTF16 = -3, /* failed to convert to utf-16 */
PSL_ERR_TO_LOWER = -4, /* failed to convert utf-16 to lowercase */ PSL_ERR_TO_LOWER = -4, /* failed to convert utf-16 to lowercase */
PSL_ERR_TO_UTF8 = -5 /* failed to convert utf-16 to utf-8 */ PSL_ERR_TO_UTF8 = -5, /* failed to convert utf-16 to utf-8 */
PSL_ERR_NO_MEM = -6 /* failed to allocate memory */
} psl_error_t; } psl_error_t;
typedef struct _psl_ctx_st psl_ctx_t; typedef struct _psl_ctx_st psl_ctx_t;

View File

@ -263,11 +263,21 @@ static int _vector_add(_psl_vector_t *v, const _psl_entry_t *elem)
if (v) { if (v) {
void *elemp; void *elemp;
elemp = malloc(sizeof(_psl_entry_t)); if (!(elemp = malloc(sizeof(_psl_entry_t))))
return -1;
memcpy(elemp, elem, sizeof(_psl_entry_t)); memcpy(elemp, elem, sizeof(_psl_entry_t));
if (v->max == v->cur) if (v->max == v->cur) {
v->entry = realloc(v->entry, (v->max *= 2) * sizeof(_psl_entry_t *)); void *m = realloc(v->entry, (v->max *= 2) * sizeof(_psl_entry_t *));
if (m)
v->entry = m;
else {
free(elemp);
return -1;
}
}
v->entry[v->cur++] = elemp; v->entry[v->cur++] = elemp;
return v->cur - 1; return v->cur - 1;
@ -776,7 +786,7 @@ static void _add_punycode_if_needed(_psl_idna_t *idna, _psl_vector_t *v, _psl_en
/* fprintf(stderr, "toASCII '%s' -> '%s'\n", e->label_buf, lookupname); */ /* fprintf(stderr, "toASCII '%s' -> '%s'\n", e->label_buf, lookupname); */
_suffix_init(&suffix, lookupname, strlen(lookupname)); _suffix_init(&suffix, lookupname, strlen(lookupname));
suffix.flags = e->flags; suffix.flags = e->flags;
suffixp = _vector_get(v, _vector_add(v, &suffix)); if ((suffixp = _vector_get(v, _vector_add(v, &suffix))))
suffixp->label = suffixp->label_buf; /* set label to changed address */ suffixp->label = suffixp->label_buf; /* set label to changed address */
} /* else ignore */ } /* else ignore */
@ -1231,10 +1241,11 @@ psl_ctx_t *psl_load_fp(FILE *fp)
suffixp = _vector_get(psl->suffixes, _vector_add(psl->suffixes, &suffix)); suffixp = _vector_get(psl->suffixes, _vector_add(psl->suffixes, &suffix));
} }
if (suffixp) {
suffixp->label = suffixp->label_buf; /* set label to changed address */ suffixp->label = suffixp->label_buf; /* set label to changed address */
_add_punycode_if_needed(idna, psl->suffixes, suffixp); _add_punycode_if_needed(idna, psl->suffixes, suffixp);
} }
}
} while ((linep = fgets(buf, sizeof(buf), fp))); } while ((linep = fgets(buf, sizeof(buf), fp)));
_vector_sort(psl->suffixes); _vector_sort(psl->suffixes);
@ -1567,6 +1578,7 @@ int psl_is_cookie_domain_acceptable(const psl_ctx_t *psl, const char *hostname,
* PSL_ERR_TO_UTF16: Failed to convert @str to unicode * PSL_ERR_TO_UTF16: Failed to convert @str to unicode
* PSL_ERR_TO_LOWER: Failed to convert unicode to lowercase * PSL_ERR_TO_LOWER: Failed to convert unicode to lowercase
* PSL_ERR_TO_UTF8: Failed to convert unicode to UTF-8 * PSL_ERR_TO_UTF8: Failed to convert unicode to UTF-8
* PSL_ERR_NO_MEM: Failed to allocate memory
* *
* Since: 0.4 * Since: 0.4
*/ */
@ -1659,11 +1671,17 @@ psl_error_t psl_str_to_utf8lower(const char *str, const char *encoding _UNUSED,
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;
if (iconv(cd, &tmp, &tmp_len, &dst_tmp, &dst_len_tmp) != (size_t)-1) { if (!dst) {
ret = PSL_ERR_NO_MEM;
}
else if (iconv(cd, &tmp, &tmp_len, &dst_tmp, &dst_len_tmp) != (size_t)-1) {
uint8_t *resbuf = malloc(dst_len * 2 + 1); uint8_t *resbuf = malloc(dst_len * 2 + 1);
size_t len = dst_len * 2; /* leave space for additional \0 byte */ size_t len = dst_len * 2; /* leave space for additional \0 byte */
if ((dst = (char *)u8_tolower((uint8_t *)dst, dst_len - dst_len_tmp, 0, UNINORM_NFKC, resbuf, &len))) { if (!resbuf) {
ret = PSL_ERR_NO_MEM;
}
else if ((dst = (char *)u8_tolower((uint8_t *)dst, dst_len - dst_len_tmp, 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)
*lower = strndup((char *)dst, len); *lower = strndup((char *)dst, len);