Skip to content

Commit 478e78a

Browse files
committed
Convert some SPL Exceptions to normal Errors in spl_fixedarray.c
1 parent 94771ed commit 478e78a

10 files changed

+57
-63
lines changed

ext/spl/spl_fixedarray.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ static inline zval *spl_fixedarray_object_read_dimension_helper(spl_fixedarray_o
310310
/* we have to return NULL on error here to avoid memleak because of
311311
* ZE duplicating uninitialized_zval_ptr */
312312
if (!offset) {
313-
zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
314-
return NULL;
313+
zend_value_error("Index invalid or out of range");
314+
RETURN_THROWS();
315315
}
316316

317317
if (Z_TYPE_P(offset) != IS_LONG) {
@@ -321,8 +321,8 @@ static inline zval *spl_fixedarray_object_read_dimension_helper(spl_fixedarray_o
321321
}
322322

323323
if (index < 0 || index >= intern->array.size) {
324-
zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
325-
return NULL;
324+
zend_value_error("Index invalid or out of range");
325+
RETURN_THROWS();
326326
} else if (Z_ISUNDEF(intern->array.elements[index])) {
327327
return NULL;
328328
} else {
@@ -378,8 +378,8 @@ static inline void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_o
378378

379379
if (!offset) {
380380
/* '$array[] = value' syntax is not supported */
381-
zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
382-
return;
381+
zend_value_error("Index invalid or out of range");
382+
RETURN_THROWS();
383383
}
384384

385385
if (Z_TYPE_P(offset) != IS_LONG) {
@@ -389,8 +389,8 @@ static inline void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_o
389389
}
390390

391391
if (index < 0 || index >= intern->array.size) {
392-
zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
393-
return;
392+
zend_value_error("Index invalid or out of range");
393+
RETURN_THROWS();
394394
} else {
395395
if (!Z_ISUNDEF(intern->array.elements[index])) {
396396
zval_ptr_dtor(&(intern->array.elements[index]));
@@ -436,8 +436,8 @@ static inline void spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_o
436436
}
437437

438438
if (index < 0 || index >= intern->array.size) {
439-
zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
440-
return;
439+
zend_value_error("Index invalid or out of range");
440+
RETURN_THROWS();
441441
} else {
442442
zval_ptr_dtor(&(intern->array.elements[index]));
443443
ZVAL_UNDEF(&intern->array.elements[index]);
@@ -550,7 +550,7 @@ SPL_METHOD(SplFixedArray, __construct)
550550
}
551551

552552
if (size < 0) {
553-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array size cannot be less than zero");
553+
zend_value_error("array size cannot be less than zero");
554554
RETURN_THROWS();
555555
}
556556

@@ -665,7 +665,7 @@ SPL_METHOD(SplFixedArray, fromArray)
665665

666666
ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(data), num_index, str_index) {
667667
if (str_index != NULL || (zend_long)num_index < 0) {
668-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array must contain only positive integer keys");
668+
zend_value_error("array must contain only positive integer keys");
669669
RETURN_THROWS();
670670
}
671671

@@ -735,7 +735,7 @@ SPL_METHOD(SplFixedArray, setSize)
735735
}
736736

737737
if (size < 0) {
738-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array size cannot be less than zero");
738+
zend_value_error("array size cannot be less than zero");
739739
RETURN_THROWS();
740740
}
741741

ext/spl/tests/fixedarray_001.phpt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ $a = new SplFixedArray(0);
66
// errors
77
try {
88
$a[0] = "value1";
9-
} catch (RuntimeException $e) {
10-
echo "Exception: ".$e->getMessage()."\n";
9+
} catch (\ValueError $e) {
10+
echo $e->getMessage() . PHP_EOL;
1111
}
1212
try {
1313
var_dump($a["asdf"]);
14-
} catch (RuntimeException $e) {
15-
echo "Exception: ".$e->getMessage()."\n";
14+
} catch (\ValueError $e) {
15+
echo $e->getMessage() . PHP_EOL;
1616
}
1717
try {
1818
unset($a[-1]);
19-
} catch (RuntimeException $e) {
20-
echo "Exception: ".$e->getMessage()."\n";
19+
} catch (\ValueError $e) {
20+
echo $e->getMessage() . PHP_EOL;
2121
}
22+
2223
$a->setSize(10);
2324

2425

@@ -45,9 +46,9 @@ $a[0] = "valueNew";
4546
var_dump($b[0]);
4647
?>
4748
--EXPECT--
48-
Exception: Index invalid or out of range
49-
Exception: Index invalid or out of range
50-
Exception: Index invalid or out of range
49+
Index invalid or out of range
50+
Index invalid or out of range
51+
Index invalid or out of range
5152
string(6) "value0"
5253
string(6) "value2"
5354
string(6) "value3"

ext/spl/tests/fixedarray_002.phpt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,20 @@ $a = new A;
3333
// errors
3434
try {
3535
$a[0] = "value1";
36-
} catch (RuntimeException $e) {
37-
echo "Exception: ".$e->getMessage()."\n";
36+
} catch (\ValueError $e) {
37+
echo $e->getMessage() . PHP_EOL;
3838
}
3939
try {
4040
var_dump($a["asdf"]);
41-
} catch (RuntimeException $e) {
42-
echo "Exception: ".$e->getMessage()."\n";
41+
} catch (\ValueError $e) {
42+
echo $e->getMessage() . PHP_EOL;
4343
}
4444
try {
4545
unset($a[-1]);
46-
} catch (RuntimeException $e) {
47-
echo "Exception: ".$e->getMessage()."\n";
46+
} catch (\ValueError $e) {
47+
echo $e->getMessage() . PHP_EOL;
4848
}
49+
4950
$a->setSize(10);
5051

5152

@@ -69,11 +70,11 @@ var_dump(count($a), $a->getSize(), count($a) == $a->getSize());
6970
?>
7071
--EXPECT--
7172
A::offsetSet
72-
Exception: Index invalid or out of range
73+
Index invalid or out of range
7374
A::offsetGet
74-
Exception: Index invalid or out of range
75+
Index invalid or out of range
7576
A::offsetUnset
76-
Exception: Index invalid or out of range
77+
Index invalid or out of range
7778
A::offsetSet
7879
A::offsetSet
7980
A::offsetSet

ext/spl/tests/fixedarray_004.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ $a = new SplFixedArray(10);
77

88
try {
99
$a[] = 1;
10-
} catch (Exception $e) {
11-
var_dump($e->getMessage());
10+
} catch (\ValueError $e) {
11+
echo $e->getMessage() . PHP_EOL;
1212
}
1313

1414
?>
1515
--EXPECT--
16-
string(29) "Index invalid or out of range"
16+
Index invalid or out of range

ext/spl/tests/fixedarray_006.phpt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ try {
1010
for ($i = 0; $i < 100; $i++) {
1111
$a[] = new stdClass;
1212
}
13-
} catch (Exception $e) {
13+
} catch (\ValueError $e) {
1414
echo $e->getMessage(), "\n";
1515
}
1616

17-
print "ok\n";
18-
1917
?>
2018
--EXPECT--
2119
Index invalid or out of range
22-
ok

ext/spl/tests/fixedarray_012.phpt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ $a = new SplFixedArray(100);
77

88
try {
99
$b = &$a[];
10-
} catch (Exception $e) {
10+
} catch (\ValueError $e) {
1111
echo $e->getMessage(), "\n";
1212
}
1313

14-
print "ok\n";
15-
1614
?>
1715
--EXPECT--
1816
Index invalid or out of range
19-
ok

ext/spl/tests/fixedarray_013.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function test(SplFixedArray &$arr) {
1212

1313
try {
1414
test($a[]);
15-
} catch (Exception $e) {
15+
} catch (\ValueError $e) {
1616
echo $e->getMessage(), "\n";
1717
}
1818

ext/spl/tests/fixedarray_014.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
--TEST--
2-
SPL: FixedArray: Trying to access inexistent item
2+
SPL: FixedArray: Trying to access nonexistent item
33
--FILE--
44
<?php
55

66
try {
77
$a = new SplFixedArray(NULL);
88
echo $a[0]++;
9-
} catch (Exception $e) {
9+
} catch (\ValueError $e) {
1010
echo $e->getMessage();
1111
}
1212

ext/spl/tests/fixedarray_020.phpt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,18 @@ var_dump(count($fa), $fa->toArray() === array_values($a));
99
$fa = SplFixedArray::fromArray($a, true);
1010
var_dump(count($fa), $fa->toArray() === $a, $fa->toArray() === (array)$fa);
1111

12+
echo "From Array with string keys, no preserve\n";
13+
SplFixedArray::fromArray(array("foo"=>"bar"), false);
14+
echo "No exception\n";
15+
16+
echo "From Array with string keys, preserve\n";
1217
try {
13-
echo "From Array with string keys, no preserve\n";
14-
SplFixedArray::fromArray(array("foo"=>"bar"), false);
15-
echo "No exception\n";
16-
} catch (Exception $e) {
17-
echo "Exception: ".$e->getMessage()."\n";
18-
}
19-
try {
20-
echo "From Array with string keys, preserve\n";
2118
SplFixedArray::fromArray(array("foo"=>"bar"), true);
22-
echo "No exception\n";
23-
} catch (Exception $e) {
24-
echo "Exception: ".$e->getMessage()."\n";
19+
echo "No exception" . PHP_EOL;
20+
} catch (\ValueError $e) {
21+
echo $e->getMessage() . PHP_EOL;
2522
}
23+
2624
?>
2725
--EXPECT--
2826
int(3)
@@ -33,4 +31,4 @@ bool(true)
3331
From Array with string keys, no preserve
3432
No exception
3533
From Array with string keys, preserve
36-
Exception: array must contain only positive integer keys
34+
array must contain only positive integer keys

ext/spl/tests/fixedarray_021.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ var_dump($a->count());
1212
/* negative init value */
1313
try {
1414
$b = new SplFixedArray(-10);
15-
} catch (Exception $e) {
16-
var_dump($e->getMessage());
15+
} catch (\ValueError $e) {
16+
echo $e->getMessage() . PHP_EOL;
1717
}
1818

1919
/* resize and negative value */
2020
$b = new SplFixedArray();
2121
try {
2222
$b->setSize(-5);
23-
} catch (Exception $e) {
24-
var_dump($e->getMessage());
23+
} catch (\ValueError $e) {
24+
echo $e->getMessage() . PHP_EOL;
2525
}
2626

2727
/* calling __construct() twice */
@@ -63,8 +63,8 @@ var_dump(empty($a["3"]));
6363
--EXPECTF--
6464
int(0)
6565
int(0)
66-
string(35) "array size cannot be less than zero"
67-
string(35) "array size cannot be less than zero"
66+
array size cannot be less than zero
67+
array size cannot be less than zero
6868
NULL
6969
int(0)
7070
int(0)

0 commit comments

Comments
 (0)