From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id DC220384842D for ; Fri, 9 Jul 2021 13:54:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DC220384842D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=jguk.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=jguk.org Received: by mail-wm1-x329.google.com with SMTP id t14-20020a05600c198eb029020c8aac53d4so25662472wmq.1 for ; Fri, 09 Jul 2021 06:54:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jguk.org; s=google; h=from:subject:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=90fhIyoBAEzc4pAHffwze8Sz0S5WG73c2BpX8ZqqvKw=; b=lNeZVKXxq7e1PidXS3vU4E0PvKLT3X1DlhhXyWMBcGEz13QSlR+eOCJVUx6kXYsxlW lGFpDP2jdqe3m4UXBfAkRd75EuCj0C/s1g/sz75pCqBz/+UzanC7jxKsKbmFqngkjrUO OaPIPitIWrxXUKaPKyrhqH0YVtWWCfvBirflwGWckiGWES8fN3h2FpnwIwrtdY8qceMa MZkaercUuIrwYQFknFSHfHlMc4lMxB/JDP73iAd/fCghu9iDiHmkZSRv75G9tqy052LG ggQN5uU0FqYXgVb+wGpvStgTGLGwS4lLPuuK+3aU6bFlRs66GPS1Q5C3MfllGdbG/2Ub n93A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=90fhIyoBAEzc4pAHffwze8Sz0S5WG73c2BpX8ZqqvKw=; b=RYbKb0FFVHLtP8wslpfo6zYYuReJHxWexqVdj2CFdMG0Lk9yvFh/EEoCG06uBuGo8Z 0jumvXTRyIWcAMFtHIpsDh7B91qAJ3HzZDQBIWAj9R1hSq66BKL6xUKD84ZCisYGZl4P GhTM1D3OImwISEbMkQU4SXl0nb6Uj1MtiY/CUYS0sRjHUyd5p89l2m4sse7oMSiLfYAa UloUX3ITWoC0A3zTSZjDVPPPF374Ib0Bh4TT9veW04IDQRFmYo6GAu8LVZCWnvPGpckm JCdu60qIJg78tKXUocl7DjLCD7ZZ2zP+9+7OisPKuElI5JRflf2yOea8t3g+U2hMyN0C jLhw== X-Gm-Message-State: AOAM532TtMT/Y9CO+q2iUJDBcZy+rbh3KUEHI8kbTv2yEkciqIVtT6ko LYTyocIO565McQzeaFmuWWJOUw== X-Google-Smtp-Source: ABdhPJzW5E4z9pRbCvA+PhoQcbD8syt+qqTo03NYeIdvAYXBq5Cm8mTOU27l33epCT/nBbvBUmeopg== X-Received: by 2002:a05:600c:a01:: with SMTP id z1mr12061602wmp.77.1625838845789; Fri, 09 Jul 2021 06:54:05 -0700 (PDT) Received: from [192.168.0.12] (cpc87345-slou4-2-0-cust172.17-4.cable.virginm.net. [81.101.252.173]) by smtp.gmail.com with ESMTPSA id z12sm5195889wrs.39.2021.07.09.06.54.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 09 Jul 2021 06:54:05 -0700 (PDT) From: Jonny Grant Subject: Re: strlen To: Segher Boessenkool , "Alejandro Colomar (man-pages)" Cc: gcc-help@gcc.gnu.org, linux-man , Florian Weimer , Michael Kerrisk 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> <20210708234929.GU1583@gate.crashing.org> Message-ID: <3ddd0291-04d2-b35a-ce81-034bb0d9392a@jguk.org> Date: Fri, 9 Jul 2021 14:54:04 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210708234929.GU1583@gate.crashing.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham 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: Fri, 09 Jul 2021 13:54:08 -0000 On 09/07/2021 00:49, Segher Boessenkool wrote: > 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; Hi Segher Yes, this could work. But it does rely on programmer typing it like that every time... Maybe an inline function better. inline size_t safestrlen(const char * s) {return s?strlen(s) : 0} Perhaps there are too many email addresses on this cc list now. I'd prefer a Annex K of C11 style function ISO/IEC TR 24731-1 for strlen() - but there isn't one such as strnlen_s. > > 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 > . Good point Jonny