From ac8c3a711148d327bf6ee4fa99fc99eb1c8ca160 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 20 May 2021 19:05:56 +0200 Subject: [PATCH] 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. --- src/libnm-glib-aux/nm-ref-string.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libnm-glib-aux/nm-ref-string.h b/src/libnm-glib-aux/nm-ref-string.h index 400758db5..b7c679f6c 100644 --- a/src/libnm-glib-aux/nm-ref-string.h +++ b/src/libnm-glib-aux/nm-ref-string.h @@ -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