Skip to content

Commit 70024b7

Browse files
committed
first setup
1 parent e3cfa4b commit 70024b7

File tree

6 files changed

+24
-1
lines changed

6 files changed

+24
-1
lines changed

ext/json/json.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static PHP_GINIT_FUNCTION(json)
7070
static PHP_RINIT_FUNCTION(json)
7171
{
7272
JSON_G(error_code) = 0;
73+
JSON_G(error_pos) = 0;
7374
return SUCCESS;
7475
}
7576

@@ -133,6 +134,7 @@ PHP_JSON_API zend_result php_json_encode_ex(smart_str *buf, zval *val, int optio
133134

134135
return_code = php_json_encode_zval(buf, val, options, &encoder);
135136
JSON_G(error_code) = encoder.error_code;
137+
JSON_G(error_pos) = encoder.error_pos;
136138

137139
return return_code;
138140
}
@@ -156,7 +158,11 @@ static const char *php_json_get_error_msg(php_json_error_code error_code) /* {{{
156158
case PHP_JSON_ERROR_CTRL_CHAR:
157159
return "Control character error, possibly incorrectly encoded";
158160
case PHP_JSON_ERROR_SYNTAX:
159-
return "Syntax error";
161+
char *msg;
162+
spprintf(&msg, 0, "Syntax error at character %zu", JSON_G(error_pos));
163+
return msg;
164+
//efree(msg);
165+
//return "Syntax error";
160166
case PHP_JSON_ERROR_UTF8:
161167
return "Malformed UTF-8 characters, possibly incorrectly encoded";
162168
case PHP_JSON_ERROR_RECURSION:
@@ -185,11 +191,15 @@ PHP_JSON_API zend_result php_json_decode_ex(zval *return_value, const char *str,
185191

186192
if (php_json_yyparse(&parser)) {
187193
php_json_error_code error_code = php_json_parser_error_code(&parser);
194+
size_t error_pos = php_json_parser_error_pos(&parser);
195+
188196
if (!(options & PHP_JSON_THROW_ON_ERROR)) {
189197
JSON_G(error_code) = error_code;
198+
JSON_G(error_pos) = error_pos;
190199
} else {
191200
zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(error_code), error_code);
192201
}
202+
193203
RETVAL_NULL();
194204
return FAILURE;
195205
}
@@ -208,7 +218,9 @@ PHP_JSON_API bool php_json_validate_ex(const char *str, size_t str_len, zend_lon
208218

209219
if (php_json_yyparse(&parser)) {
210220
php_json_error_code error_code = php_json_parser_error_code(&parser);
221+
size_t error_pos = php_json_parser_error_pos(&parser);
211222
JSON_G(error_code) = error_code;
223+
JSON_G(error_pos) = error_pos;
212224
return false;
213225
}
214226

@@ -238,6 +250,7 @@ PHP_FUNCTION(json_encode)
238250

239251
if (!(options & PHP_JSON_THROW_ON_ERROR) || (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
240252
JSON_G(error_code) = encoder.error_code;
253+
JSON_G(error_pos) = encoder.error_pos;
241254
if (encoder.error_code != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
242255
smart_str_free(&buf);
243256
RETURN_FALSE;

ext/json/json_parser.y

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ static void php_json_yyerror(php_json_parser *parser, char const *msg)
300300
{
301301
if (!parser->scanner.errcode) {
302302
parser->scanner.errcode = PHP_JSON_ERROR_SYNTAX;
303+
parser->scanner.errpos = (size_t)(parser->scanner.cursor - parser->scanner.str_start);
303304
}
304305
}
305306

@@ -308,6 +309,11 @@ PHP_JSON_API php_json_error_code php_json_parser_error_code(const php_json_parse
308309
return parser->scanner.errcode;
309310
}
310311

312+
PHP_JSON_API size_t php_json_parser_error_pos(const php_json_parser *parser)
313+
{
314+
return parser->scanner.errpos;
315+
}
316+
311317
static const php_json_parser_methods default_parser_methods =
312318
{
313319
php_json_parser_array_create,

ext/json/php_json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ ZEND_BEGIN_MODULE_GLOBALS(json)
9090
int encoder_depth;
9191
int encode_max_depth;
9292
php_json_error_code error_code;
93+
size_t error_pos;
9394
ZEND_END_MODULE_GLOBALS(json)
9495

9596
PHP_JSON_API ZEND_EXTERN_MODULE_GLOBALS(json)

ext/json/php_json_encoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct _php_json_encoder {
2626
int depth;
2727
int max_depth;
2828
php_json_error_code error_code;
29+
size_t error_pos;
2930
};
3031

3132
static inline void php_json_encode_init(php_json_encoder *encoder)

ext/json/php_json_scanner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef struct _php_json_scanner {
3535
int state; /* condition state */
3636
int options; /* options */
3737
php_json_error_code errcode; /* error type if there is an error */
38+
size_t errpos;
3839
int utf8_invalid; /* whether utf8 is invalid */
3940
int utf8_invalid_count; /* number of extra character for invalid utf8 */
4041
} php_json_scanner;

ext/json/tests/json_validate_002.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require_once("json_validate_requires.inc");
88
json_validate_trycatchdump("");
99
json_validate_trycatchdump("-");
1010
json_validate_trycatchdump("", -1);
11+
json_validate_trycatchdump('{"key1":"value1", "value2"}', 2);
1112
json_validate_trycatchdump('{"key1":"value1", "key2":"value2"}', 1);
1213
json_validate_trycatchdump('{"key1":"value1", "key2":"value2"}', 2);
1314
json_validate_trycatchdump("-", 0);

0 commit comments

Comments
 (0)