Skip to content

Commit

Permalink
fix printf negative infinity
Browse files Browse the repository at this point in the history
  • Loading branch information
divinity76 committed Sep 20, 2024
1 parent 7e6e712 commit 092b17b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 10 additions & 2 deletions ext/standard/formatted_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/strings/002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));


Expand Down Expand Up @@ -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

0 comments on commit 092b17b

Please sign in to comment.