Skip to content

Commit 08841bf

Browse files
committed
Fix GH-15552: Signed integer overflow in ext/standard/scanf.c
We ensure that the argnum `value` is in the allowed range, *before* mapping it to the `objIndex`, not *afterwards*. Closes GH-15581.
1 parent 93021c6 commit 08841bf

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ PHP NEWS
2525
. Fixed bug GH-15432 (Heap corruption when querying a vector). (cmb,
2626
Kamil Tekiela)
2727

28+
- Standard:
29+
. Fixed bug GH-15552 (Signed integer overflow in ext/standard/scanf.c). (cmb)
30+
2831
- Streams:
2932
. Fixed bug GH-15628 (php_stream_memory_get_buffer() not zero-terminated).
3033
(cmb)

ext/standard/scanf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,7 @@ PHPAPI int ValidateFormat(char *format, int numVars, int *totalSubs)
361361
if (gotSequential) {
362362
goto mixedXPG;
363363
}
364-
objIndex = value - 1;
365-
if ((objIndex < 0) || (numVars && (objIndex >= numVars))) {
364+
if ((value < 1) || (numVars && (value > numVars))) {
366365
goto badIndex;
367366
} else if (numVars == 0) {
368367
/*
@@ -382,6 +381,7 @@ PHPAPI int ValidateFormat(char *format, int numVars, int *totalSubs)
382381

383382
xpgSize = (xpgSize > value) ? xpgSize : value;
384383
}
384+
objIndex = value - 1;
385385
goto xpgCheckDone;
386386
}
387387

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Bug GH-15552 (Signed integer overflow in ext/standard/scanf.c)
3+
--FILE--
4+
<?php
5+
var_dump(sscanf('hello','%2147483648$s'));
6+
?>
7+
--EXPECTF--
8+
Fatal error: Uncaught ValueError: "%n$" argument index out of range in %s:%d
9+
Stack trace:%A

0 commit comments

Comments
 (0)