Skip to content

Fixes typecast in Cbor#4707

Open
nikhil-gupta-tw wants to merge 4 commits intomasterfrom
fix/cbor
Open

Fixes typecast in Cbor#4707
nikhil-gupta-tw wants to merge 4 commits intomasterfrom
fix/cbor

Conversation

@nikhil-gupta-tw
Copy link
Copy Markdown

This pull request improves the robustness of CBOR decoding by adding overflow checks when handling bytes and string lengths, preventing potential security or stability issues due to excessively large or malformed data. The main changes focus on ensuring that length calculations do not exceed the maximum value representable by a 32-bit unsigned integer.

CBOR decoding overflow protection:

  • Added explicit overflow checks in Decode::getTotalLen() to throw an exception if the total length of bytes or string data exceeds UINT32_MAX, preventing silent wrapping or undefined behavior.
  • Updated Decode::getBytes() to check for overflow when calculating the required length, throwing an exception if the total exceeds UINT32_MAX or if the available data is insufficient.
  • Modified Decode::isValid() to use a 64-bit variable for length calculation, ensuring that validity checks are accurate even for large data lengths.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the CBOR decoder against integer overflow when computing total lengths for bytes/string items, reducing the risk of wraparound and incorrect bounds checks on malformed inputs.

Changes:

  • Added explicit overflow detection in Decode::getTotalLen() for bytes/string total lengths.
  • Updated Decode::getBytes() to compute required length in 64-bit and reject values exceeding UINT32_MAX.
  • Adjusted Decode::isValid() to use 64-bit length calculation for bytes/string validity checks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@sergei-boiko-trustwallet sergei-boiko-trustwallet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just code style comments

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 23, 2026

Binary size comparison

➡️ aarch64-apple-ios: 14.34 MB

➡️ aarch64-apple-ios-sim: 14.34 MB

➡️ aarch64-linux-android: 18.77 MB

➡️ armv7-linux-androideabi: 16.20 MB

➡️ wasm32-unknown-emscripten: 13.68 MB

Copy link
Copy Markdown
Contributor

@sergei-boiko-trustwallet sergei-boiko-trustwallet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a couple questions

case MT_string:
return (uint32_t)typeDesc.byteCount + (uint32_t)typeDesc.value;
{
uint64_t totalLen = static_cast<uint64_t>(typeDesc.byteCount) + typeDesc.value;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this sum overflow?

}
auto len = (uint32_t)typeDesc.value;
if (length() < (uint32_t)typeDesc.byteCount + (uint32_t)len) {
uint64_t requiredLen = static_cast<uint64_t>(typeDesc.byteCount) + typeDesc.value;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question

case MT_string:
{
auto len = (uint32_t)(typeDesc.byteCount + typeDesc.value);
uint64_t len = static_cast<uint64_t>(typeDesc.byteCount) + typeDesc.value;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

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.

3 participants