From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 10132 invoked from network); 16 Jul 2023 17:50:01 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 16 Jul 2023 17:50:01 -0000 Received: (qmail 7611 invoked by uid 550); 16 Jul 2023 17:49:58 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 7579 invoked from network); 16 Jul 2023 17:49:58 -0000 X-Virus-Scanned: SPAM Filter at disroot.org Date: Sun, 16 Jul 2023 23:49:45 +0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1689529785; bh=QAGOvipy6QB0eM+anGo+CFlIZKAf1s4S4LEdMelK2vM=; h=Date:From:To:Subject:References:In-Reply-To; b=TxtYx6nQsBG4z2sGM6KNKvGZUzi/gJJ7Ph8SolfOwUx2rbsVJPjuLNfRW4PoEy2bj 9KDTkfgRV6TB8kyxnpNBQpWyhgHtm+/JKpfQfxgYxlvPMuxEDCdm1IRBRzZ+pgRd0F VG3aQ5pSikUV/7ns1lNqd/T1gpv76VhkBo3npGujj0Mh5SDI3hE+27OdTZu9l1sybO FP/P84eP040nknQEIGRcic4+tqZvkMFWE/xSxv78rgtfzBkfVCz47+4rEiOYepmXfU gubB5PuzLBx4N+1QHhLP+yNESTzNubxJCxvk5V5KtOqRUV6/toKZhw9OjHyaPrzwRM +LboahJ2N+j3g== From: NRK To: musl@lists.openwall.com Message-ID: <20230716174945.qc6234b654k5eebx@gen2.localdomain> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Subject: Re: [musl] 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