[PATCH v2 3/7] string/test-str*cmp: remove stupid_[strcmp, strncmp, wcscmp, wcsncmp].

Florian Weimer fweimer@redhat.com
Mon Jan 10 13:16:40 GMT 2022


* Wilco Dijkstra:

> Hi Florian,
>
>>>> These implementations are incorrect. There may be a mismatch in s1/s2
>>>> before invalid memory but no null CHAR / length boundary.
>>>
>>> This is not true, see
>>> eg. https://en.cppreference.com/w/cpp/string/byte/strcmp.  Most string
>>> functions require that the string is correctly terminated. There are
>>> only a few exceptions (memchr IIRC), and several generic
>>> implementations use strlen or strnlen before the main loop.
>>
>> I am not sure if those are bugs.  Don't we support non-array usage in
>> these functions as an extension?  At least for strncmp and strnlen and
>> their wide counterparts.  C11 is pretty clear that strncmp operates on
>> arrays, so this is an extension.
>
> My concern is about being able to read beyond a mismatch (which you have
> to anyway when you process more than 1 character per iteration) rather than
> reading beyond the end of a string or array.
>
> What do you mean with non-array usage? Reading beyond the size
> parameter in a strn* function if a NUL terminator has not been found
> yet? Or not stopping at NUL before the size?

strncmp is commonly used as a starts-with-prefix function, as in:

  strncmp (s, "prefix", 6)

This would be the second case: stopping at NUL before the number of
specified bytes are read.

Thanks,
Florian



More information about the Libc-alpha mailing list