|
|
Message-ID: <20230716174945.qc6234b654k5eebx@gen2.localdomain>
Date: Sun, 16 Jul 2023 23:49:45 +0600
From: NRK <nrk@...root.org>
To: musl@...ts.openwall.com
Subject: Re: strcmp() guarantees and assumptions
Hi Robert,
> Or to phrase it differently, is the following a legal implementation of
> strcmp()?
>
> int strcmp(char *a, char *b) {
> size_t la = strlen(a), lb = strlen(b);
>
> if (la != lb)
> return ((la > lb) - (lb > la));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I don't see how this can ever be a valid strcmp implementation. The
return value of the comparison functions must be about the first
mismatching byte, not about the string lengths.
| The sign of a nonzero value returned by the comparison functions is
| determined by the sign of the difference between the values of the
| first pair of characters that differ in the objects being compared.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ref: https://port70.net/~nsz/c/c11/n1570.html#7.24.4p1
> Or is it generally agreed upon that libc implementations support
> strcmp() calls on unterminated strings?
memchr (since C11) has the following requirement:
| The implementation shall behave as if it reads the characters
| sequentially and stops as soon as a matching character is found.
I don't believe any such requirement exists for strcmp, so unless
someone proves otherwise, I'd say it's fair game for libc to assume that
the strings are nul-terminated.
Moreover strcmp's description states the following:
| The strcmp function compares the string pointed to by s1 to the string pointed to by s2.
^^^^^^ ^^^^^^
And "string" according to the C standard is always nul-terminated.
- NRK
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.