mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] strcmp() guarantees and assumptions
@ 2023-07-16 17:22 Robert Clausecker
  2023-07-16 17:49 ` NRK
  0 siblings, 1 reply; 8+ messages in thread
From: Robert Clausecker @ 2023-07-16 17:22 UTC (permalink / raw)
  To: musl; +Cc: mjg

Greetings,

I am currently developing SIMD-enhanced implementations of libc
functions for the FreeBSD libc.  One of the next functions I want to
tackle is strcmp().  There, the following question obtains:

    Is strcmp() permitted to assume that its arguments are NUL
    terminated strings?

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));

    	return memcmp(a, b, la);
    }

A situation I dimly recall where this assumption did not hold was in a
program that used strcmp() to compare two buffers known to have a
mismatch somewhere, but without guaranteed NUL termination.  A naïve
strcmp() implementation processed this just fine, but this one might
crash.

I have previously asked the ISO/IEC 9899:2023 editor [1] who indicated
that he believes my interpretation to be correct, but asked me to look
for a second opinion.

Assuming that my assumption on strcmp() is correct, is this an
assumption common libc implementations make?  Or is it generally
agreed upon that libc implementations support strcmp() calls on
unterminated strings?

Thank you for your help.

Yours,
Robert Clausecker

[1]: https://twitter.com/__phantomderp/status/1680614038567354370

-- 
()  ascii ribbon campaign - for an 8-bit clean world 
/\  - against html email  - against proprietary attachments

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-07-17 16:22 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-16 17:22 [musl] strcmp() guarantees and assumptions Robert Clausecker
2023-07-16 17:49 ` NRK
2023-07-16 17:59   ` Robert Clausecker
2023-07-16 19:24     ` Pedro Falcato
2023-07-16 19:29       ` Pedro Falcato
2023-07-16 19:33     ` Markus Wichmann
2023-07-16 21:13       ` Robert Clausecker
2023-07-17 16:22         ` Adhemerval Zanella Netto

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).