From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.5 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3DD17C07E96 for ; Thu, 8 Jul 2021 23:53:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1D9E361377 for ; Thu, 8 Jul 2021 23:53:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229606AbhGHX4Z (ORCPT ); Thu, 8 Jul 2021 19:56:25 -0400 Received: from gate.crashing.org ([63.228.1.57]:36454 "EHLO gate.crashing.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229491AbhGHX4Y (ORCPT ); Thu, 8 Jul 2021 19:56:24 -0400 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 Precedence: bulk List-ID: X-Mailing-List: linux-man@vger.kernel.org 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