-
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 9 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 |
---|---|---|
|
@@ -70,6 +70,7 @@ static PHP_GINIT_FUNCTION(json) | |
static PHP_RINIT_FUNCTION(json) | ||
{ | ||
JSON_G(error_code) = 0; | ||
JSON_G(error_pos) = 0; | ||
return SUCCESS; | ||
} | ||
|
||
|
@@ -133,6 +134,7 @@ PHP_JSON_API zend_result php_json_encode_ex(smart_str *buf, zval *val, int optio | |
|
||
return_code = php_json_encode_zval(buf, val, options, &encoder); | ||
JSON_G(error_code) = encoder.error_code; | ||
JSON_G(error_pos) = encoder.error_pos; | ||
|
||
return return_code; | ||
} | ||
|
@@ -177,6 +179,29 @@ static const char *php_json_get_error_msg(php_json_error_code error_code) /* {{{ | |
} | ||
/* }}} */ | ||
|
||
|
||
char *php_json_get_error_msg_wrapper(php_json_error_code error_code) /* {{{ */ | ||
{ | ||
char *final_message; | ||
const char *error_message = php_json_get_error_msg(error_code); | ||
|
||
switch(error_code) { | ||
case PHP_JSON_ERROR_SYNTAX: | ||
case PHP_JSON_ERROR_UTF8: | ||
case PHP_JSON_ERROR_CTRL_CHAR: | ||
case PHP_JSON_ERROR_UNSUPPORTED_TYPE: | ||
case PHP_JSON_ERROR_INVALID_PROPERTY_NAME: | ||
case PHP_JSON_ERROR_UTF16: | ||
spprintf(&final_message, 0, "%s near character %zu",error_message, JSON_G(error_pos)); | ||
break; | ||
default: | ||
spprintf(&final_message, 0, "%s", error_message); | ||
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. Printing to a formatting string like this seems a bit odd - you're just duplicating the message. Can you do a variable assignment instead? |
||
} | ||
|
||
return final_message; | ||
} | ||
/* }}} */ | ||
|
||
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) /* {{{ */ | ||
{ | ||
php_json_parser parser; | ||
|
@@ -185,11 +210,15 @@ PHP_JSON_API zend_result php_json_decode_ex(zval *return_value, const char *str, | |
|
||
if (php_json_yyparse(&parser)) { | ||
php_json_error_code error_code = php_json_parser_error_code(&parser); | ||
size_t error_pos = php_json_parser_error_pos(&parser); | ||
|
||
if (!(options & PHP_JSON_THROW_ON_ERROR)) { | ||
JSON_G(error_code) = error_code; | ||
JSON_G(error_pos) = error_pos; | ||
} else { | ||
zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(error_code), error_code); | ||
} | ||
|
||
RETVAL_NULL(); | ||
return FAILURE; | ||
} | ||
|
@@ -208,7 +237,9 @@ PHP_JSON_API bool php_json_validate_ex(const char *str, size_t str_len, zend_lon | |
|
||
if (php_json_yyparse(&parser)) { | ||
php_json_error_code error_code = php_json_parser_error_code(&parser); | ||
size_t error_pos = php_json_parser_error_pos(&parser); | ||
JSON_G(error_code) = error_code; | ||
JSON_G(error_pos) = error_pos; | ||
return false; | ||
} | ||
|
||
|
@@ -238,6 +269,7 @@ PHP_FUNCTION(json_encode) | |
|
||
if (!(options & PHP_JSON_THROW_ON_ERROR) || (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { | ||
JSON_G(error_code) = encoder.error_code; | ||
JSON_G(error_pos) = encoder.error_pos; | ||
if (encoder.error_code != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { | ||
smart_str_free(&buf); | ||
RETURN_FALSE; | ||
|
@@ -364,6 +396,8 @@ PHP_FUNCTION(json_last_error_msg) | |
{ | ||
ZEND_PARSE_PARAMETERS_NONE(); | ||
|
||
RETURN_STRING(php_json_get_error_msg(JSON_G(error_code))); | ||
char *msg = php_json_get_error_msg_wrapper(JSON_G(error_code)); | ||
RETVAL_STRING(msg); | ||
efree(msg); | ||
} | ||
/* }}} */ |
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; | ||
} | ||
*/ | ||
|
Uh oh!
There was an error while loading. Please reload this page.