ArduinoCore-API icon indicating copy to clipboard operation
ArduinoCore-API copied to clipboard

String == String does not work with embedded NUL characters

Open cvwillegen opened this issue 5 years ago • 8 comments

https://github.com/arduino/ArduinoCore-API/blob/7f8de5869ee3de7a915bf1db64313c08595b3ac3/api/String.cpp#L445 and https://github.com/arduino/ArduinoCore-API/blob/7f8de5869ee3de7a915bf1db64313c08595b3ac3/api/String.cpp#L445 should use memcmp() to make sure that a String object can be compared to another String object that has embedded NUL characters

cvwillegen avatar Apr 18 '20 08:04 cvwillegen

Also, https://github.com/arduino/ArduinoCore-API/blob/7f8de5869ee3de7a915bf1db64313c08595b3ac3/api/String.cpp#L467 https://github.com/arduino/ArduinoCore-API/blob/7f8de5869ee3de7a915bf1db64313c08595b3ac3/api/String.cpp#L492 https://github.com/arduino/ArduinoCore-API/blob/7f8de5869ee3de7a915bf1db64313c08595b3ac3/api/String.cpp#L498 have the same problem.

You can debate if there is a change needed to https://github.com/arduino/ArduinoCore-API/blob/7f8de5869ee3de7a915bf1db64313c08595b3ac3/api/String.cpp#L470, but String::equalsIgnoreCase implies only printable characters.

cvwillegen avatar Apr 18 '20 08:04 cvwillegen

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).

matthijskooijman avatar Apr 21 '20 11:04 matthijskooijman

Yes, I saw your pull request (and commented on it). As far as I can see, it still has strcmp() calls in it... please correct me if I'm wrong!

Christ van Willegen

cvwillegen avatar Apr 21 '20 14:04 cvwillegen

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

prath06 avatar Nov 03 '20 16:11 prath06

@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?

matthijskooijman avatar Nov 04 '20 11:11 matthijskooijman

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.

mrengineer7777 avatar May 04 '22 13:05 mrengineer7777

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.

matthijskooijman avatar May 04 '22 13:05 matthijskooijman

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

cvwillegen avatar May 04 '22 13:05 cvwillegen