Skip to content
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

Bug: Proper json_last_error() handling with try catch #882

Closed
Pilskalns opened this issue Nov 29, 2023 · 0 comments · Fixed by #883
Closed

Bug: Proper json_last_error() handling with try catch #882

Pilskalns opened this issue Nov 29, 2023 · 0 comments · Fixed by #883

Comments

@Pilskalns
Copy link
Contributor

So, the last release introduces more strict JSON syntax check.

There is an inherent/global issue with how json_last_error() works. In short, the returned error code can be a false positive. It does not reset after a successful encode or decode call. It changes only when there is a new error code.

At the issue core is the following sequence, which gives unintuitive results:

<?php
json_decode("\00invalid json");
var_dump(json_last_error());

json_decode("[]", true, 512, JSON_THROW_ON_ERROR);
var_dump(json_last_error());

Both var dumps would return an error code. You can read about the issue source over this thread: php/php-src#10166

While in isolation this works ok, in the integration it becomes unreliable if, in the same call/request, there was another error code thrown (i.e. to check if a string is a json). I'm preparing a PR that uses try-catch to handle the error properly.

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

Successfully merging a pull request may close this issue.

1 participant