diff --git a/src/pcre2_convert.c b/src/pcre2_convert.c index ad7312ab7..8a2b293d5 100644 --- a/src/pcre2_convert.c +++ b/src/pcre2_convert.c @@ -1215,9 +1215,11 @@ for (int i = 0; i < 2; i++) /* Allocate memory for the buffer, with hidden space for an allocator at the start. The next time round the loop runs the conversion for real. */ - allocated = PRIV(memctl_malloc)(sizeof(pcre2_memctl) + - (*bufflenptr + 1)*PCRE2_CODE_UNIT_WIDTH, (pcre2_memctl *)ccontext); - if (allocated == NULL) + if (*bufflenptr > ((PCRE2_SIZE_MAX - sizeof(pcre2_memctl)) / + CU2BYTES(1)) - 1 || + (allocated = PRIV(memctl_malloc)(sizeof(pcre2_memctl) + + CU2BYTES(*bufflenptr + 1), + (pcre2_memctl *)ccontext)) == NULL) { *bufflenptr = 0; /* Error offset */ return PCRE2_ERROR_NOMEMORY; diff --git a/src/pcre2_substring.c b/src/pcre2_substring.c index f68b464e0..a6f5277a3 100644 --- a/src/pcre2_substring.c +++ b/src/pcre2_substring.c @@ -210,9 +210,10 @@ PCRE2_SIZE size; PCRE2_UCHAR *yield; rc = pcre2_substring_length_bynumber(match_data, stringnumber, &size); if (rc < 0) return rc; -yield = PRIV(memctl_malloc)(sizeof(pcre2_memctl) + - (size + 1)*PCRE2_CODE_UNIT_WIDTH, (pcre2_memctl *)match_data); -if (yield == NULL) return PCRE2_ERROR_NOMEMORY; +if (size > ((PCRE2_SIZE_MAX - sizeof(pcre2_memctl)) / CU2BYTES(1)) - 1 || + (yield = PRIV(memctl_malloc)(sizeof(pcre2_memctl) + + CU2BYTES(size + 1), (pcre2_memctl *)match_data)) == NULL) + return PCRE2_ERROR_NOMEMORY; yield = (PCRE2_UCHAR *)(((char *)yield) + sizeof(pcre2_memctl)); if (size != 0) memcpy(yield, match_data->subject + match_data->ovector[stringnumber*2], CU2BYTES(size));