Skip to content

Handle String::toInt overflow consistently - #283

Open
Firat17aze wants to merge 1 commit into
arduino:masterfrom
Firat17aze:fix/string-toint-overflow
Open

Firat17aze wants to merge 1 commit into
arduino:masterfrom
Firat17aze:fix/string-toint-overflow

Conversation

@Firat17aze

Copy link
Copy Markdown

String::toInt() delegates to atol(), whose behavior is undefined when the input is outside the range of long. As a result, oversized values can produce different results across Arduino platforms. This fixes #175.

The conversion now accumulates decimal digits with explicit bounds checks. It returns 0 before overflow, accepts the exact LONG_MIN and LONG_MAX boundaries, preserves the existing leading-whitespace, sign, and trailing-text behavior, and avoids relying on global errno state.

Validation:

  • Focused String::toInt() tests: 12 assertions in 7 test cases passed.
  • Full host suite: 980 assertions in 309 test cases passed.
  • AddressSanitizer and UndefinedBehaviorSanitizer full host suite: 980 assertions in 309 test cases passed.
  • AVR-GCC 9.5 compiled api/String.cpp for ATmega328P with -Os -Wall -Wextra -Werror.
  • git diff --check passed.

No hardware testing was performed; this is a conversion-only change covered by host tests and AVR compilation.

LLMs were used for review and code generation in some parts of this pull request.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

String::toInt() and atol() behave different on different platforms with a number that is too large.

2 participants