Skip to content

Commit 865739e

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). close GH-18977
1 parent aee1d7f commit 865739e

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ PHP NEWS
3333
- Standard:
3434
. Fix misleading errors in printf(). (nielsdos)
3535
. Fix RCN violations in array functions. (nielsdos)
36+
. Fixed GH-18976 pack() overflow with h/H format and INT_MAX repeater value.
37+
(David Carlier)
3638

3739
- Streams:
3840
. Fixed GH-13264 (fgets() and stream_get_line() do not return false on filter

ext/standard/pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ PHP_FUNCTION(pack)
388388
switch ((int) code) {
389389
case 'h':
390390
case 'H':
391-
INC_OUTPUTPOS((arg + (arg % 2)) / 2,1) /* 4 bit per arg */
391+
INC_OUTPUTPOS((arg / 2) + (arg % 2),1) /* 4 bit per arg */
392392
break;
393393

394394
case 'a':
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-18976 (pack overflow with h/H format)
3+
--INI--
4+
memory_limit=-1
5+
--FILE--
6+
<?php
7+
pack('h2147483647', 1);
8+
pack('H2147483647', 1);
9+
?>
10+
--EXPECTF--
11+
12+
Warning: pack(): Type h: not enough characters in string in %s on line %d
13+
14+
Warning: pack(): Type H: not enough characters in string in %s on line %d

0 commit comments

Comments
 (0)