Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ext/json/json_scanner.re
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,14 @@ std:
s->errcode = PHP_JSON_ERROR_UTF8;
return PHP_JSON_T_ERROR;
}
<STR_P1>EOI {
if (s->limit < s->cursor) {
s->errcode = PHP_JSON_ERROR_SYNTAX;
} else {
s->errcode = PHP_JSON_ERROR_CTRL_CHAR;
}
return PHP_JSON_T_ERROR;
}
<STR_P1>CTRL {
s->errcode = PHP_JSON_ERROR_CTRL_CHAR;
return PHP_JSON_T_ERROR;
Expand Down
31 changes: 31 additions & 0 deletions ext/json/tests/gh22527.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
GH-22527: Unterminated JSON strings are misleadingly reported as “Control character error”
--FILE--
<?php

var_dump(json_decode('"'));
var_dump(json_last_error(), json_last_error_msg());

var_dump(json_decode('"123'));
var_dump(json_last_error(), json_last_error_msg());

var_dump(json_decode('{"123": "'));
var_dump(json_last_error(), json_last_error_msg());

var_dump(json_decode('"' . chr(0)));
var_dump(json_last_error(), json_last_error_msg());

?>
--EXPECT--
NULL
int(4)
string(30) "Syntax error near location 1:1"
NULL
int(4)
string(30) "Syntax error near location 1:1"
NULL
int(4)
string(30) "Syntax error near location 1:9"
NULL
int(3)
string(71) "Control character error, possibly incorrectly encoded near location 1:1"
5 changes: 2 additions & 3 deletions ext/json/tests/json_last_error_msg_error_location_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ string(30) "Syntax error near location 1:1"

Error at position 1:10:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:10"
int(4)
string(31) "Syntax error near location 1:10"

Error at position 1:9:
bool(false)
Expand Down Expand Up @@ -118,4 +118,3 @@ Error at position 1:10:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:10"

41 changes: 20 additions & 21 deletions ext/json/tests/json_last_error_msg_error_location_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,51 +53,50 @@ Testing error locations with Unicode UTF-8 characters

Error after Japanese characters:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:12"
int(4)
string(31) "Syntax error near location 1:12"

Error after Russian characters:
bool(false)
int(3)
string(71) "Control character error, possibly incorrectly encoded near location 1:9"
int(4)
string(30) "Syntax error near location 1:9"

Error after Chinese characters:
bool(false)
int(3)
string(71) "Control character error, possibly incorrectly encoded near location 1:8"
int(4)
string(30) "Syntax error near location 1:8"

Error after Arabic characters:
bool(false)
int(3)
string(71) "Control character error, possibly incorrectly encoded near location 1:9"
int(4)
string(30) "Syntax error near location 1:9"

Error after Emoji:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:11"
int(4)
string(31) "Syntax error near location 1:11"

Error in mixed ASCII and UTF-8:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:27"
int(4)
string(31) "Syntax error near location 1:27"

Error with UTF-8 escaped sequences:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:10"
int(4)
string(31) "Syntax error near location 1:10"

Error in object with multiple UTF-8 keys:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:22"
int(4)
string(31) "Syntax error near location 1:22"

Error in array with UTF-8 strings:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:18"
int(4)
string(31) "Syntax error near location 1:18"

Error in nested object with UTF-8:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:15"

int(4)
string(31) "Syntax error near location 1:15"
13 changes: 6 additions & 7 deletions ext/json/tests/json_last_error_msg_error_location_004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ Testing error locations in deeply nested structures

Error in deeply nested object:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:31"
int(4)
string(31) "Syntax error near location 1:31"

Error in deeply nested array:
bool(true)
Expand All @@ -78,16 +78,15 @@ string(31) "Syntax error near location 1:21"

Error in complex structure:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:93"
int(4)
string(31) "Syntax error near location 1:93"

Error in array of objects:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:68"
int(4)
string(31) "Syntax error near location 1:68"

Error in object with array values:
bool(false)
int(2)
string(61) "State mismatch (invalid or malformed JSON) near location 1:82"

41 changes: 20 additions & 21 deletions ext/json/tests/json_last_error_msg_error_location_005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,51 +53,50 @@ Testing error locations with UTF-16 surrogate pairs and escape sequences

Error after UTF-16 escaped emoji:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:11"
int(4)
string(31) "Syntax error near location 1:11"

Error after multiple UTF-16 pairs:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:10"
int(4)
string(31) "Syntax error near location 1:10"

Error with mixed UTF-8 and UTF-16:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:11"
int(4)
string(31) "Syntax error near location 1:11"

Error with UTF-16 in key:
bool(false)
int(3)
string(71) "Control character error, possibly incorrectly encoded near location 1:9"
int(4)
string(30) "Syntax error near location 1:9"

Error with multiple UTF-16 keys:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:22"
int(4)
string(31) "Syntax error near location 1:22"

Error with BMP characters:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:10"
int(4)
string(31) "Syntax error near location 1:10"

Error with supplementary plane:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:11"
int(4)
string(31) "Syntax error near location 1:11"

Error in array with UTF-16:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:12"
int(4)
string(31) "Syntax error near location 1:12"

Error in nested structure with UTF-16:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:18"
int(4)
string(31) "Syntax error near location 1:18"

Error with UTF-16 and control chars:
bool(false)
int(3)
string(72) "Control character error, possibly incorrectly encoded near location 1:10"

int(4)
string(31) "Syntax error near location 1:10"
5 changes: 2 additions & 3 deletions ext/json/tests/json_last_error_msg_error_location_006.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ string(33) "Syntax error near location 1:1011"

Error with very long key:
bool(false)
int(3)
string(73) "Control character error, possibly incorrectly encoded near location 1:506"
int(4)
string(32) "Syntax error near location 1:506"

Error after empty object:
bool(false)
Expand Down Expand Up @@ -149,4 +149,3 @@ Error with mixed whitespace:
bool(false)
int(3)
string(71) "Control character error, possibly incorrectly encoded near location 3:2"

5 changes: 2 additions & 3 deletions ext/json/tests/json_last_error_msg_error_location_007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ string(30) "Syntax error near location 1:9"

Unclosed string:
bool(false)
int(3)
string(71) "Control character error, possibly incorrectly encoded near location 1:9"
int(4)
string(30) "Syntax error near location 1:9"

Invalid escape sequence:
bool(false)
Expand Down Expand Up @@ -175,4 +175,3 @@ Missing comma between object properties:
bool(false)
int(4)
string(30) "Syntax error near location 1:9"

Loading
Loading