Skip to content

Commit 34439b8

Browse files
committed
poc tests passed for json ext
1 parent 73a671e commit 34439b8

File tree

12 files changed

+73
-82
lines changed

12 files changed

+73
-82
lines changed

ext/json/json.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ static const char *php_json_get_error_msg(php_json_error_code error_code) /* {{{
179179
}
180180
/* }}} */
181181

182+
183+
char *php_json_get_error_msg_wrapper(php_json_error_code error_code) /* {{{ */
184+
{
185+
char *final_message;
186+
const char *error_message = php_json_get_error_msg(error_code);
187+
188+
switch(error_code) {
189+
case PHP_JSON_ERROR_SYNTAX:
190+
case PHP_JSON_ERROR_UTF8:
191+
case PHP_JSON_ERROR_CTRL_CHAR:
192+
case PHP_JSON_ERROR_UNSUPPORTED_TYPE:
193+
case PHP_JSON_ERROR_INVALID_PROPERTY_NAME:
194+
case PHP_JSON_ERROR_UTF16:
195+
spprintf(&final_message, 0, "%s near character %zu",error_message, JSON_G(error_pos));
196+
break;
197+
default:
198+
spprintf(&final_message, 0, "%s", error_message);
199+
}
200+
201+
return final_message;
202+
}
203+
/* }}} */
204+
182205
PHP_JSON_API zend_result php_json_decode_ex(zval *return_value, const char *str, size_t str_len, zend_long options, zend_long depth) /* {{{ */
183206
{
184207
php_json_parser parser;
@@ -373,13 +396,8 @@ PHP_FUNCTION(json_last_error_msg)
373396
{
374397
ZEND_PARSE_PARAMETERS_NONE();
375398

376-
if (JSON_G(error_code) == PHP_JSON_ERROR_SYNTAX) {
377-
char *msg;
378-
spprintf(&msg, 0, "Syntax error near character %zu", JSON_G(error_pos));
379-
RETVAL_STRING(msg);
380-
efree(msg);
381-
} else {
382-
RETURN_STRING(php_json_get_error_msg(JSON_G(error_code)));
383-
}
399+
char *msg = php_json_get_error_msg_wrapper(JSON_G(error_code));
400+
RETVAL_STRING(msg);
401+
efree(msg);
384402
}
385403
/* }}} */

ext/json/json_parser.y

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,6 @@ static void php_json_yyerror(php_json_parser *parser, char const *msg)
305305
if (!parser->scanner.errcode) {
306306
parser->scanner.errcode = PHP_JSON_ERROR_SYNTAX;
307307
parser->scanner.errpos = (size_t)(parser->scanner.str_start - parser->scanner.input_start);
308-
fprintf(stderr, "End of input: errpos=%zu, cursor=%p, str_start=%p\n", parser->scanner.errpos, parser->scanner.cursor, parser->scanner.str_start);
309-
//parser->scanner.errpos = (size_t)(parser->scanner.cursor - parser->scanner.str_start);
310308
}
311309
}
312310

@@ -359,13 +357,6 @@ PHP_JSON_API void php_json_parser_init_ex(php_json_parser *parser,
359357
parser->max_depth = max_depth;
360358
parser->return_value = return_value;
361359
memcpy(&parser->methods, parser_methods, sizeof(php_json_parser_methods));
362-
363-
if (!str || str_len == 0) {
364-
parser->scanner.errcode = PHP_JSON_ERROR_SYNTAX;
365-
parser->scanner.errpos = 0;
366-
}
367-
368-
fprintf(stderr, "Init: str_start=%p, cursor=%p, limit=%p, str_len=%zu\n", parser->scanner.str_start, parser->scanner.cursor, parser->scanner.limit, str_len);
369360
}
370361

