Skip to content

Commit 2422972

Browse files
committed
ArrayList: throws exception when indexes are not integers
1 parent d151b6e commit 2422972

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Utils/ArrayList.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function offsetSet($index, $value)
5151
if ($index === null) {
5252
$this->list[] = $value;
5353

54-
} elseif ($index < 0 || $index >= count($this->list)) {
54+
} elseif (!is_int($index) || $index < 0 || $index >= count($this->list)) {
5555
throw new Nette\OutOfRangeException('Offset invalid or out of range');
5656

5757
} else {
@@ -68,7 +68,7 @@ public function offsetSet($index, $value)
6868
*/
6969
public function offsetGet($index)
7070
{
71-
if ($index < 0 || $index >= count($this->list)) {
71+
if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
7272
throw new Nette\OutOfRangeException('Offset invalid or out of range');
7373
}
7474
return $this->list[(int) $index];
@@ -81,7 +81,7 @@ public function offsetGet($index)
8181
*/
8282
public function offsetExists($index): bool
8383
{
84-
return $index >= 0 && $index < count($this->list);
84+
return is_int($index) && $index >= 0 && $index < count($this->list);
8585
}
8686

8787

@@ -93,7 +93,7 @@ public function offsetExists($index): bool
9393
*/
9494
public function offsetUnset($index)
9595
{
96-
if ($index < 0 || $index >= count($this->list)) {
96+
if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
9797
throw new Nette\OutOfRangeException('Offset invalid or out of range');
9898
}
9999
array_splice($this->list, (int) $index, 1);

tests/Utils/ArrayList.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ test(function () {
8383
Assert::exception(function () use ($list) {
8484
$list[2] = true;
8585
}, OutOfRangeException::class, 'Offset invalid or out of range');
86+
87+
Assert::exception(function () use ($list) {
88+
$list['key'] = true;
89+
}, OutOfRangeException::class, 'Offset invalid or out of range');
8690
});
8791

8892

@@ -98,6 +102,10 @@ test(function () {
98102
Assert::exception(function () use ($list) {
99103
$tmp = $list[2];
100104
}, OutOfRangeException::class, 'Offset invalid or out of range');
105+
106+
Assert::exception(function () use ($list) {
107+
$tmp = $list['key'];
108+
}, OutOfRangeException::class, 'Offset invalid or out of range');
101109
});
102110

103111

@@ -113,4 +121,8 @@ test(function () {
113121
Assert::exception(function () use ($list) {
114122
unset($list[2]);
115123
}, OutOfRangeException::class, 'Offset invalid or out of range');
124+
125+
Assert::exception(function () use ($list) {
126+
unset($list['key']);
127+
}, OutOfRangeException::class, 'Offset invalid or out of range');
116128
});

0 commit comments

Comments
 (0)