refstr: be extra careful about calling memcpy() with dangling pointer

This commit is contained in:
Thomas Haller 2021-03-18 09:54:12 +01:00
parent 4f935d1d6b
commit 33a69bbde6
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -44,7 +44,11 @@ _ref_string_equal(gconstpointer ptr_a, gconstpointer ptr_b)
_ref_string_get(ptr_a, &cstr_a, &len_a);
_ref_string_get(ptr_b, &cstr_b, &len_b);
return len_a == len_b && memcmp(cstr_a, cstr_b, len_a) == 0;
/* memcmp() accepts "n=0" argument, but it's not clear whether in that case
* all pointers must still be valid. The input pointer might be provided by
* the user via nm_ref_string_new_len(), and for len=0 we want to allow
* also invalid pointers. Hence, this extra "len_a==0" check. */
return len_a == len_b && (len_a == 0 || (memcmp(cstr_a, cstr_b, len_a) == 0));
}
/*****************************************************************************/