371362
PHP_JSON_API void php_json_parser_init(php_json_parser *parser,

ext/json/json_scanner.re

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,11 @@ int php_json_scan(php_json_scanner *s)
121121
std:
122122
s->token = s->cursor;
123123

124-
fprintf(stderr, "Scan: START=%p \n cursor=%p \n token=%p \n str_start=%p \n limit=%p \n value_START='%.*s' \n value_token='%.*s' \n value_cursor='%.*s' \n value_str_start='%.*s' \n",
125-
s->input_start,
126-
s->cursor,
127-
s->token,
128-
s->str_start,
129-
s->limit,
130-
(int)(s->limit - s->input_start),
131-
s->input_start,
132-
(int)(s->limit - s->token),
133-
s->token,
134-
(int)(s->limit - s->cursor),
135-
s->cursor,
136-
(int)(s->limit - s->str_start),
137-
s->str_start
138-
);
139-
140-
if (s->cursor >= s->limit) {
124+
/*if (s->cursor >= s->limit) {
141125
s->errcode = PHP_JSON_ERROR_SYNTAX;
142126
s->errpos = (size_t)(s->str_start - s->input_start);
143-
fprintf(stderr, "End of input: errpos=%zu, cursor=%p, str_start=%p\n", s->errpos, s->cursor, s->str_start);
144127
return PHP_JSON_T_ERROR;
145-
}
128+
}*/
146129

147130
/*!re2c
148131
re2c:indent:top = 1;
@@ -258,8 +241,7 @@ std:
258241
<JS>UTF8 {
259242
s->errcode = PHP_JSON_ERROR_SYNTAX;
260243
s->errpos = (size_t)(s->str_start - s->input_start);
261-
fprintf(stderr, "Syntax error: errpos=%zu, cursor=%p, str_start=%p\n",
262-
s->errpos, s->cursor, s->str_start);
244+
263245
return PHP_JSON_T_ERROR;
264246
}
265247
<JS>ANY {
@@ -301,8 +283,7 @@ std:
301283
<STR_P1>ESCPREF {
302284
s->errcode = PHP_JSON_ERROR_SYNTAX;
303285
s->errpos = (size_t)(s->str_start - s->input_start);
304-
fprintf(stderr, "Syntax error: errpos=%zu, cursor=%p, str_start=%p\n",
305-
s->errpos, s->cursor, s->str_start);
286+
306287
return PHP_JSON_T_ERROR;
307288
}
308289
<STR_P1>["] {
@@ -410,8 +391,7 @@ std:
410391
default:
411392
s->errcode = PHP_JSON_ERROR_SYNTAX;
412393
s->errpos = (size_t)(s->str_start - s->input_start);
413-
fprintf(stderr, "Syntax error: errpos=%zu, cursor=%p, str_start=%p\n",
414-
s->errpos, s->cursor, s->str_start);
394+
415395
return PHP_JSON_T_ERROR;
416396
}
417397
*(s->pstr++) = esc;
@@ -441,8 +421,7 @@ std:
441421
<*>ANY {
442422
s->errcode = PHP_JSON_ERROR_SYNTAX;
443423
s->errpos = (size_t)(s->str_start - s->input_start);
444-
fprintf(stderr, "Syntax error: errpos=%zu, cursor=%p, str_start=%p\n",
445-
s->errpos, s->cursor, s->str_start);
424+
446425
return PHP_JSON_T_ERROR;
447426
}
448427
*/

ext/json/tests/007.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ int(2)
3030
string(42) "State mismatch (invalid or malformed JSON)"
3131
NULL
3232
int(3)
33-
string(53) "Control character error, possibly incorrectly encoded"
33+
string(70) "Control character error, possibly incorrectly encoded near character 2"
3434
NULL
3535
int(4)
36-
string(12) "Syntax error"
36+
string(29) "Syntax error near character 0"
3737
Done

ext/json/tests/bug54058.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ var_dump(json_last_error(), json_last_error_msg());
2929
?>
3030
--EXPECT--
3131
int(5)
32-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
32+
string(73) "Malformed UTF-8 characters, possibly incorrectly encoded near character 0"
3333
int(5)
34-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
34+
string(73) "Malformed UTF-8 characters, possibly incorrectly encoded near character 0"
3535
int(5)
36-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
36+
string(73) "Malformed UTF-8 characters, possibly incorrectly encoded near character 0"
3737
int(5)
38-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
38+
string(73) "Malformed UTF-8 characters, possibly incorrectly encoded near character 0"

ext/json/tests/bug61537.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ var_dump(json_last_error(), json_last_error_msg());
2424
--EXPECT--
2525
bool(false)
2626
int(5)
27-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
27+
string(73) "Malformed UTF-8 characters, possibly incorrectly encoded near character 0"
2828
string(4) "null"
2929
int(5)
30-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
30+
string(73) "Malformed UTF-8 characters, possibly incorrectly encoded near character 0"
3131

3232
bool(false)
3333
int(5)
34-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
34+
string(73) "Malformed UTF-8 characters, possibly incorrectly encoded near character 0"
3535
string(4) "null"
3636
int(5)
37-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
37+
string(73) "Malformed UTF-8 characters, possibly incorrectly encoded near character 0"

ext/json/tests/bug62010.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ var_dump(json_last_error_msg());
1010
--EXPECT--
1111
NULL
1212
bool(true)
13-
string(50) "Single unpaired UTF-16 surrogate in unicode escape"
13+
string(67) "Single unpaired UTF-16 surrogate in unicode escape near character 1"

ext/json/tests/bug68546.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ NULL
1616
bool(true)
1717
NULL
1818
bool(true)
19-
string(36) "The decoded property name is invalid"
19+
string(54) "The decoded property name is invalid near character 20"
2020
Done

ext/json/tests/json_encode_exceptions.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ object(JsonException)#1 (7) {
2626
["code":protected]=>
2727
int(5)
2828
["file":protected]=>
29-
string(%d) "%s"
29+
string(67) "/home/usuario/dev/php-src/ext/json/tests/json_encode_exceptions.php"
3030
["line":protected]=>
31-
int(%d)
31+
int(4)
3232
["trace":"Exception":private]=>
3333
array(1) {
3434
[0]=>
3535
array(4) {
3636
["file"]=>
37-
string(%d) "%s"
37+
string(67) "/home/usuario/dev/php-src/ext/json/tests/json_encode_exceptions.php"
3838
["line"]=>
39-
int(%d)
39+
int(4)
4040
["function"]=>
4141
string(11) "json_encode"
4242
["args"]=>
@@ -53,4 +53,4 @@ object(JsonException)#1 (7) {
5353
}
5454
string(4) "null"
5555
int(5)
56-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
56+
string(73) "Malformed UTF-8 characters, possibly incorrectly encoded near character 0"

ext/json/tests/json_validate_002.phpt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,32 @@ json_validate() - Error handling
55

66
require_once("json_validate_requires.inc");
77

8-
//json_validate_trycatchdump("");
9-
//json_validate_trycatchdump("-");
10-
//json_validate_trycatchdump("", -1);
8+
json_validate_trycatchdump("");
9+
json_validate_trycatchdump("-");
10+
json_validate_trycatchdump("", -1);
1111
json_validate_trycatchdump('{"key1":"value1", "value2"}', 2);
12-
//json_validate_trycatchdump('{"key1":"value1", "key2":"value2"}', 1);
13-
//json_validate_trycatchdump('{"key1":"value1", "key2":"value2"}', 2);
14-
//json_validate_trycatchdump("-", 0);
15-
//json_validate_trycatchdump("-", 512, JSON_BIGINT_AS_STRING);
16-
//json_validate_trycatchdump("-", 512, JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE);
17-
//json_validate_trycatchdump("-", 512, JSON_INVALID_UTF8_IGNORE);
18-
//json_validate_trycatchdump("{}", 512, JSON_INVALID_UTF8_IGNORE);
12+
json_validate_trycatchdump('{"key1":"value1", "key2":"value2"}', 1);
13+
json_validate_trycatchdump('{"key1":"value1", "key2":"value2"}', 2);
14+
json_validate_trycatchdump("-", 0);
15+
json_validate_trycatchdump("-", 512, JSON_BIGINT_AS_STRING);
16+
json_validate_trycatchdump("-", 512, JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE);
17+
json_validate_trycatchdump("-", 512, JSON_INVALID_UTF8_IGNORE);
18+
json_validate_trycatchdump("{}", 512, JSON_INVALID_UTF8_IGNORE);
1919

2020
?>
2121
--EXPECTF--
2222
bool(false)
2323
int(4)
24-
string(12) "Syntax error"
24+
string(29) "Syntax error near character 0"
2525
bool(false)
2626
int(4)
27-
string(12) "Syntax error"
27+
string(29) "Syntax error near character 0"
2828
bool(false)
2929
int(4)
30-
string(12) "Syntax error"
30+
string(29) "Syntax error near character 0"
31+
bool(false)
32+
int(4)
33+
string(30) "Syntax error near character 19"
3134
bool(false)
3235
int(1)
3336
string(28) "Maximum stack depth exceeded"
@@ -45,7 +48,7 @@ int(0)
4548
string(8) "No error"
4649
bool(false)
4750
int(4)
48-
string(12) "Syntax error"
51+
string(29) "Syntax error near character 0"
4952
bool(true)
5053
int(0)
5154
string(8) "No error"

0 commit comments

Comments
 (0)