Repository navigation
Conversation
Fixes WordPress#651. When json_decode() receives invalid JSON it returns null. Previously the null value was passed to empty($data['key']), which on PHP 8.x generates an "Undefined array key" or "Trying to access array offset on value of type null" warning for every key access. Add an is_array() check immediately after json_decode(); return null when the input is not a valid JSON object/array. Update the @return docblock to reflect the nullable return type. Add test_json_unserialize_returns_null_for_invalid_json() covering malformed JSON, an empty string, a JSON null literal, and a plain JSON string.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #651.
What problem does this fix?
Base_Image::json_unserialize()calledjson_decode($json, true)but never verified the result was a valid array before accessing$data['source']and$data['alt']. When$jsonis invalid JSON (empty string, a bare scalar, a JSON null, etc.),json_decode()returnsnull. Accessing keys on anullvalue on PHP 8.x produces:Changes
includes/image/class-base-image.php: add anis_array()check immediately afterjson_decode(); returnnullwhen the decoded value is not an array. Update the@returndocblock to reflect the nullable return type.tests/phpunit/tests/test-base-image.php: addtest_json_unserialize_returns_null_for_invalid_json()covering malformed JSON, an empty string, a JSONnullliteral, and a plain JSON string.Testing
Use of AI Tools
AI assistance: Yes
Tool(s): Claude
Model(s): claude-sonnet-4-6
Used for: Code implementation and test; reviewed and verified by contributor.