From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate.crashing.org (gate.crashing.org [63.228.1.57]) by sourceware.org (Postfix) with ESMTP id 4D0513858018 for ; Thu, 8 Jul 2021 23:50:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4D0513858018 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=kernel.crashing.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=kernel.crashing.org Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 168NnVok028725; Thu, 8 Jul 2021 18:49:31 -0500 Received: (from segher@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id 168NnTDO028724; Thu, 8 Jul 2021 18:49:29 -0500 X-Authentication-Warning: gate.crashing.org: segher set sender to segher@kernel.crashing.org using -f Date: Thu, 8 Jul 2021 18:49:29 -0500 From: Segher Boessenkool To: "Alejandro Colomar (man-pages)" Cc: Jonny Grant , gcc-help@gcc.gnu.org, linux-man , Florian Weimer , Michael Kerrisk Subject: Re: strlen Message-ID: <20210708234929.GU1583@gate.crashing.org> References: <87363whf2z.fsf@mid.deneb.enyo.de> <48e874cb-2b95-2783-ddfc-ae12d9aaf8f5@jguk.org> <87bl7fozxi.fsf@mid.deneb.enyo.de> <23679a28-5986-0af2-4d98-7de68ed0deec@jguk.org> <53b3666b-d200-ef97-b050-cc38669c61cd@gmail.com> <564825ed-1e1f-b344-da35-1b83c551ed5f@jguk.org> <5566b180-1333-d73b-22ee-6c6d32053921@jguk.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i X-Spam-Status: No, score=-6.1 required=5.0 tests=BAYES_00, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, TXREP, T_SPF_HELO_PERMERROR, T_SPF_PERMERROR autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 08 Jul 2021 23:50:36 -0000 On Thu, Jul 08, 2021 at 01:06:17PM +0200, Alejandro Colomar (man-pages) via Gcc-help wrote: > On 7/8/21 12:07 PM, Jonny Grant wrote: > >We can't guarantee safestrlen() won't be called with NULL. So because > >strlen() itself doesn't check for NULL in C standard we'd need to call the > >wrapper so that NULL can be checked for. > >size_t __attribute__((optimize("O0"))) safestrlen(const char * s) > >{ > > if (NULL == s) return 0; > > else return strlen(s); > >} > That also allows differentiating a length of 0 (i.e., "") from an > invalid string (i.e., NULL), by returning -1 for NULL. It is incorrect to return any particular value for strlen(0); not 0, not -1, not anything. Since there *is* no string, it doesn't have a length either. So instead of making some function for this, I recommend just writing something like bla = s ? strlen(s) : 0; wherever you need it. If a function name isn't self-explanatory, and even *cannot* be, your factoring is most likely not ideal. Code is primarily there for humans to read, it should be optimised for that. Segher