Skip to content

fix(waves): Fix potential size_t to uint16_t incorrect cast#4681

Merged
sergei-boiko-trustwallet merged 3 commits intomasterfrom
fix/waves-bytes-encoding
Mar 6, 2026
Merged

fix(waves): Fix potential size_t to uint16_t incorrect cast#4681
sergei-boiko-trustwallet merged 3 commits intomasterfrom
fix/waves-bytes-encoding

Conversation

@sergei-boiko-trustwallet
Copy link
Copy Markdown
Contributor

This pull request introduces an important validation to the encodeDynamicLengthBytes function in src/Waves/BinaryCoding.h, ensuring that encoded data does not exceed the maximum allowed length.

Validation improvements:

  • Added a check in encodeDynamicLengthBytes to throw an exception if the data size exceeds the uint16_t maximum, preventing oversized data from being encoded.

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

Adds a defensive validation to TW::Waves::encodeDynamicLengthBytes to prevent encoding lengths that exceed what can be represented in the 16-bit length prefix.

Changes:

  • Introduces a maximum-length check intended to prevent size_tuint16_t truncation during encoding.
  • Throws an exception when the input exceeds the allowed length boundary.
Comments suppressed due to low confidence (1)

src/Waves/BinaryCoding.h:21

  • Once the size check is corrected to guard bytes.size(), please add a unit test that exercises the new failure mode (e.g., bytes.size() == uint16_t::max() + 1 should throw) to prevent regressions in this encoding boundary.
    constexpr auto dataLimit = static_cast<size_t>(std::numeric_limits<uint16_t>::max());
    if (data.size() > dataLimit) {
        throw std::invalid_argument("Data size exceeds maximum allowed length");
    }
    encode16BE(static_cast<uint16_t>(bytes.size()), data);

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

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 25, 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

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/Waves/BinaryCoding.h:21

  • std::invalid_argument is used here, but this header does not include <stdexcept>. Relying on transitive includes is not guaranteed and can break compilation depending on include order; add #include <stdexcept> to keep the header self-contained.
    if (bytes.size() > limit) {
        throw std::invalid_argument("Data size exceeds maximum allowed length");
    }

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

@sergei-boiko-trustwallet sergei-boiko-trustwallet merged commit 1c8f4de into master Mar 6, 2026
19 checks passed
@sergei-boiko-trustwallet sergei-boiko-trustwallet deleted the fix/waves-bytes-encoding branch March 6, 2026 07:26
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.

4 participants