Skip to content

Fix NULL dereference in cbor_copy/cbor_copy_definite on allocation failure - #419

Merged
PJK merged 2 commits into
masterfrom
fix/cbor-copy-null-deref
Apr 3, 2026
Merged

PJK merged 2 commits into
masterfrom
fix/cbor-copy-null-deref

Conversation

@PJK

@PJK PJK commented Apr 3, 2026

Copy link
Copy Markdown
Owner

Fixes #417.

Summary

  • _cbor_copy_int: cbor_mark_negint(res) was called unconditionally after the switch, even if the preceding cbor_build_uint*() returned NULL on allocation failure. Added early return NULL before the dereference.
  • cbor_copy_definite bytestring: cbor_bytestring_set_handle was called on a potentially-NULL res from cbor_new_definite_bytestring(). Added early return with _cbor_free(combined_data) to avoid the dereference and the buffer leak.
  • cbor_copy_definite string: identical pattern to the bytestring case, same fix.

Test plan

  • New tests added for all four negint widths (test_negint{8,16,32,64}_alloc_failure) — confirm cbor_copy returns NULL rather than crashing when allocation fails
  • New test_definite_bytestring_item_alloc_failure — covers the path where combined_data malloc succeeds but cbor_new_definite_bytestring fails
  • New test_definite_string_item_alloc_failure — same for strings
  • All 65 copy_test cases pass

🤖 Generated with Claude Code

PJK and others added 2 commits April 4, 2026 00:39
…ilure

Three spots dereferenced the result of an allocation without a NULL check:
- _cbor_copy_int: cbor_mark_negint(res) called even if cbor_build_uint*
  returned NULL (reported in issue #417)
- cbor_copy_definite bytestring: cbor_bytestring_set_handle called on NULL
  res; also leaked combined_data on failure
- cbor_copy_definite string: same pattern as bytestring

Each fix adds an early return NULL (with _cbor_free of any already-allocated
buffer) before the dereference. Tests added for all three paths, including
the specific case where the item allocation fails after the buffer allocation
succeeds.

Fixes #417

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codecov

codecov Bot commented Apr 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.94%. Comparing base (e1f711a) to head (2df97ab).
⚠️ Report is 8 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #419   +/-   ##
=======================================
  Coverage   99.94%   99.94%           
=======================================
  Files          20       20           
  Lines        1747     1754    +7     
=======================================
+ Hits         1746     1753    +7     
  Misses          1        1           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@PJK
PJK merged commit 109bb7c into master Apr 3, 2026
19 checks passed
@PJK
PJK deleted the fix/cbor-copy-null-deref branch April 3, 2026 22:44
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.

[Security] Multiple NULL pointer dereferences in cbor_copy() on allocation failure

1 participant

⌂ 换网址