Skip to content

Commit b214c55

Browse files
committed
Fix test failures, remove redundant test
1 parent c34c575 commit b214c55

10 files changed

Lines changed: 51 additions & 49 deletions

package.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,23 @@
2020
-->
2121
<date>2022-10-14</date>
2222
<version>
23-
<release>2.2.0dev</release>
24-
<api>2.2.0dev</api>
23+
<release>3.0.0dev</release>
24+
<api>3.0.0dev</api>
2525
</version>
2626
<stability>
2727
<release>stable</release>
2828
<api>stable</api>
2929
</stability>
3030
<license uri="https://www.apache.org/licenses/LICENSE-2.0.html">Apache 2.0</license>
3131
<notes>
32+
* Add SimdJsonValueError. In php 8.0+, it extends ValueError, and it extends Error in older php versions.
33+
This provides an API similar to the JSON module, which started throwing ValueError for invalid depths in php 8.0.
34+
* Throw SimdJsonValueError instead of emitting notices if $depth is too small or too large in all simdjson PHP functions.
35+
simdjson_is_valid(), simdjson_key_count() and simdjson_key_exists() now have non-null return types.
36+
* Throw a SimdJsonException in simdjson_key_exists on error conditions such as invalid json, to be consistent with other simdjson PHP functions.
37+
* Add an optional boolean `$throw_if_uncountable = false` to simdjson_key_count.
38+
When this is overridden to be true, simdjson_key_count will throw a SimdJsonException if the JSON pointer refers to a value that exists but is neither an array nor an object instead of returning 0.
3239
* Expose simdjson C APIs marked with PHP_SIMDJSON_API in php_simdjson.h to other C projects.
33-
* Ensure that that header file works when required by C projects and that publicly exposed functions are available.
3440
</notes>
3541
<contents>
3642
<dir name="/">
@@ -64,6 +70,7 @@
6470
<file name="decode_strict_types.phpt" role="test"/>
6571
<file name="decode_types.phpt" role="test"/>
6672
<file name="depth.phpt" role="test"/>
73+
<file name="dump.inc" role="test"/>
6774
<file name="is_valid.phpt" role="test"/>
6875
<file name="is_valid_args.phpt" role="test"/>
6976
<file name="key_count.phpt" role="test"/>
@@ -94,7 +101,6 @@
94101
<file name="bug69187.phpt" role="test"/>
95102
<file name="fail001.phpt" role="test"/>
96103
<file name="json_decode_basic.phpt" role="test"/>
97-
<file name="json_decode_error.phpt" role="test"/>
98104
<file name="json_decode_invalid_utf8.phpt" role="test"/>
99105
<file name="pass001.1_64bit.phpt" role="test"/>
100106
<file name="pass001.1.phpt" role="test"/>

php_simdjson.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,17 @@ ZEND_BEGIN_ARG_INFO_EX(simdjson_key_value_arginfo, 0, 0, 2)
7171
ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0)
7272
ZEND_END_ARG_INFO()
7373

74-
SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(simdjson_key_exists_arginfo, 0, 2, _IS_BOOL, 1)
74+
SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(simdjson_key_exists_arginfo, 0, 2, _IS_BOOL, 0)
7575
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)
7676
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
7777
ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0)
7878
ZEND_END_ARG_INFO()
7979

80-
SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(simdjson_key_count_arginfo, 0, 2, IS_LONG, 1)
80+
SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(simdjson_key_count_arginfo, 0, 2, IS_LONG, 0)
8181
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)
8282
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
8383
ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0)
84+
ZEND_ARG_TYPE_INFO(0, throw_if_uncountable, _IS_BOOL, 0)
8485
ZEND_END_ARG_INFO()
8586

8687
#define SIMDJSON_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(simdjson, v)

tests/compat/json_decode_error.phpt

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/decode_max_depth.phpt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@ simdjson_decode reject max depth
55
declare(strict_types=1);
66
ini_set('error_reporting', (string)E_ALL);
77
ini_set('display_errors', 'stderr');
8+
require_once __DIR__ . '/dump.inc';
89

