From 92d3e78babd33711afde447733bee20bdd802eb7 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Thu, 1 May 2025 21:42:24 +0900 Subject: [PATCH 1/2] On a 64-bit system, memory allocation is done in a single operation --- ext/intl/grapheme/grapheme_string.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ext/intl/grapheme/grapheme_string.c b/ext/intl/grapheme/grapheme_string.c index 9bc7796cc35f6..fa9d4c5dadaf8 100644 --- a/ext/intl/grapheme/grapheme_string.c +++ b/ext/intl/grapheme/grapheme_string.c @@ -1049,8 +1049,15 @@ PHP_FUNCTION(grapheme_levenshtein) } zend_long *p1, *p2, *tmp; +#if SIZEOF_SIZE_T == 8 + /* strlen_2 is an int32_t, so it will not overflow. */ + zend_long *pbuf = safe_emalloc(((size_t) strlen_2 + 1) * 2, sizeof(zend_long), 0); + p1 = pbuf; + p2 = p1 + (strlen_2 + 1); +#else p1 = safe_emalloc(strlen_2 + 1, sizeof(zend_long), 0); p2 = safe_emalloc(strlen_2 + 1, sizeof(zend_long), 0); +#endif for (i2 = 0; i2 <= strlen_2; i2++) { p1[i2] = i2 * cost_ins; @@ -1098,8 +1105,12 @@ PHP_FUNCTION(grapheme_levenshtein) retval = p1[strlen_2]; RETVAL_LONG(retval); +#if SIZEOF_SIZE_T == 8 + efree(pbuf); +#else efree(p2); efree(p1); +#endif out_collator: ucol_close(collator); From fe84ba660e59116a75071aeefb58c10816f3d6aa Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Thu, 1 May 2025 22:25:55 +0900 Subject: [PATCH 2/2] Address a comment --- ext/intl/grapheme/grapheme_string.c | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/ext/intl/grapheme/grapheme_string.c b/ext/intl/grapheme/grapheme_string.c index fa9d4c5dadaf8..7db90b45c51f4 100644 --- a/ext/intl/grapheme/grapheme_string.c +++ b/ext/intl/grapheme/grapheme_string.c @@ -1048,16 +1048,10 @@ PHP_FUNCTION(grapheme_levenshtein) goto out_collator; } - zend_long *p1, *p2, *tmp; -#if SIZEOF_SIZE_T == 8 - /* strlen_2 is an int32_t, so it will not overflow. */ - zend_long *pbuf = safe_emalloc(((size_t) strlen_2 + 1) * 2, sizeof(zend_long), 0); + zend_long *pbuf, *p1, *p2, *tmp; + pbuf = safe_emalloc(strlen_2 + 1, sizeof(zend_long) * 2, 0); p1 = pbuf; p2 = p1 + (strlen_2 + 1); -#else - p1 = safe_emalloc(strlen_2 + 1, sizeof(zend_long), 0); - p2 = safe_emalloc(strlen_2 + 1, sizeof(zend_long), 0); -#endif for (i2 = 0; i2 <= strlen_2; i2++) { p1[i2] = i2 * cost_ins; @@ -1105,12 +1099,7 @@ PHP_FUNCTION(grapheme_levenshtein) retval = p1[strlen_2]; RETVAL_LONG(retval); -#if SIZEOF_SIZE_T == 8 efree(pbuf); -#else - efree(p2); - efree(p1); -#endif out_collator: ucol_close(collator);