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

String == String does not work with embedded NUL characters #112

Open
cvwillegen opened this issue Apr 18, 2020 · 8 comments
Open

String == String does not work with embedded NUL characters #112

cvwillegen opened this issue Apr 18, 2020 · 8 comments

Comments

@cvwillegen
Copy link

return strcmp(buffer, s.buffer);
and
return strcmp(buffer, s.buffer);
should use memcmp() to make sure that a String object can be compared to another String object that has embedded NUL characters

@cvwillegen
Copy link
Author

Also,

return strcmp(buffer, cstr) == 0;
return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
have the same problem.

You can debate if there is a change needed to

unsigned char String::equalsIgnoreCase( const String &s2 ) const
, but String::equalsIgnoreCase implies only printable characters.

@matthijskooijman
Copy link
Collaborator

Did you see #97? It is related and I think it might solve your issue (but it's been too long since I wrote it, so I cannot recall exactly).

@cvwillegen
Copy link
Author

cvwillegen commented Apr 21, 2020 via email

@prath06
Copy link

prath06 commented Nov 3, 2020

Instead of strcmp(), try using stricmp(). It may work

@matthijskooijman
Copy link
Collaborator

@cvwillegen, seems you're right, I should probaly dust off that PR sometime and also fix those calls...

@prath06, but it seems that stricmp() just makes the comparison case insensitive, and also it does not seem to be a standard libc function at all. How would this help here?

@mrengineer7777
Copy link

In C, C strings are just an array of chars terminated by NULL. That is how strcmp(), strcpy(), etc know where the end of the string is. The Arduino String class does a nice job hiding those implementation details, but the fact remains: embedded nulls in Strings WILL BREAK THINGS.

@matthijskooijman
Copy link
Collaborator

In C, C strings are just an array of chars terminated by NULL.

Sure, that's how C strings work. But the String class is something different - it's a buffer with an explicit length, so there is no reason why embedded nuls could not be supported by it.

Sure, we could define this as an (artificial) limitation on the String class as well, which could make its implementation simpler (because it can then just use the C-string functions for its processing without any extra work), but changes made to String in the past suggest that this is not the intention, and the goal is to make String work even with embedded nuls.

@cvwillegen
Copy link
Author

memcmp is a standard function in the C library...

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

No branches or pull requests

4 participants