Skip to content

Commit 96173f9

Browse files
committed
Convert some SPL Exceptions to normal Errors in spl_array.c
1 parent a0f2110 commit 96173f9

9 files changed

+49
-59
lines changed

ext/spl/spl_array.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
11021102
/* {{{ spl_array_set_array */
11031103
static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, int just_array) {
11041104
if (Z_TYPE_P(array) != IS_OBJECT && Z_TYPE_P(array) != IS_ARRAY) {
1105-
zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object", 0);
1105+
zend_type_error("Passed variable is not an array or object");
11061106
return;
11071107
}
11081108

@@ -1131,8 +1131,7 @@ static void spl_array_set_array(zval *object, spl_array_object *intern, zval *ar
11311131
} else {
11321132
zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
11331133
if (handler != zend_std_get_properties) {
1134-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
1135-
"Overloaded object of type %s is not compatible with %s",
1134+
zend_type_error("Overloaded object of type %s is not compatible with %s",
11361135
ZSTR_VAL(Z_OBJCE_P(array)->name), ZSTR_VAL(intern->std.ce->name));
11371136
return;
11381137
}
@@ -1377,7 +1376,7 @@ SPL_METHOD(Array, seek)
13771376
return; /* ok */
13781377
}
13791378
}
1380-
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", opos);
1379+
zend_value_error("Seek position " ZEND_LONG_FMT " is out of range", opos);
13811380
} /* }}} */
13821381

13831382
static zend_long spl_array_object_count_elements_helper(spl_array_object *intern) /* {{{ */
@@ -1452,7 +1451,7 @@ static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fnam
14521451
intern->nApplyCount--;
14531452
} else if (use_arg == SPL_ARRAY_METHOD_MAY_USER_ARG) {
14541453
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
1455-
zend_throw_exception(spl_ce_BadMethodCallException, "Function expects one argument at most", 0);
1454+
zend_argument_count_error("Function expects one argument at most");
14561455
goto exit;
14571456
}
14581457
if (arg) {
@@ -1463,7 +1462,7 @@ static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fnam
14631462
intern->nApplyCount--;
14641463
} else {
14651464
if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
1466-
zend_throw_exception(spl_ce_BadMethodCallException, "Function expects exactly one argument", 0);
1465+
zend_argument_count_error("Function expects exactly one argument");
14671466
goto exit;
14681467
}
14691468
ZVAL_COPY_VALUE(&params[1], arg);

ext/spl/tests/ArrayObject_overloaded_object_incompatible.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Objects with overloaded get_properties are incompatible with ArrayObject
66
$ao = new ArrayObject([1, 2, 3]);
77
try {
88
$ao->exchangeArray(new SplFixedArray);
9-
} catch (Exception $e) {
9+
} catch (\TypeError $e) {
1010
echo $e->getMessage(), "\n";
1111
}
1212
var_dump($ao);

ext/spl/tests/arrayObject_exchangeArray_basic3.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ unset($original, $ao, $swapIn, $copy);
4040
$original = new C;
4141
$ao = new ArrayObject($original);
4242
try {
43-
$copy = $ao->exchangeArray(null);
44-
$copy['addedToCopy'] = 'added To Copy';
45-
} catch (Exception $e) {
46-
echo "Exception:" . $e->getMessage() . "\n";
43+
$copy = $ao->exchangeArray(null);
44+
$copy['addedToCopy'] = 'added To Copy';
45+
} catch (\TypeError $e) {
46+
echo $e->getMessage() . \PHP_EOL;
4747
}
4848
$original->addedToOriginal = 'added To Original';
4949
var_dump($ao, $original, $copy);
@@ -103,7 +103,7 @@ NULL
103103

104104

105105
--> exchangeArray() with bad arg type:
106-
Exception:Passed variable is not an array or object
106+
Passed variable is not an array or object
107107

108108
Warning: Undefined variable: copy in %s on line %d
109109
object(ArrayObject)#3 (1) {

ext/spl/tests/arrayObject_uasort_error1.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ Test ArrayObject::uasort() function : wrong arg count
1212
$ao = new ArrayObject();
1313

1414
try {
15-
$ao->uasort();
16-
} catch (BadMethodCallException $e) {
17-
echo $e->getMessage() . "\n";
15+
$ao->uasort();
16+
} catch (\ArgumentCountError $e) {
17+
echo $e->getMessage() . "\n";
1818
}
1919

