thecodingmachine / safe

All PHP functions, rewritten to throw exceptions instead of returning false
MIT License
2.36k stars 143 forks source link

Skip `json_last_error` check when `JSON_THROW_ON_ERROR` flag is set in `json_decode` #451

Open marmichalski opened 1 month ago

marmichalski commented 1 month ago

This handles a rather unpleasant quirk in PHP where if json_decode is called without JSON_THROW_ON_ERROR_FLAG for an invalid json string before calling json_decode for a valid json string with said flag the json_error_last method will report the error from the first call, instead of being reset on the second.

I've implemented a simple test to illustrate how this happens.

https://bugs.php.net/bug.php?id=77997

Also found this: https://github.com/thecodingmachine/safe/issues/437#issuecomment-2139132472