From 7f760c34eb6563b98d886fb1f54aa7548c9affea Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Thu, 2 May 2024 21:32:10 +0900 Subject: [PATCH 1/2] Adding UNEXPECTED --- ext/bcmath/libbcmath/src/str2num.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/bcmath/libbcmath/src/str2num.c b/ext/bcmath/libbcmath/src/str2num.c index 79649a45781ac..698e0cc74ccf6 100644 --- a/ext/bcmath/libbcmath/src/str2num.c +++ b/ext/bcmath/libbcmath/src/str2num.c @@ -75,7 +75,7 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale) if (decimal_point) { /* search */ fractional_ptr = fractional_end = decimal_point + 1; - if (*fractional_ptr == '\0') { + if (UNEXPECTED(*fractional_ptr == '\0')) { goto after_fractional; } From c764a6b9603a4d31a3ac78d78cf69a754a4fa847 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Thu, 2 May 2024 21:39:14 +0900 Subject: [PATCH 2/2] memset is now used when there are many 0 pads at the end. --- ext/bcmath/libbcmath/src/num2str.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ext/bcmath/libbcmath/src/num2str.c b/ext/bcmath/libbcmath/src/num2str.c index 07ded20ab27ad..eec755d3cd7ca 100644 --- a/ext/bcmath/libbcmath/src/num2str.c +++ b/ext/bcmath/libbcmath/src/num2str.c @@ -38,7 +38,6 @@ zend_string *bc_num2str_ex(bc_num num, size_t scale) { zend_string *str; char *sptr; - size_t index; bool signch; size_t min_scale = MIN(num->n_scale, scale); @@ -64,8 +63,15 @@ zend_string *bc_num2str_ex(bc_num num, size_t scale) if (scale > 0) { *sptr++ = '.'; sptr = bc_copy_bcd_val(sptr, nptr, nptr + min_scale); - for (index = num->n_scale; index < scale; index++) { - *sptr++ = BCD_CHAR(0); + + size_t scale_diff; + if (scale > num->n_scale && (scale_diff = scale - num->n_scale) > 8) { + memset(sptr, BCD_CHAR(0), scale_diff); + sptr += scale_diff; + } else { + for (size_t index = num->n_scale; index < scale; index++) { + *sptr++ = BCD_CHAR(0); + } } }