-
Notifications
You must be signed in to change notification settings - Fork 7.9k
json_last_error_msg - better message - error position near by #18866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
70024b7
05f2fc5
b434301
73a671e
34439b8
a0764f3
0593c47
f4f5e08
765d574
eb6400c
7255a81
0f3a191
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -35,6 +35,7 @@ int json_yydebug = 1; | |||||
#define PHP_JSON_DEPTH_INC \ | ||||||
if (parser->max_depth && parser->depth >= parser->max_depth) { \ | ||||||
parser->scanner.errcode = PHP_JSON_ERROR_DEPTH; \ | ||||||
parser->scanner.errpos = (size_t)((parser->scanner.str_start - parser->scanner.input_start) - parser->scanner.str_esc - parser->scanner.utf8_invalid_count); \ | ||||||
YYERROR; \ | ||||||
} \ | ||||||
++parser->depth | ||||||
|
@@ -108,6 +109,7 @@ object_end: | |||||
| ']' | ||||||
{ | ||||||
parser->scanner.errcode = PHP_JSON_ERROR_STATE_MISMATCH; | ||||||
parser->scanner.errpos = (size_t)((parser->scanner.str_start - parser->scanner.input_start) - parser->scanner.str_esc - parser->scanner.utf8_invalid_count); \ | ||||||
YYERROR; | ||||||
} | ||||||
; | ||||||
|
@@ -164,6 +166,7 @@ array_end: | |||||
| '}' | ||||||
{ | ||||||
parser->scanner.errcode = PHP_JSON_ERROR_STATE_MISMATCH; | ||||||
parser->scanner.errpos = (size_t)((parser->scanner.str_start - parser->scanner.input_start) - parser->scanner.str_esc - parser->scanner.utf8_invalid_count); \ | ||||||
YYERROR; | ||||||
} | ||||||
; | ||||||
|
@@ -242,6 +245,7 @@ static int php_json_parser_object_update(php_json_parser *parser, zval *object, | |||||
} else { | ||||||
if (ZSTR_LEN(key) > 0 && ZSTR_VAL(key)[0] == '\0') { | ||||||
parser->scanner.errcode = PHP_JSON_ERROR_INVALID_PROPERTY_NAME; | ||||||
parser->scanner.errpos = (size_t)((parser->scanner.str_start - parser->scanner.input_start) - parser->scanner.str_esc - parser->scanner.utf8_invalid_count); \ | ||||||
zend_string_release_ex(key, 0); | ||||||
zval_ptr_dtor_nogc(zvalue); | ||||||
zval_ptr_dtor_nogc(object); | ||||||
|
@@ -300,6 +304,7 @@ static void php_json_yyerror(php_json_parser *parser, char const *msg) | |||||
{ | ||||||
if (!parser->scanner.errcode) { | ||||||
parser->scanner.errcode = PHP_JSON_ERROR_SYNTAX; | ||||||
parser->scanner.errpos = (size_t)((parser->scanner.str_start - parser->scanner.input_start) - parser->scanner.str_esc - parser->scanner.utf8_invalid_count); | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -308,6 +313,11 @@ PHP_JSON_API php_json_error_code php_json_parser_error_code(const php_json_parse | |||||
return parser->scanner.errcode; | ||||||
} | ||||||
|
||||||
PHP_JSON_API size_t php_json_parser_error_pos(const php_json_parser *parser) | ||||||
{ | ||||||
return parser->scanner.errpos; | ||||||
} | ||||||
|
||||||
static const php_json_parser_methods default_parser_methods = | ||||||
{ | ||||||
php_json_parser_array_create, | ||||||
|
@@ -339,7 +349,7 @@ PHP_JSON_API void php_json_parser_init_ex(php_json_parser *parser, | |||||
int options, | ||||||
int max_depth, | ||||||
const php_json_parser_methods *parser_methods) | ||||||
{ | ||||||
{ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
memset(parser, 0, sizeof(php_json_parser)); | ||||||
php_json_scanner_init(&parser->scanner, str, str_len, options); | ||||||
parser->depth = 1; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,9 +93,24 @@ static int php_json_ucs2_to_int(php_json_scanner *s, int size) | |
|
||
void php_json_scanner_init(php_json_scanner *s, const char *str, size_t str_len, int options) | ||
{ | ||
s->token = NULL; | ||
s->marker = NULL; | ||
s->ctxmarker = NULL; | ||
s->pstr = NULL; | ||
s->str_esc = 0; | ||
s->state = 0; | ||
s->errcode = PHP_JSON_ERROR_NONE; | ||
s->errpos = 0; /* Initialize errpos */ | ||
s->utf8_invalid = 0; | ||
s->utf8_invalid_count = 0; | ||
s->str_start = (php_json_ctype *)str; /* Initialize str_start */ | ||
s->input_start = (php_json_ctype *)str; /* Initialize input_start */ | ||
|
||
s->cursor = (php_json_ctype *) str; | ||
s->limit = (php_json_ctype *) str + str_len; | ||
s->options = options; | ||
s->errpos = 0; | ||
s->errcode = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't initialize these values twice - errpos is set to 0 above, and errcode is set to PHP_JSON_ERROR_NONE which is also 0, so these two lines can just be removed |
||
PHP_JSON_CONDITION_SET(JS); | ||
} | ||
|
||
|
@@ -201,6 +216,7 @@ std: | |
return PHP_JSON_T_EOI; | ||
} else { | ||
s->errcode = PHP_JSON_ERROR_CTRL_CHAR; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
return PHP_JSON_T_ERROR; | ||
} | ||
} | ||
|
@@ -213,19 +229,23 @@ std: | |
} | ||
<JS>CTRL { | ||
s->errcode = PHP_JSON_ERROR_CTRL_CHAR; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
return PHP_JSON_T_ERROR; | ||
} | ||
<JS>UTF8 { | ||
s->errcode = PHP_JSON_ERROR_SYNTAX; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
return PHP_JSON_T_ERROR; | ||
} | ||
<JS>ANY { | ||
s->errcode = PHP_JSON_ERROR_UTF8; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
return PHP_JSON_T_ERROR; | ||
} | ||
|
||
<STR_P1>CTRL { | ||
s->errcode = PHP_JSON_ERROR_CTRL_CHAR; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
return PHP_JSON_T_ERROR; | ||
} | ||
<STR_P1>UTF16_1 { | ||
|
@@ -246,6 +266,7 @@ std: | |
} | ||
<STR_P1>UCS2 { | ||
s->errcode = PHP_JSON_ERROR_UTF16; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
return PHP_JSON_T_ERROR; | ||
} | ||
<STR_P1>ESC { | ||
|
@@ -254,6 +275,8 @@ std: | |
} | ||
<STR_P1>ESCPREF { | ||
s->errcode = PHP_JSON_ERROR_SYNTAX; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
|
||
return PHP_JSON_T_ERROR; | ||
} | ||
<STR_P1>["] { | ||
|
@@ -283,6 +306,7 @@ std: | |
if (s->options & PHP_JSON_INVALID_UTF8_SUBSTITUTE) { | ||
if (s->utf8_invalid_count > INT_MAX - 2) { | ||
s->errcode = PHP_JSON_ERROR_UTF8; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
return PHP_JSON_T_ERROR; | ||
} | ||
s->utf8_invalid_count += 2; | ||
|
@@ -293,6 +317,7 @@ std: | |
PHP_JSON_CONDITION_GOTO(STR_P1); | ||
} | ||
s->errcode = PHP_JSON_ERROR_UTF8; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
return PHP_JSON_T_ERROR; | ||
} | ||
|
||
|
@@ -358,6 +383,8 @@ std: | |
break; | ||
default: | ||
s->errcode = PHP_JSON_ERROR_SYNTAX; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
|
||
return PHP_JSON_T_ERROR; | ||
} | ||
*(s->pstr++) = esc; | ||
|
@@ -386,6 +413,8 @@ std: | |
|
||
<*>ANY { | ||
s->errcode = PHP_JSON_ERROR_SYNTAX; | ||
s->errpos = (size_t)(s->str_start - s->input_start - s->str_esc - s->utf8_invalid_count); | ||
|
||
return PHP_JSON_T_ERROR; | ||
} | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Printing to a formatting string like this seems a bit odd - you're just duplicating the message. Can you do a variable assignment instead?