glib-aux: improve nm_ref_string_equals_str() to work for non-C-strings

A NMRefString tracks the length seprately, it thus may not be a NUL terminated
string (although, there is always a NUL character at the end of the buffer).

As such, the previous implementation did not work correctly in when comparing
for example NMRefString("a\0b") with "a". There was even a comment hinting
to that fact. Instead of making obscure comments, fix the implementation to
behave always correctly.
This commit is contained in:
Thomas Haller
2021-05-20 19:05:56 +02:00
parent ce7c28c514
commit ac8c3a7111

View File

@@ -95,13 +95,15 @@ nm_ref_string_get_len(NMRefString *rstr)
}
static inline gboolean
nm_ref_string_equals_str(NMRefString *rstr, const char *s)
nm_ref_string_equals_str(NMRefString *rstr, const char *str)
{
/* Note that rstr->len might be greater than strlen(rstr->str). This function does
* not cover that and would ignore everything after the first NUL byte. If you need
* that distinction, this function is not for you. */
if (!str)
return (!!rstr);
return rstr ? (s && nm_streq(rstr->str, s)) : (s == NULL);
if (!rstr)
return FALSE;
return rstr->len == strlen(str) && (rstr->str == str || memcmp(rstr->str, str, rstr->len) == 0);
}
static inline gboolean