arduino / arduino/ArduinoCore-API

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

Open
#175 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
306
Forks
150
PR merge metrics
No merged PRs in 30d

Description

User axemaster found strange behavior when using String::toInt() with a number that is too large. See the topic in the Arduino forum: [https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669).
The result is different for 8-bit and 32-bit platforms.
It was narrowed down to the undefined behaviour of atol() when a number is too large to fit in a long.

The atol() in String::toInt() in String.cpp could be replaced by strtol() followed by a check for LONG_MAX or LONG_MIN and return zero if that happens. The documentation can stay as it is, because it will return a long, and zero if something is wrong.
To allow LONG_MAX and LONG_MIN as valid input numbers, the errno has to be used. I don't know how that will hold up in a multitasking environment.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in String.cpp at String::toInt() and review how atol() behaves for values outside the platform's long range. Compare the proposed strtol() and errno handling across 8-bit and 32-bit platforms; the issue is done when oversized inputs produce consistent documented results without mishandling LONG_MAX or LONG_MIN.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.