Add clamping logic for milliseconds conversion and unit tests#10326
Merged
Conversation
Removed detailed comments about seconds to milliseconds conversion.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens Default interval helpers against overflow/wrap when converting seconds→milliseconds and when applying congestion scaling, aligning the returned millisecond values with the INT32_MAX sentinel used elsewhere (e.g., for OSThread::runOnce()-style intervals). It also adds unit tests to lock in the new clamping behavior and prevent regressions.
Changes:
- Add a seconds→milliseconds conversion helper that clamps at
INT32_MAXto preventsecs * 1000wrap. - Add saturation logic in
getConfiguredOrDefaultMsScaled()to avoid undefined behavior when scaled values exceed representable ranges. - Add unit tests covering threshold behavior, UINT32_MAX inputs, and scaled overflow saturation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/mesh/Default.cpp |
Introduces clamped seconds→ms conversion and saturation for scaled intervals. |
test/test_default/test_main.cpp |
Adds unit tests validating clamping/saturation behavior for both unscaled and scaled paths. |
Comments suppressed due to low confidence (1)
src/mesh/Default.cpp:64
- In getConfiguredOrDefaultMsScaled(), the saturation check is done using double precision, but the returned value is computed again as
base * coefin float and then implicitly converted to uint32_t. Because float has ~256ms granularity around ~2.1e9, this second multiplication can round up past INT32_MAX even when the double check passed, reintroducing a value that will become negative when cast to int32_t downstream. Compute the scaled value once in double, clamp it to MAX_MS, and then cast (optionally rounding) so the returned uint32_t is guaranteed <= INT32_MAX.
float coef = congestionScalingCoefficient(numOnlineNodes);
if (static_cast<double>(base) * static_cast<double>(coef) >= static_cast<double>(MAX_MS))
return MAX_MS;
return base * coef;
}
uint32_t Default::getConfiguredOrMinimumValue(uint32_t configured, uint32_t minValue)
{
Evil8it
pushed a commit
to Evil8it/ME4TACTNK
that referenced
this pull request
Jun 10, 2026
…stic#10326) * Add clamping logic for milliseconds conversion and unit tests * Simplify comments in secondsToMsClamped function Removed detailed comments about seconds to milliseconds conversion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.