Skip to content

Commit 99c6019

Browse files
committed
Unserialize: Warn if extra data is appended to the serialized string
1 parent 4a8e35c commit 99c6019

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

ext/standard/tests/serialize/typed_property_ref_overwrite.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Test {
77
public ?object $prop;
88
}
99
$s = <<<'STR'
10-
O:4:"Test":2:{s:4:"prop";R:1;s:4:"prop";N;}}
10+
O:4:"Test":2:{s:4:"prop";R:1;s:4:"prop";N;}
1111
STR;
1212
var_dump(unserialize($s));
1313

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Extra data at the end of a valid value
3+
--FILE--
4+
<?php
5+
6+
var_dump(unserialize('i:5;i:6;'));
7+
var_dump(unserialize('N;i:6;'));
8+
var_dump(unserialize('b:1;i:6;'));
9+
var_dump(unserialize('a:1:{s:3:"foo";b:1;}i:6;'));
10+
11+
?>
12+
--EXPECTF--
13+
Warning: unserialize(): Extra data starting at offset 4 of 8 bytes in %s on line %d
14+
int(5)
15+
16+
Warning: unserialize(): Extra data starting at offset 2 of 6 bytes in %s on line %d
17+
NULL
18+
19+
Warning: unserialize(): Extra data starting at offset 4 of 8 bytes in %s on line %d
20+
bool(true)
21+
22+
Warning: unserialize(): Extra data starting at offset 20 of 24 bytes in %s on line %d
23+
array(1) {
24+
["foo"]=>
25+
bool(true)
26+
}

ext/standard/var.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,11 @@ PHPAPI void php_unserialize_with_options(zval *return_value, const char *buf, co
14061406
zval_ptr_dtor(return_value);
14071407
}
14081408
RETVAL_FALSE;
1409+
} else if ((char*)p < buf + buf_len) {
1410+
if (!EG(exception)) {
1411+
php_error_docref(NULL, E_WARNING, "Extra data starting at offset " ZEND_LONG_FMT " of %zd bytes",
1412+
(zend_long)((char*)p - buf), buf_len);
1413+
}
14091414
} else if (BG(unserialize).level > 1) {
14101415
ZVAL_COPY(return_value, retval);
14111416
} else if (Z_REFCOUNTED_P(return_value)) {

0 commit comments

Comments
 (0)