9-
function dump(Closure $c) {
10-
try {
11-
var_dump($c());
12-
} catch (SimdJsonValueError $e) {
13-
printf("Caught %s: %s\n", get_class($e), $e->getMessage());
14-
}
15-
}
16-
17-
foreach ([0, 1024, PHP_INT_MAX >> 1] as $depth) {
10+
foreach ([0, PHP_INT_MIN, 1024, PHP_INT_MAX >> 1, PHP_INT_MAX] as $depth) {
1811
dump(function () use ($depth) { return simdjson_decode('[]', true, $depth); });
1912
dump(function () use ($depth) { return simdjson_key_count('{"a":"b"}', 'a', $depth); });
2013
dump(function () use ($depth) { return simdjson_key_value('{"a":{}}', 'a', true, $depth); });
@@ -28,6 +21,11 @@ Caught SimdJsonValueError: simdjson_key_count(): Argument #4 ($depth) must be gr
2821
Caught SimdJsonValueError: simdjson_key_value(): Argument #4 ($depth) must be greater than zero
2922
Caught SimdJsonValueError: simdjson_key_exists(): Argument #3 ($depth) must be greater than zero
3023
Caught SimdJsonValueError: simdjson_is_valid(): Argument #2 ($depth) must be greater than zero
24+
Caught SimdJsonValueError: simdjson_decode(): Argument #2 ($depth) must be greater than zero
25+
Caught SimdJsonValueError: simdjson_key_count(): Argument #4 ($depth) must be greater than zero
26+
Caught SimdJsonValueError: simdjson_key_value(): Argument #4 ($depth) must be greater than zero
27+
Caught SimdJsonValueError: simdjson_key_exists(): Argument #3 ($depth) must be greater than zero
28+
Caught SimdJsonValueError: simdjson_is_valid(): Argument #2 ($depth) must be greater than zero
3129
array(0) {
3230
}
3331
int(0)
@@ -40,3 +38,8 @@ Caught SimdJsonValueError: simdjson_key_count(): Argument #4 ($depth) exceeds ma
4038
Caught SimdJsonValueError: simdjson_key_value(): Argument #4 ($depth) exceeds maximum allowed value of %d
4139
Caught SimdJsonValueError: simdjson_key_exists(): Argument #3 ($depth) exceeds maximum allowed value of %d
4240
Caught SimdJsonValueError: simdjson_is_valid(): Argument #2 ($depth) exceeds maximum allowed value of %d
41+
Caught SimdJsonValueError: simdjson_decode(): Argument #2 ($depth) exceeds maximum allowed value of %d
42+
Caught SimdJsonValueError: simdjson_key_count(): Argument #4 ($depth) exceeds maximum allowed value of %d
43+
Caught SimdJsonValueError: simdjson_key_value(): Argument #4 ($depth) exceeds maximum allowed value of %d
44+
Caught SimdJsonValueError: simdjson_key_exists(): Argument #3 ($depth) exceeds maximum allowed value of %d
45+
Caught SimdJsonValueError: simdjson_is_valid(): Argument #2 ($depth) exceeds maximum allowed value of %d

tests/depth.phpt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ display_errors=stderr
55
error_reporting=E_ALL
66
--FILE--
77
<?php
8-
var_dump(simdjson_decode('[]', true, 0));
9-
var_dump(simdjson_key_value('[]', '', true, 0));
8+
require_once __DIR__ . '/dump.inc';
9+
10+
// too small/large depths tested in decode_max_depth
1011
var_dump(simdjson_key_count('[]', '', 1));
1112
var_dump(simdjson_decode('[]', true, 1));
1213
echo "Test '[1]'\n";
@@ -29,10 +30,6 @@ var_dump(simdjson_decode('[[]]', true, 2));
2930
var_dump(simdjson_decode('[[1]]', true, 3));
3031
?>
3132
--EXPECTF--
32-
Warning: simdjson_decode(): Depth must be greater than zero in %s on line 2
33-
NULL
34-
Warning: simdjson_key_value(): Depth must be greater than zero in %s on line 3
35-
NULL
3633
int(0)
3734
array(0) {
3835
}

tests/dump.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
function dump(Closure $c) {
3+
try {
4+
var_dump($c());
5+
} catch (Throwable $e) {
6+
printf("Caught %s: %s\n", get_class($e), $e->getMessage());
7+
}
8+
}

tests/is_valid_args.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ Function [ <internal:simdjson> function simdjson_is_valid ] {
1313
Parameter #0 [ <required> string $json ]
1414
Parameter #1 [ <optional> int%S $depth%S ]
1515
}
16-
- Return [ %Sbool%S ]
16+
- Return [ bool%S ]
1717
}

tests/is_valid_edge_cases.phpt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ error_reporting=E_ALL
55
display_errors=stderr
66
--FILE--
77
<?php
8-
var_dump(\simdjson_is_valid('[]', -1));
9-
var_dump(\simdjson_is_valid('[[]]', 2));
10-
var_dump(\simdjson_is_valid('[[]]', 1024));
11-
var_dump(\simdjson_is_valid('[[]]', 1));
12-
var_dump(\simdjson_is_valid('[[]]'));
8+
require_once __DIR__ . '/dump.inc';
9+
dump(function () { return simdjson_is_valid('[]', -1); });
10+
dump(function () { return simdjson_is_valid('[[]]', 2); });
11+
dump(function () { return simdjson_is_valid('[[]]', 1024); });
12+
dump(function () { return simdjson_is_valid('[[]]', 1); });
13+
dump(function () { return simdjson_is_valid('[[]]'); });
1314
echo "test deeply nested\n";
1415
$long = str_repeat('[', 3000) . str_repeat(']', 3000);
15-
var_dump(\simdjson_is_valid($long));
16-
var_dump(\simdjson_is_valid($long, 2999));
17-
var_dump(\simdjson_is_valid($long, 3000));
16+
dump(function () use ($long) { return simdjson_is_valid($long); });
17+
dump(function () use ($long) { return simdjson_is_valid($long, 2999); });
18+
dump(function () use ($long) { return simdjson_is_valid($long, 3000); });
1819
?>
1920
--EXPECTF--
20-
Warning: simdjson_is_valid(): Depth must be greater than zero in %sis_valid_edge_cases.php on line 2
21-
NULL
21+
Caught SimdJsonValueError: simdjson_is_valid(): Argument #2 ($depth) must be greater than zero
2222
bool(true)
2323
bool(true)
2424
bool(false)

tests/key_count_args.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ echo $reflection;
99
--EXPECTF--
1010
Function [ <internal:simdjson> function simdjson_key_count ] {
1111

12-
- Parameters [3] {
12+
- Parameters [4] {
1313
Parameter #0 [ <required> string $json ]
1414
Parameter #1 [ <required> string $key ]
1515
Parameter #2 [ <optional> int%S $depth%S ]
16+
Parameter #3 [ <optional> bool%S $throw_if_uncountable%S ]
1617
}
17-
- Return [ %Sint%S ]
18+
- Return [ int%S ]
1819
}

tests/key_exists_args.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ Function [ <internal:simdjson> function simdjson_key_exists ] {
1414
Parameter #1 [ <required> string $key ]
1515
Parameter #2 [ <optional> int%S $depth%S ]
1616
}
17-
- Return [ %Sbool%S ]
17+
- Return [ bool%S ]
1818
}

0 commit comments

Comments
 (0)