Reject malformed input in mg_json_get_hex#3634
Open
94xhn wants to merge 1 commit into
Open
Conversation
scaprile
requested changes
Jul 14, 2026
scaprile
left a comment
Collaborator
There was a problem hiding this comment.
You need to sign the CLA if you want this to be merged, otherwise we will close this PR and may do something different that serves the same purpose.
Comment on lines
+370
to
+374
| if (!mg_str_to_num(mg_str_n(json.buf + off + 1 + i, 2), 16, | ||
| &result[i >> 1], sizeof(uint8_t))) { | ||
| mg_free(result); | ||
| return NULL; | ||
| } |
Collaborator
There was a problem hiding this comment.
I don't like this style, it shifts the attention from the algorithm to handling wreckage. I would break on a single line and check/free after the loop, also on a single line
Collaborator
|
Please do not add those long, ugly, and useless AI-generated commit comments; and please let me know when you signed the CLA. |
scaprile
self-requested a review
July 14, 2026 20:40
scaprile
approved these changes
Jul 14, 2026
Reject odd-length and non-hex strings before decoding them. Tested: 1796 ASan/UBSan tests in amalgamated and unamalgamated builds
94xhn
force-pushed
the
fix/json-hex-validation
branch
from
July 16, 2026 00:34
ae9d86f to
dc52d8b
Compare
Author
|
I have signed the Cesanta CLA. I also squashed the branch to one commit and shortened the commit message as requested. |
Collaborator
|
@cpq WDYT ? LGTM |
cpq
approved these changes
Jul 22, 2026
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.
mg_json_get_hex()currently ignores failedmg_str_to_num()calls and processes odd-length strings in two-character steps. As a result,0gsucceeds as byte0x00, andabcsucceeds as byte0xabinstead of reporting invalid input.Reject odd-length and non-hex strings while preserving valid input and the existing empty-string behavior. The unit tests now cover empty, odd-length, and non-hex inputs.
Testing:
make -B -C test test TFLAGS=-DLOCALHOST_ONLY(ASan/UBSan, 1796 tests)make -B -C test unamalgamated TFLAGS=-DLOCALHOST_ONLY, thenunit_test(ASan/UBSan, 1796 tests)mongoose.candmongoose.hwith the repository Makefile; no diff