Skip to content

Add clamping logic for milliseconds conversion and unit tests#10326

Merged
thebentern merged 3 commits into
masterfrom
ms-overflow
Apr 29, 2026
Merged

Add clamping logic for milliseconds conversion and unit tests#10326
thebentern merged 3 commits into
masterfrom
ms-overflow

Conversation

@thebentern

Copy link
Copy Markdown
Contributor

No description provided.

@thebentern thebentern requested review from Copilot and mverch67 April 28, 2026 15:42
@github-actions github-actions Bot added needs-review Needs human review enhancement New feature or request labels Apr 28, 2026
Removed detailed comments about seconds to milliseconds conversion.

Copilot AI left a comment

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.

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_MAX to prevent secs * 1000 wrap.
  • 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 * coef in 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)
{

@thebentern thebentern merged commit 8d8ff21 into master Apr 29, 2026
153 of 156 checks passed
@caveman99 caveman99 deleted the ms-overflow branch June 7, 2026 20:56
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request needs-review Needs human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants