arduino / arduino/ArduinoCore-avr
`Stream::find` does incorrect comparison
- Dominant language
- C
- Stars
- 1.5k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
This code hangs forever, even when `Serial1` repeatedly receives `0xCA`:
```cpp
while (!Serial1.find(0xCA)) ;
```
The reason is in `Stream::findMulti`:
https://github.com/arduino/ArduinoCore-avr/blob/86df345b3cf46754a5db38fb983ec2808ce31303/cores/arduino/Stream.cpp#L270
Consider a comparison where:
- `c` is `int` with an unsigned value `0xCA`.
- `t->str` is `const char *` with a signed value `-54` decimal, which as an unsigned value is `0xCA`.
In the comparison, the char is sign extended to an `int`, resulting in `0xFFFFFFCA` giving:
```cpp
if (0xCA == 0xFFFFFFCA) {
```
This is never `true` and not the intention. There appear to be other comparisons in `Stream.cpp` with this problem.
### Additional context
Tested on Arduino Mega 2560 R3.
#### Related
- https://github.com/arduino/ArduinoCore-API/pull/274
- https://github.com/arduino/ArduinoCore-avr/issues/249
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in cores/arduino/Stream.cpp at Stream::findMulti and inspect the comparison at the linked line, then review the other Stream.cpp comparisons mentioned in the issue. Reproduce the Serial1.find(0xCA) example on the Mega 2560 setup; done means the matching byte is recognized and the loop no longer hangs.
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