From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id BBCF53855025 for ; Sun, 27 Jun 2021 19:46:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BBCF53855025 Received: by mail-wm1-x32e.google.com with SMTP id j11-20020a05600c1c0bb02901e23d4c0977so11504201wms.0 for ; Sun, 27 Jun 2021 12:46:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=DAErNgS/j8DCnY2ERck8XX1Z4inkZWw1VYk+KrVuhjw=; b=E3C5R4RIdTlYWVolqvWN/0d+WFba84nJYlxvZcsB6hAys5aV8X0IVrtiGkSXNs+iHE bbx1tTXQxAWHe0YeD6qjGkHN3MhMJ+AAsOTjHyHE5Hh0a0DkRCBhldpM4ngLwNLsI1qk YX8z8rHCcCCzTMm63D8gWrhjNJiQgUYJT+3RAmqcY1S4ozYQiXDEJ/hQIooqk/CbSOpV qjE0FbZwehtWyauTocA2ilT/aM55gLi1Vv2/kq88aqmCyppNfMrNIHvWbli2dyqbz13r f5J69WkUUTEO1yZ11dsuDKmu0xBhNYL8uXecS+Hnku9nSQNuNLQ1m46qcNH4uTnwm4q/ CUIw== X-Gm-Message-State: AOAM530mX4Bivq5pNkMfjXWL9vxP0jOAmgxM0YRMlWjISwADg54qFZTb yYaEO3V/gmzZNftuRclIkXamTSP0jCY= X-Google-Smtp-Source: ABdhPJxRN2p0uVsep9uFsS1xMSmExZeXxLSkk0jSME26BDXwBp/th5Be0d6L+/yjmnRb05ilkTMgjQ== X-Received: by 2002:a05:600c:a45:: with SMTP id c5mr23077064wmq.153.1624823200906; Sun, 27 Jun 2021 12:46:40 -0700 (PDT) Received: from [10.8.0.150] ([195.53.121.100]) by smtp.gmail.com with ESMTPSA id g17sm7466428wrw.31.2021.06.27.12.46.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 27 Jun 2021 12:46:40 -0700 (PDT) Subject: Re: [RFC] strcpys(): New function for copying strings safely From: "Alejandro Colomar (man-pages)" To: glibc Cc: tech@openbsd.org, Christoph Hellwig , "linux-kernel@vger.kernel.org" References: <755875ec-baae-6cab-52a8-3c9530db1ce6@gmail.com> Message-ID: <38428d5e-ead2-bf18-e198-cecd4caeb3e7@gmail.com> Date: Sun, 27 Jun 2021 21:46:38 +0200 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: <755875ec-baae-6cab-52a8-3c9530db1ce6@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 27 Jun 2021 19:46:43 -0000 On 6/27/21 9:26 PM, Alejandro Colomar (man-pages) wrote: > > It is designed so that usage requires the minimum number of lines of > code for complete usage (including error handling checks): > > [[ > // When we already checked that 'size' is >= 1 > // and truncation is not an issue: > > strcpys_np(size, dest, src, NULL); Also, given how unlikely this case is, I have in my code: `[[gnu::warn_unused_result]]` I forgot to talk about it in the definition I sent. I would put that attribute in the glibc definition, if this is added to glibc. To ignore it, a simple cast of the result to `(void)` should be enough (or a more complex macro, like `UNUSED(strcpys_np(...));`). > > [[ > > #include > #include > > > [[gnu::nonnull]] > ssize_t strscpy_np(ssize_t size, >                    char dest[static restrict size], >                    const char src[static restrict size]) > { >     ssize_t len; > >     if (size <= 0) >         return -1; > >     len = strnlen(src, size - 1); >     memcpy(dest, src, len); >     dest[len] = '\0'; > >     return len; > } > > [[gnu::nonnull(2, 3)]] [[gnu::warn_unused_result]] > int strcpys_np(ssize_t size, >                char dest[static restrict size], >                const char src[static restrict size], >                ssize_t *restrict len) > { >     ssize_t l; > >     l = strscpy_np(size, dest, src); >     if (len) >         *len = l; > >     if (l == -1) >         return -1; >     if (l >= size) >         return 1; >     return 0; > } > > ]] -- Alejandro Colomar Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/ http://www.alejandro-colomar.es/