Skip to content

Commit 3b0a190

Browse files
committed
Promote warning to exception in unserialize()
1 parent f0b5d55 commit 3b0a190

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

ext/standard/tests/serialize/max_depth.phpt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@ function create_nested_data($depth, $prefix, $suffix, $inner = 'i:0;') {
88
}
99

1010
echo "Invalid max_depth:\n";
11-
var_dump(unserialize('i:0;', ['max_depth' => 'foo']));
12-
var_dump(unserialize('i:0;', ['max_depth' => -1]));
11+
try {
12+
unserialize('i:0;', ['max_depth' => 'foo']);
13+
} catch (TypeError $exception) {
14+
echo $exception->getMessage() . "\n";
15+
}
16+
17+
try {
18+
unserialize('i:0;', ['max_depth' => -1]);
19+
} catch (ValueError $exception) {
20+
echo $exception->getMessage() . "\n";
21+
}
1322

1423
echo "Array:\n";
1524
var_dump(unserialize(
@@ -95,12 +104,8 @@ var_dump(is_array(unserialize(
95104
?>
96105
--EXPECTF--
97106
Invalid max_depth:
98-
99-
Warning: unserialize(): max_depth should be int in %s on line %d
100-
bool(false)
101-
102-
Warning: unserialize(): max_depth cannot be negative in %s on line %d
103-
bool(false)
107+
max_depth should be int
108+
max_depth cannot be negative
104109
Array:
105110
bool(true)
106111

ext/standard/var.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
10031003

10041004
if (ce != PHP_IC_ENTRY && zend_hash_str_exists(&ce->function_table, "__sleep", sizeof("__sleep")-1)) {
10051005
zval retval, tmp;
1006-
1006+
10071007
Z_ADDREF_P(struc);
10081008
ZVAL_OBJ(&tmp, Z_OBJ_P(struc));
10091009

@@ -1138,7 +1138,7 @@ PHPAPI void php_var_serialize_destroy(php_serialize_data_t d) {
11381138
}
11391139
}
11401140

1141-
/* {{{ proto string serialize(mixed variable)
1141+
/* {{{ proto string|null serialize(mixed variable)
11421142
Returns a string representation of variable (which can later be unserialized) */
11431143
PHP_FUNCTION(serialize)
11441144
{
@@ -1156,7 +1156,7 @@ PHP_FUNCTION(serialize)
11561156

11571157
if (EG(exception)) {
11581158
smart_str_free(&buf);
1159-
RETURN_FALSE;
1159+
return;
11601160
}
11611161

11621162
if (buf.s) {
@@ -1231,13 +1231,11 @@ PHP_FUNCTION(unserialize)
12311231
max_depth = zend_hash_str_find_deref(Z_ARRVAL_P(options), "max_depth", sizeof("max_depth") - 1);
12321232
if (max_depth) {
12331233
if (Z_TYPE_P(max_depth) != IS_LONG) {
1234-
php_error_docref(NULL, E_WARNING, "max_depth should be int");
1235-
RETVAL_FALSE;
1234+
zend_type_error("max_depth should be int");
12361235
goto cleanup;
12371236
}
12381237
if (Z_LVAL_P(max_depth) < 0) {
1239-
php_error_docref(NULL, E_WARNING, "max_depth cannot be negative");
1240-
RETVAL_FALSE;
1238+
zend_value_error("max_depth cannot be negative");
12411239
goto cleanup;
12421240
}
12431241

0 commit comments

Comments
 (0)