From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id CA1123858D32 for ; Mon, 26 Dec 2022 02:43:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CA1123858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x535.google.com with SMTP id m19so14186969edj.8 for ; Sun, 25 Dec 2022 18:43:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=2Qd1CJ6NKK3vZ5Ffq/BVvO2XPZTV9hZyEcrSmT5tC7A=; b=bYslD6Ts2i/61Fq0OecqEkMPpvyp2y2ZSQFjY+FhIOyG90agjsunPI62VrEjBSLRHd NjbSI7MpXj5K+gT7mNf11qhB1fFawaa7fk/Y2Ggu1wfdZNG8NkzKod/K6B82aOJiWehl G83FxQrFTgGgZtwx1Z6y4yqSwQlfjFetfmgRZmVYneeOv86pp8J91zR0G1WTh5OOpMOy z+5hc3OyK1KpEVy5izzS/tHgTIhIXMUDbFfXLBjYhi628JWvSrQwTq5puo7xnfvM9aa5 FW9V4dArUkgSwryWY38QJfc4y2/eBnFp0qdTOmQ+nCeqQStqMKSSaJI3mSukGn7uMXA9 Tndg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2Qd1CJ6NKK3vZ5Ffq/BVvO2XPZTV9hZyEcrSmT5tC7A=; b=PbJc2BDILt/7QiflX4kTO6hKJxJQXyp7X+zHR2WMlzJKKOa7W8ov8FMCbUDpzJ7RUw 7TtbzjxIgFoT03sKCdLsBKagwxMGZLzbBSmZalMT7dpkhp6Sxt3kQooIxWEnpmiQ/cR/ 7JIPeCDeo0CiDYOnvcg8nzJzzkm/2+br5dEa6aywBf4mwOXupv/dRLqLpmpNs0PbpUGW XKX/hXBOuZh9/cCnZelIkssfd5rXhvT8B0dF5//sfyJzOYMFo7tZkvM9oLJUdGWloUP8 c5rExppzpNKwajBleYgGbLT1NWaWhi7u6BMtfOp1YZMuI0rk8wJdJJF7A22D04PNDJoU m9RQ== X-Gm-Message-State: AFqh2kpT4sc3UUwlXoRsnJpdZ+B3JF4FHFVOm36XK70Pjd0YooPBiqH8 F2KjYxt6dMOe9N0Z/tlq+V5yG0dVy+uS5ea8uMo= X-Google-Smtp-Source: AMrXdXsRYqpZYRzP16BXy51bJOus0klzecy4swbD9dnfy+7m9pohzufKyH+Sz4gveXVEVJO0F7hen7/sX0mcsDCBpro= X-Received: by 2002:a05:6402:545:b0:46f:e856:881c with SMTP id i5-20020a056402054500b0046fe856881cmr1654602edx.258.1672022631821; Sun, 25 Dec 2022 18:43:51 -0800 (PST) MIME-Version: 1.0 References: <91d159d1-d379-af67-6859-bf8e3fa14c72@gmail.com> <703b1fca-27ae-d1c7-863b-0573296fdb43@gmail.com> <6d7d708e-3255-ab54-0d11-e922c260189c@gmail.com> <058d4eeb-ed4e-577b-26c2-60855e45248a@gmail.com> In-Reply-To: <058d4eeb-ed4e-577b-26c2-60855e45248a@gmail.com> From: Noah Goldstein Date: Sun, 25 Dec 2022 18:43:40 -0800 Message-ID: Subject: Re: [PATCH 1/1] string: Add stpecpy(3) To: Alejandro Colomar Cc: Wilco Dijkstra , GNU C Library Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Sun, Dec 25, 2022 at 4:37 PM Alejandro Colomar wrote: > > On 12/26/22 01:32, Noah Goldstein wrote: > >>> You could probably optimize out one of the branches along the line of: > >>> if((dst - 1UL) >= (end - 1UL)) { > >>> // if dst == NULL, then dst - 1UL -> SIZE_MAX and must be >= any value. > >> > >> You would need a cast, wouldn't you? Otherwise, you'll get pointer arithmetic. > >> Pointer arithmetic with NULL is UB. > >> > >>> // if dst == end, then (dst - 1UL) >= (end - 1UL) will be true. > >>> return NULL; > >> > >> Returning NULL on truncation would be a possibility, but then we'd need to use > >> errno to tell the user if the error was truncation or an input NULL (which > >> reports an error to a previous vsnprintf(3) call wrapped by [v]stpeprintf(). > > > > I'm not sure I see what you mean. Your current logic is: > > ``` > > if (dst == end) > > return NULL; > > if (dst == NULL) > > return NULL; > > No; current code is: > > if (dst == end) > return end; > if (dst == NULL) > return NULL; I see. The code you commented earlier was NULL for both. Either way you can just make it: ``` if((dst - 1UL) >= (end - 1UL)) { return dst; // either dst == NULL or dst == end. } ``` > > NULL is an error (contents of string are undefined; per vsnprintf(3)'s spec), > while 'end' is just truncation, and contents if the string are well defined. > > > > ``` > > Equivalent (since dst >= end || dst == NULL is required) is: > > ``` > > if((dst - 1UL) >= (end - 1UL)) { > > return NULL; > > } > > ``` > > May need to be cast to a `uintptr` or something but don't see > > what you mean about needing to check errno and such. > > > >> > >> Using errno would probably counter any optimization, since you'd still need one > >> more branch for setting errno, so I guess it's simpler to just use end for > >> truncation. > >> > >> > >> Oooor, if we reimplement __vsnprintf_internal(3) to work on size_t and never > >> fail, then we could add a [v]stpeprintf(3) that never fails, and then this > >> function would only bail out on truncation. > >> > >> Would it be possible to make __vsnprintf_internal() never fail? What are the > >> current failing conditions; only a size greater than INT_MAX, or are there more > >> errors? > > > > Don't think its worth reimplementing __vsnprintf_internal to save a single > > branch here. > > It wouldn't be only for that, but also allowing to write size_t bytes of > formatted output. However, I question how useful that is, since you only need > that many bytes when you're catenating strings with %s, for which stpecpy(3) can > be used; so yes, probably it's not worth it. > > -- >