Skip to content

router: fix 64bit overflow clamping#126

Merged
eyck merged 3 commits into
Minres:mainfrom
sanpar-qcom:qcom-fix-64bit-overflow-clamping
Apr 21, 2026
Merged

router: fix 64bit overflow clamping#126
eyck merged 3 commits into
Minres:mainfrom
sanpar-qcom:qcom-fix-64bit-overflow-clamping

Conversation

@sanpar-qcom
Copy link
Copy Markdown
Contributor

  • add logic to avoid clamp overflow to router
  • add regression test for high-range address

Comment thread src/components/scc/router.h Outdated
// make sure end address does not exceed size
auto remap_end = (tranges[idx].remap ? 0 : tranges[idx].base) + tranges[idx].size;
if(tranges[idx].size && dmi_data.get_end_address() >= remap_end)
using addr_t = decltype(address);
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.

address has a fixed type deaclared in line 294. No need to get the type using decltype()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I understand that, but my intention here is to make sure we always use the same data type as address, even if someone changes the type of the variable address without knowing this part of code exists.

I admit it is a small possibility (but probably close to 0), but when it happens, all those intermediate variables naturally follow the type rather than two different types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in d69a53c

Comment thread src/components/scc/router.h Outdated
auto remap_end = (tranges[idx].remap ? 0 : tranges[idx].base) + tranges[idx].size;
if(tranges[idx].size && dmi_data.get_end_address() >= remap_end)
using addr_t = decltype(address);
auto const remap_base = tranges[idx].remap ? addr_t{0} : static_cast<addr_t>(tranges[idx].base);
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.

Why do you have these static_cast here and in the next line? They make reading the code difficult and are IMO not needed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same as before - plus another chance where tranges[].base changes its type.
Unless we have a guarantee (i.e., the type of tranges[idx].base is deduced from address), I want to make sure the final type would be the same as address - with logical assumptions that all of these types should be static-cast-able.

Combined with above, it makes me a bit hesitant to use uint64_t here. Although, if you feel strong about this, I'm happy to hardcode uint64_t here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in d69a53c

Comment thread src/components/scc/router.h Outdated
auto const remap_base = tranges[idx].remap ? addr_t{0} : static_cast<addr_t>(tranges[idx].base);
auto const remap_size = static_cast<addr_t>(tranges[idx].size);
auto const remap_end = remap_base + remap_size;
auto const remap_end_overflow = remap_size > (std::numeric_limits<addr_t>::max() - remap_base);
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.

This can be simplified. In C++, unsigned datatypes have a defined overflow behavior. If remap_end is less than remap base we have an overflow. This coud be simplified to auto const remap_end_overflow = remap_end < remap_base;
With this simplification you could combine it with the previous line auto const remap_end_overflow = (remap_base + remap_size) < remap_base;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in d69a53c

Copy link
Copy Markdown
Contributor

@eyck eyck left a comment

Choose a reason for hiding this comment

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

LGTM

@eyck eyck merged commit 0f53d30 into Minres:main Apr 21, 2026
4 checks passed
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