Skip to content

Commit 7a3f25e

Browse files
committed
Fix ref source management during unserialization
Only register the slot for adding ref sources later if we didn't immediately register one. Also avoids leaking a ref source if it is added early and the assignment fails. Fixes oss-fuzz #27628.
1 parent e074e02 commit 7a3f25e

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Failure to assign ref to typed property
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public int $prop;
8+
}
9+
10+
$s = <<<'STR'
11+
O:4:"Test":1:{s:4:"prop";O:8:"stdClass":1:{s:1:"y";R:2;}}
12+
STR;
13+
try {
14+
var_dump(unserialize($s));
15+
} catch (Error $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
19+
?>
20+
--EXPECT--
21+
Cannot assign stdClass to property Test::$prop of type int

ext/standard/var_unserializer.re

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -560,17 +560,6 @@ string_key:
560560
Z_TRY_DELREF_P(old_data);
561561
ZVAL_COPY_VALUE(old_data, &d);
562562
data = old_data;
563-
564-
if (UNEXPECTED(info)) {
565-
/* Remember to which property this slot belongs, so we can add a
566-
* type source if it is turned into a reference lateron. */
567-
if (!(*var_hash)->ref_props) {
568-
(*var_hash)->ref_props = emalloc(sizeof(HashTable));
569-
zend_hash_init((*var_hash)->ref_props, 8, NULL, NULL, 0);
570-
}
571-
zend_hash_index_update_ptr(
572-
(*var_hash)->ref_props, (zend_uintptr_t) data, info);
573-
}
574563
} else {
575564
var_push_dtor(var_hash, old_data);
576565
data = zend_hash_update_ind(ht, Z_STR(key), &d);
@@ -600,8 +589,18 @@ string_key:
600589
zval_ptr_dtor_nogc(&key);
601590
goto failure;
602591
}
592+
603593
if (Z_ISREF_P(data)) {
604594
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), info);
595+
} else {
596+
/* Remember to which property this slot belongs, so we can add a
597+
* type source if it is turned into a reference lateron. */
598+
if (!(*var_hash)->ref_props) {
599+
(*var_hash)->ref_props = emalloc(sizeof(HashTable));
600+
zend_hash_init((*var_hash)->ref_props, 8, NULL, NULL, 0);
601+
}
602+
zend_hash_index_update_ptr(
603+
(*var_hash)->ref_props, (zend_uintptr_t) data, info);
605604
}
606605
}
607606

0 commit comments

Comments
 (0)