Skip to content

Fails gracefully when allocation fails during array/map construction - #224

Merged
PJK merged 3 commits into
PJK:masterfrom
James-ZHANG:fix-bad-memory-access
Dec 3, 2022
Merged

PJK merged 3 commits into
PJK:masterfrom
James-ZHANG:fix-bad-memory-access

Conversation

@James-ZHANG

@James-ZHANG James-ZHANG commented Sep 29, 2022 •

Copy link
Copy Markdown
Contributor

Fails gracefully when allocation fails during array/map construction

Description

Return values of cbor_array_push, _cbor_map_add_key and _cbor_map_add_value are unchecked. An unchecked false return value here implies potential out-of-bound memory access.

Checklist

  • I have read followed CONTRIBUTING.md
    • I have added tests
    • I have updated the documentation
    • I have updated the CHANGELOG
  • Are there any breaking changes? If so, are they documented?
    No
  • Does this PR introduce any platform specific code? If so, is this captured in the description?
    No
  • Security: Does this PR potentially affect security? If so, is this captured in the description?
    No
  • Performance: Does this PR potentially affect performance? If so, is this captured in the description?
    No

@PJK

PJK commented Oct 18, 2022

Copy link
Copy Markdown
Owner

Apologies for the delay and thanks a ton for the PR!

Isn't there still a problem with the memory cleanup? I think that when a nested _cbor_builder_append fails, all the partially built items on the ctx->stack should be freed up, but creation_failed is never checked as we "unwind the stack"

@James-ZHANG

Copy link
Copy Markdown
Contributor Author

all the partially built items on the ctx->stack should be freed up

Those are already freed up in cbor_load.

I think the invariant of _cbor_builder_append is it shouldn't have any memory leak: the passed item either becomes part of stack->root; or in case of failure, gets freed properly (and in that case it leaves partially built stack items behind).

Added a function comment emphasizing that.

@PJK

PJK commented Dec 3, 2022

Copy link
Copy Markdown
Owner

Makes sense, thank you! This could use better docs, filed #226

@PJK
PJK merged commit ff66e8c into PJK:master Dec 3, 2022
PJK added a commit that referenced this pull request Dec 3, 2022
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 this pull request may close these issues.

2 participants

⌂ 换网址