Patch rollover-unsafe millis() deadline checks in RF24Network.cpp - #270
Merged
Conversation
Copilot
AI
changed the title
[WIP] Fix rollover-unsafe millis() timeout patterns
Patch rollover-unsafe Jun 4, 2026
millis() deadline checks in RF24Network.cpp
TMRh20
marked this pull request as ready for review
June 4, 2026 08:56
2bndy5
approved these changes
Jun 4, 2026
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.
This PR replaces rollover-unsafe timeout logic in
RF24Networkthat used absolute deadlines (millis() + N) with elapsed-time comparisons based on unsigned subtraction. This preserves current timing behavior while making the checks correct acrossmillis()wraparound.update()overrun timeouttimeout = millis() + 100/if (millis() > timeout)with a start-time + elapsed check.Fragmentation retry delay
timer = millis() + 2/while (millis() < timer)with an elapsed-time wait loop.Pattern consistency
Original prompt
Patch rollover-unsafe
millis()timeout patterns in thenRF24/RF24Networkrepository by replacing absolute-deadline logic likeuint32_t timeout = millis() + N; while (millis() < timeout) {}orif (millis() > timeout)with rollover-safe elapsed-time comparisons using unsigned subtraction, such asconst uint32_t start = millis(); while (millis() - start < N) {}.Confirmed unsafe sites already identified from repository code/search results:
RF24Network.cpp:uint32_t timeout = millis() + 100;withif (millis() > timeout)inupdate().RF24Network.cpp: fragmentation retry delay usinguint32_t timer = millis() + 2; while (millis() < timer) {}.Requirements:
nRF24/RF24Network.millis()deadline comparisons are discovered during implementation in this repo and they are clearly the same class of bug, patch them too.The following is the prior conversation context from the user's chat exploration (may be truncated):
User: can you find all rollover unsafe millis() calls in RF24, RF24Network, RF24Mesh, RF24Ethernet & RF24Gateway?
Assistant: Yes — I found the clearly rollover-unsafe
millis()usage patterns in those repos.Unsafe pattern I looked for: storing an absolute deadline like
timeout = millis() + Nand later comparing withmillis() < timeoutor similar. That breaks acrossmillis()wraparound.Safe pattern:
if (millis() - start >= interval)using unsigned subtraction.Confirmed rollover-unsafe calls
RF24Mesh
RF24Mesh.cpp—requestAddress()This is rollover-unsafe because it compares current time to an absolute future timestamp. If wrap happens before/around the loop, the condition can fail early or behave incorrectly.
RF24Ethernet
RF24Client.h—ConnectStatein-class initializationThese initializations are not inherently unsafe by themselves, but they are suspect state seeds because they store absolute times. Whether they’re unsafe depends on how they’re compared later.
RF24Client.cpp— reopen-window timingThis one is safe. Included here only because it came up in search and uses the correct subtraction form.
RF24Client.cpp—waitAvailable()Also safe.
So for RF24Ethernet, from the results I could verify, I found no confirmed unsafe compare pattern, only timestamp storage in
ConnectStatethat should be audited further if you want exhaustive proof.RF24Gateway
RF24Gateway.cpp— ARP timeout waitThis is safe.
The gateway examples also use:
Those are safe too. Example:
examples/RF24GatewayNode.cpp.So for RF24Gateway, I found no confirmed rollover-unsafe
millis()comparisons in the surfaced results.Repos with no confirmed unsafe hits from what I could verify
millis()comparison surfaced in the returned results.This pull request was created from Copilot chat.