Skip to content

Fix TRange issues with clip, contains and random#2987

Merged
slaff merged 3 commits intoSmingHub:developfrom
mikee47:fix/range-clipping
Jan 23, 2026
Merged

Fix TRange issues with clip, contains and random#2987
slaff merged 3 commits intoSmingHub:developfrom
mikee47:fix/range-clipping

Conversation

@mikee47
Copy link
Contributor

@mikee47 mikee47 commented Jan 21, 2026

Avoid truncation of parameter to TRange clip() and contains() methods

For instance, if TRange<int8_t>::clip() is passed an int value which exceeds the natural range of int8_t then it is truncated and the clipping returns an unexpected value. For example:

TRange<int8_t> range(0, 100);

/* Compiler catches this with an overflow error, attempting to convert `int` to `int8_t` */
range.clip(0x1005);

/* But here the compiler says nothing, and 0x1005 is silently truncated to 0x05,
   thus 5 is returned where 100 is expected */
int v = 0x1005;
range.clip(v);

Both clip and contain methods are now declared constexpr so they can be evaluated at compile time if required.

Fix random() for 64-bit values. May need second 32-bit random value to cover range properly

Add tests.

…hods

For instance, calling `TRange<int8_t>::clip()` with an `int` value
which exceeds the natural range of `int8_t` then it is truncated
and the clipping returns an unexpected value.
@what-the-diff
Copy link

what-the-diff bot commented Jan 21, 2026

PR Summary

  • Change 1: Modification to Range.h

    • This change allows the code to handle different types of mathematical limits more effectively, enhancing our system's flexibility and robustness.
    • Some methods have been updated to handle different types of inputs.
    • A method that creates a random number has been improved to handle large numbers better.
  • Change 2: Update to modules.h

    • The Range module has been registered, and it's now included in tests, ensuring the functionality is verified correctly.
  • Change 3: Creation of Range.cpp

    • A new test class has been implemented for verifying the functionality of Range.
    • Various test cases have been added to stress test the functionality, guaranteeing it behaves correctly under different scenarios, like when dealing with very large numbers or when numbers fall outside the specified range.

These changes will make our system more robust, flexible, and easy to debug, enhancing the quality of our product.

@slaff slaff added this to the 6.3.0 milestone Jan 21, 2026
@slaff slaff merged commit fe1abfa into SmingHub:develop Jan 23, 2026
46 of 49 checks passed
@mikee47 mikee47 deleted the fix/range-clipping branch January 23, 2026 21:10
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