2020
try {
21-
$ao->uasort(1,2);
22-
} catch (BadMethodCallException $e) {
23-
echo $e->getMessage() . "\n";
21+
$ao->uasort(1,2);
22+
} catch (\ArgumentCountError $e) {
23+
echo $e->getMessage() . "\n";
2424
}
2525
?>
2626
--EXPECT--

ext/spl/tests/arrayObject_uksort_error1.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ Test ArrayObject::uksort() function : wrong arg count
1212
$ao = new ArrayObject();
1313

1414
try {
15-
$ao->uksort();
16-
} catch (BadMethodCallException $e) {
17-
echo $e->getMessage() . "\n";
15+
$ao->uksort();
16+
} catch (\ArgumentCountError $e) {
17+
echo $e->getMessage() . "\n";
1818
}
1919

2020
try {
21-
$ao->uksort(1,2);
22-
} catch (BadMethodCallException $e) {
23-
echo $e->getMessage() . "\n";
21+
$ao->uksort(1,2);
22+
} catch (\ArgumentCountError $e) {
23+
echo $e->getMessage() . "\n";
2424
}
2525
?>
2626
--EXPECT--

ext/spl/tests/array_014.phpt

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,20 @@ $it->seek(5);
99
var_dump($it->current());
1010
$it->seek(4);
1111
var_dump($it->current());
12-
try
13-
{
14-
$it->seek(-1);
15-
var_dump($it->current());
16-
}
17-
catch(Exception $e)
18-
{
19-
echo $e->getMessage() . "\n";
20-
}
2112

22-
try
23-
{
24-
$it->seek(12);
25-
var_dump($it->current());
13+
try {
14+
$it->seek(-1);
15+
var_dump($it->current());
16+
} catch (\ValueError $e) {
17+
echo $e->getMessage() . \PHP_EOL;
2618
}
27-
catch(Exception $e)
28-
{
29-
echo $e->getMessage() . "\n";
19+
20+
21+
try {
22+
$it->seek(12);
23+
var_dump($it->current());
24+
} catch (\ValueError $e) {
25+
echo $e->getMessage() . \PHP_EOL;
3026
}
3127

3228
$pos = 0;

ext/spl/tests/bug70155.phpt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ SPL: Bug #70155 Use After Free Vulnerability in unserialize() with SPLArrayObjec
44
<?php
55
$inner = 'x:i:0;O:12:"DateInterval":1:{s:1:"y";i:3;};m:a:1:{i:0;R:2;}';
66
$exploit = 'C:11:"ArrayObject":'.strlen($inner).':{'.$inner.'}';
7-
$data = unserialize($exploit);
87

9-
var_dump($data);
8+
try {
9+
$data = unserialize($exploit);
10+
} catch (\TypeError $e) {
11+
echo $e->getMessage() . \PHP_EOL;
12+
}
13+
1014
?>
11-
--EXPECTF--
12-
Fatal error: Uncaught InvalidArgumentException: Overloaded object of type DateInterval is not compatible with ArrayObject in %s
13-
Stack trace:
14-
%s
15-
%s
16-
%s
17-
%s
15+
--EXPECT--
16+
Overloaded object of type DateInterval is not compatible with ArrayObject

ext/spl/tests/spl_iterator_getcallchildren.phpt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ var_dump($test->current());
1414
$test->next();
1515
var_dump($test->current());
1616
try {
17-
$output = $test->callGetChildren();
18-
} catch (InvalidArgumentException $ilae){
19-
$output = null;
20-
print "invalid argument exception\n";
17+
var_dump($test->callGetChildren());
18+
} catch (\TypeError $e) {
19+
echo $e->getMessage() . \PHP_EOL;
2120
}
22-
var_dump($output);
23-
2421

2522
?>
2623
--EXPECT--
@@ -33,5 +30,4 @@ var_dump($output);
3330
int(9)
3431
}
3532
int(7)
36-
invalid argument exception
37-
NULL
33+
Passed variable is not an array or object

ext/spl/tests/unserialize_errors.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ try {
2525

2626
try {
2727
unserialize('O:11:"ArrayObject":3:{i:0;i:0;i:1;i:0;i:2;a:0:{}}');
28-
} catch (Exception $e) {
28+
} catch (\TypeError $e) {
2929
echo $e->getMessage(), "\n";
3030
}
3131

@@ -51,7 +51,7 @@ try {
5151

5252
try {
5353
unserialize('O:13:"ArrayIterator":3:{i:0;i:0;i:1;i:0;i:2;a:0:{}}');
54-
} catch (Exception $e) {
54+
} catch (\TypeError $e) {
5555
echo $e->getMessage(), "\n";
5656
}
5757

0 commit comments

Comments
 (0)