CBOR-to-JSON: fix memory leak when parsing invalid CBOR#279
Merged
thiagomacieira merged 1 commit intointel:mainfrom Mar 14, 2025
Merged
Conversation
When function text_string_to_escaped successfully parses a string and
fails to parse the next value (cbor_value_finish_string_iteration
returns an error), it correctly propagates the error but the string is
never freed.
This can be reproduced with:
make CC='clang -g -fsanitize=address'
printf '\x82\x60\xff' | ./bin/cbordump -j
clang's Address Sanitizer reports:
=================================================================
==20317==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 1 byte(s) in 1 object(s) allocated from:
#0 0x560b654b9916 in __interceptor_realloc (/tinycbor/bin/cbordump+0xa4916) (BuildId: f9933666b5d987b21f68c2887de4aebe93bc2bef)
intel#1 0x560b654f5c18 in escape_text_string /tinycbor/src/cbortojson.c:331:15
intel#2 0x560b654f3e29 in text_string_to_escaped /tinycbor/src/cbortojson.c:377:19
intel#3 0x560b654f267d in value_to_json /tinycbor/src/cbortojson.c:674:19
intel#4 0x560b654f34c2 in array_to_json /tinycbor/src/cbortojson.c:545:25
intel#5 0x560b654f2085 in value_to_json /tinycbor/src/cbortojson.c:627:19
intel#6 0x560b654f1baf in cbor_value_to_json_advance /tinycbor/src/cbortojson.c:816:12
intel#7 0x560b654ea928 in dumpFile /tinycbor/tools/cbordump/cbordump.c:76:19
intel#8 0x560b654ead2b in main /tinycbor/tools/cbordump/cbordump.c:149:9
intel#9 0x7fa9d7629d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
SUMMARY: AddressSanitizer: 1 byte(s) leaked in 1 allocation(s).
Fix this by freeing the string when cbor_value_finish_string_iteration
fails.
Fixes: e072bc1 ("CBOR-to-JSON: do properly escape JSON strings")
Member
|
I'll add a Mac |
Member
No failures reported in https://github.com/thiagomacieira/tinycbor/actions/runs/13861024310/job/38789295761. We don't test |
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.
When function
text_string_to_escaped successfullyparses a string and fails to parse the next value (cbor_value_finish_string_iterationreturns an error), it correctly propagates the error but the string is never freed.This can be reproduced with:
clang's Address Sanitizer reports:
Fix this by freeing the string when cbor_value_finish_string_iteration fails.
Fixes: e072bc1 ("CBOR-to-JSON: do properly escape JSON strings", #274)