Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream#find does incorrect comparison #541

Open
NathanSweet opened this issue Sep 6, 2023 · 1 comment
Open

Stream#find does incorrect comparison #541

NathanSweet opened this issue Sep 6, 2023 · 1 comment
Labels

Comments

@NathanSweet
Copy link

NathanSweet commented Sep 6, 2023

This code hangs forever, even when Serial repeatedly receives 0xCA, tested on Arduino Mega 2560 R3:

while (!Serial1.find(0xCA)) ;

The reason is in Stream findMulti:
https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Stream.cpp#L270

Consider this comparison:

if (c == t->str[t->index]) {

c is int with an unsigned value 0xCA.
t->str is const char * with (on Arduino Mega 2560 R3) 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:

if (0xCA == 0xFFFFFFCA) {

This is never true and not the intention. There appear to be other comparisons in Stream.cpp with this problem.

@NathanSweet
Copy link
Author

Here is a fix:

    int cc = timedRead();
    if (cc < 0)
      return -1;
    char c = cc;

I leave it to those more familiar with this codebase to decide on the best fix.

@per1234 per1234 added the bug label Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants