Skip to content

Commit 0b9207a

Browse files
author
Mark Baker
authored
Merge pull request #2686 from PHPOffice/Additional-Validations-for-Lookup-Arrays
Validate that lookup arrays are actually arrays
2 parents 4c1d953 + 4881e2a commit 0b9207a

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

src/PhpSpreadsheet/Calculation/LookupRef/HLookup.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ public static function lookup($lookupValue, $lookupArray, $indexNumber, $notExac
3232
}
3333

3434
$notExactMatch = (bool) ($notExactMatch ?? true);
35-
$lookupArray = self::convertLiteralArray($lookupArray);
3635

3736
try {
37+
self::validateLookupArray($lookupArray);
38+
$lookupArray = self::convertLiteralArray($lookupArray);
3839
$indexNumber = self::validateIndexLookup($lookupArray, $indexNumber);
3940
} catch (Exception $e) {
4041
return $e->getMessage();

src/PhpSpreadsheet/Calculation/LookupRef/LookupBase.php

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
abstract class LookupBase
99
{
10+
/**
11+
* @param mixed $lookup_array
12+
*/
13+
protected static function validateLookupArray($lookup_array): void
14+
{
15+
if (!is_array($lookup_array)) {
16+
throw new Exception(ExcelError::REF());
17+
}
18+
}
19+
1020
protected static function validateIndexLookup(array $lookup_array, $index_number): int
1121
{
1222
// index_number must be a number greater than or equal to 1

src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static function lookup($lookupValue, $lookupArray, $indexNumber, $notExac
3333
$notExactMatch = (bool) ($notExactMatch ?? true);
3434

3535
try {
36+
self::validateLookupArray($lookupArray);
3637
$indexNumber = self::validateIndexLookup($lookupArray, $indexNumber);
3738
} catch (Exception $e) {
3839
return $e->getMessage();

tests/data/Calculation/LookupRef/VLOOKUP.php

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ function densityGrid(): array
2424
2,
2525
false,
2626
],
27+
[
28+
'#REF!',
29+
1,
30+
'HELLO WORLD',
31+
2,
32+
false,
33+
],
2734
[
2835
100,
2936
1,

0 commit comments

Comments
 (0)