Skip to content

Commit 09767d7

Browse files
committed
Convert some SPL Exceptions to normal Errors in spl_fixedarray.c
1 parent 3f438c6 commit 09767d7

10 files changed

+66
-72
lines changed

ext/spl/spl_fixedarray.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ static inline zval *spl_fixedarray_object_read_dimension_helper(spl_fixedarray_o
313313
/* we have to return NULL on error here to avoid memleak because of
314314
* ZE duplicating uninitialized_zval_ptr */
315315
if (!offset) {
316-
zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
316+
zend_value_error("Index invalid or out of range");
317317
return NULL;
318318
}
319319

@@ -324,7 +324,7 @@ static inline zval *spl_fixedarray_object_read_dimension_helper(spl_fixedarray_o
324324
}
325325

326326
if (index < 0 || index >= intern->array.size) {
327-
zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
327+
zend_value_error("Index invalid or out of range");
328328
return NULL;
329329
} else {
330330
return &intern->array.elements[index];
@@ -379,7 +379,7 @@ static inline void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_o
379379

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

@@ -390,7 +390,7 @@ static inline void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_o
390390
}
391391

392392
if (index < 0 || index >= intern->array.size) {
393-
zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
393+
zend_value_error("Index invalid or out of range");
394394
return;
395395
} else {
396396
zval_ptr_dtor(&(intern->array.elements[index]));
@@ -435,7 +435,7 @@ static inline void spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_o
435435
}
436436

437437
if (index < 0 || index >= intern->array.size) {
438-
zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
438+
zend_value_error("Index invalid or out of range");
439439
return;
440440
} else {
441441
zval_ptr_dtor(&(intern->array.elements[index]));
@@ -542,7 +542,7 @@ SPL_METHOD(SplFixedArray, __construct)
542542
}
543543

544544
if (size < 0) {
545-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array size cannot be less than zero");
545+
zend_value_error("array size cannot be less than zero");
546546
RETURN_THROWS();
547547
}
548548

@@ -653,7 +653,7 @@ SPL_METHOD(SplFixedArray, fromArray)
653653

654654
ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(data), num_index, str_index) {
655655
if (str_index != NULL || (zend_long)num_index < 0) {
656-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array must contain only positive integer keys");
656+
zend_value_error("array must contain only positive integer keys");
657657
RETURN_THROWS();
658658
}
659659

@@ -723,7 +723,7 @@ SPL_METHOD(SplFixedArray, setSize)
723723
}
724724

725725
if (size < 0) {
726-
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array size cannot be less than zero");
726+
zend_value_error("array size cannot be less than zero");
727727
RETURN_THROWS();
728728
}
729729

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ SPL: FixedArray: adding new elements
66
$a = new SplFixedArray(10);
77

88
try {
9-
$a[] = 1;
10-
} catch (Exception $e) {
11-
var_dump($e->getMessage());
9+
$a[] = 1;
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: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ $b = 10000;
77
$a = new SplFixedArray($b);
88

99
try {
10-
for ($i = 0; $i < 100; $i++) {
11-
$a[] = new stdClass;
12-
}
13-
} catch (Exception $e) {
14-
echo $e->getMessage(), "\n";
10+
for ($i = 0; $i < 100; $i++) {
11+
$a[] = new stdClass;
12+
}
13+
} catch (\ValueError $e) {
14+
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: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ SPL: FixedArray: Assigning the object to another variable using []
66
$a = new SplFixedArray(100);
77

88
try {
9-
$b = &$a[];
10-
} catch (Exception $e) {
11-
echo $e->getMessage(), "\n";
9+
$b = &$a[];
10+
} catch (\ValueError $e) {
11+
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ function test(SplFixedArray &$arr) {
1111
}
1212

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

1919
?>

ext/spl/tests/fixedarray_014.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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 {
7-
$a = new SplFixedArray(NULL);
8-
echo $a[0]++;
9-
} catch (Exception $e) {
10-
echo $e->getMessage();
7+
$a = new SplFixedArray(NULL);
8+
echo $a[0]++;
9+
} catch (\ValueError $e) {
10+
echo $e->getMessage();
1111
}
1212

1313
?>

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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ var_dump($a->count());
1111

1212
/* negative init value */
1313
try {
14-
$b = new SplFixedArray(-10);
15-
} catch (Exception $e) {
16-
var_dump($e->getMessage());
14+
$b = new SplFixedArray(-10);
15+
} catch (\ValueError $e) {
16+
echo $e->getMessage() . PHP_EOL;
1717
}
1818

1919
/* resize and negative value */
2020
$b = new SplFixedArray();
2121
try {
22-
$b->setSize(-5);
23-
} catch (Exception $e) {
24-
var_dump($e->getMessage());
22+
$b->setSize(-5);
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)