Skip to content

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

Open

Description

@cvwillegen

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

Activity

  1. cvwillegen commented on Apr 18, 2020

    @cvwillegen
    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.

  2. matthijskooijman commented on Apr 21, 2020

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

  3. cvwillegen commented on Apr 21, 2020

    @cvwillegen
    Author
  4. prath06 commented on Nov 3, 2020

    @prath06

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

  5. matthijskooijman commented on Nov 4, 2020

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

  6. mrengineer7777 commented on May 4, 2022

    @mrengineer7777

    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.

  7. matthijskooijman commented on May 4, 2022

    @matthijskooijman
    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.

  8. cvwillegen commented on May 4, 2022

    @cvwillegen
    Author

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

  9. added a commit that references this issue on Jul 28, 2026
    2000eb1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions