From 092b17b2cd1d924e6a0f9f234d3fbfd5f9310240 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Fri, 20 Sep 2024 15:55:12 +0200 Subject: [PATCH 1/4] fix printf negative infinity --- ext/standard/formatted_print.c | 12 ++++++++++-- tests/strings/002.phpt | 4 ++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c index ba0f73d9a9c22..74c3648d1b553 100644 --- a/ext/standard/formatted_print.c +++ b/ext/standard/formatted_print.c @@ -247,8 +247,16 @@ php_sprintf_appenddouble(zend_string **buffer, size_t *pos, if (zend_isinf(number)) { is_negative = (number<0); - php_sprintf_appendstring(buffer, pos, "INF", 3, 0, padding, - alignment, 3, is_negative, 0, always_sign); + if (is_negative) { + // ideally negative should have been handled inside php_sprintf_appendstring , + // but the code is difficult to debug, and it's easy to break stuff inside there (I tried, I broke stuff), + // handling it here is much easier.. + php_sprintf_appendstring(buffer, pos, "-INF", 4, 0, padding, + alignment, 4, is_negative, 0, always_sign); + } else { + php_sprintf_appendstring(buffer, pos, "INF", 3, 0, padding, + alignment, 3, is_negative, 0, always_sign); + } return; } diff --git a/tests/strings/002.phpt b/tests/strings/002.phpt index 54630836b1632..6284e9bf5d339 100644 --- a/tests/strings/002.phpt +++ b/tests/strings/002.phpt @@ -44,6 +44,8 @@ try { } catch(\ValueError $e) { print('Error found: '.$e->getMessage()."\n"); } +printf("printf test 31:%.17g\n", INF); +printf("printf test 32:%.17g\n", -INF); vprintf("vprintf test 1:%2\$-2d %1\$2d\n", array(1, 2)); @@ -83,4 +85,6 @@ printf test 27:3 1 2 printf test 28:02 1 printf test 29:2 1 printf test 30:Error found: Argument number specifier must be greater than zero and less than 2147483647 +printf test 31:INF +printf test 32:-INF vprintf test 1:2 1 From 13ae1f88b2bb1133fc99132d1e263f6ffde13897 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Fri, 20 Sep 2024 21:54:13 +0200 Subject: [PATCH 2/4] comment cleanup seems the location is correct :) https://github.com/php/php-src/pull/15965/files#r1768840758 --- ext/standard/formatted_print.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c index 74c3648d1b553..0fa79303d2576 100644 --- a/ext/standard/formatted_print.c +++ b/ext/standard/formatted_print.c @@ -248,9 +248,6 @@ php_sprintf_appenddouble(zend_string **buffer, size_t *pos, if (zend_isinf(number)) { is_negative = (number<0); if (is_negative) { - // ideally negative should have been handled inside php_sprintf_appendstring , - // but the code is difficult to debug, and it's easy to break stuff inside there (I tried, I broke stuff), - // handling it here is much easier.. php_sprintf_appendstring(buffer, pos, "-INF", 4, 0, padding, alignment, 4, is_negative, 0, always_sign); } else { From 0d4dc4d7578e7b6fa9cc3ba276c4743c7c77901a Mon Sep 17 00:00:00 2001 From: divinity76 Date: Fri, 20 Sep 2024 21:56:43 +0200 Subject: [PATCH 3/4] Update ext/standard/formatted_print.c Co-authored-by: Christoph M. Becker --- ext/standard/formatted_print.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c index 0fa79303d2576..33e44a46021b2 100644 --- a/ext/standard/formatted_print.c +++ b/ext/standard/formatted_print.c @@ -251,8 +251,9 @@ php_sprintf_appenddouble(zend_string **buffer, size_t *pos, php_sprintf_appendstring(buffer, pos, "-INF", 4, 0, padding, alignment, 4, is_negative, 0, always_sign); } else { - php_sprintf_appendstring(buffer, pos, "INF", 3, 0, padding, - alignment, 3, is_negative, 0, always_sign); + char *str = is_negative ? "-INF" : "INF"; + php_sprintf_appendstring(buffer, pos, str, strlen(str), 0, padding, + alignment, strlen(str), is_negative, 0, always_sign); } return; } From 2fa8151dd3064ec3a4f76540553277852475336c Mon Sep 17 00:00:00 2001 From: divinity76 Date: Sat, 21 Sep 2024 08:44:02 +0200 Subject: [PATCH 4/4] strlen https://github.com/php/php-src/pull/15965/files#r1768850508 --- ext/standard/formatted_print.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c index 33e44a46021b2..75fe77a43d5d1 100644 --- a/ext/standard/formatted_print.c +++ b/ext/standard/formatted_print.c @@ -246,15 +246,10 @@ php_sprintf_appenddouble(zend_string **buffer, size_t *pos, } if (zend_isinf(number)) { - is_negative = (number<0); - if (is_negative) { - php_sprintf_appendstring(buffer, pos, "-INF", 4, 0, padding, - alignment, 4, is_negative, 0, always_sign); - } else { - char *str = is_negative ? "-INF" : "INF"; - php_sprintf_appendstring(buffer, pos, str, strlen(str), 0, padding, - alignment, strlen(str), is_negative, 0, always_sign); - } + is_negative = (number<0); + char *str = is_negative ? "-INF" : "INF"; + php_sprintf_appendstring(buffer, pos, str, strlen(str), 0, padding, + alignment, strlen(str), is_negative, 0, always_sign); return; }