Skip to content

Commit 7495339

Browse files
committed
Fix GH-18976: pack with h or H format string overflow.
adding with its own remainder, INT_MAX overflows here (negative values are discarded).
1 parent aee1d7f commit 7495339

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

ext/standard/pack.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,12 @@ PHP_FUNCTION(pack)
388388
switch ((int) code) {
389389
case 'h':
390390
case 'H':
391+
if (arg == INT_MAX) {
392+
efree(formatcodes);
393+
efree(formatargs);
394+
zend_value_error("Type %c: integer overflow in format string", code);
395+
RETURN_THROWS();
396+
}
391397
INC_OUTPUTPOS((arg + (arg % 2)) / 2,1) /* 4 bit per arg */
392398
break;
393399

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-18976 pack overflow wit h/H format
3+
--FILE--
4+
<?php
5+
try {
6+
pack('h2147483647');
7+
} catch (ValueError $e) {
8+
echo $e->getMessage(), PHP_EOL;
9+
}
10+
try {
11+
pack('H2147483647');
12+
} catch (ValueError $e) {
13+
echo $e->getMessage(), PHP_EOL;
14+
}
15+
?>
16+
--EXPECT--
17+
Type h: integer overflow in format string
18+
Type H: integer overflow in format string

0 commit comments

Comments
 (0)