8000 json_last_error_msg - better message - error position near by by juan-morales · Pull Request #18866 · php/php-src · GitHub
[go: up one dir, main page]

Skip to content

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

juan-morales
Copy link
Contributor
@juan-morales juan-morales commented Jun 16, 2025 8000

Working POC giving a better error message when the json_last_error_msg function is called (only for this function).

Adding near character %d to a json related error message.

Putting aside my bad C coding skill, I would like to know your opinions on this PR, and kindly ask for reviews in order to improve it.

Thx in advance.

(@bukka I know you have been working on a new parser, but ... until that reaches the surface, could we go this way? (with the proper adjustments for sure)

@juan-morales juan-morales changed the title WIP - Json last error msg - better message - error position near by json_last_error_msg - better message - error position near by Jun 17, 2025
Copy link
Member
@DanielEScherzer DanielEScherzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few code style issues

juan-morales and others added 3 commits June 20, 2025 08:32
Co-authored-by: DanielEScherzer <daniel.e.scherzer@gmail.com>
spprintf(&final_message, 0, "%s near character %zu", error_message, JSON_G(error_pos));
break;
default:
spprintf(&final_message, 0, "%s", error_message);
Copy link
Member

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?

@@ -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)
{
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{
{

s->cursor = (php_json_ctype *) str;
s->limit = (php_json_ctype *) str + str_len;
s->options = options;
s->errpos = 0;
s->errcode = 0;
Copy link
Member

Choose a reason for hiding this comment

The 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0