From patchwork Thu Dec 22 21:42:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 62295 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 29DBB385B52C for ; Thu, 22 Dec 2022 21:45:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 29DBB385B52C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671745522; bh=Fq0BwIlQYMS7VUMF+5mYrroz/YQemrjXoNsWBi7kVGk=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=K1JmIpGXc2mYOinBVpssELLKOz1/xV3vCzhICe5DheuvZcbFY+QJurd7qKyijyk+t DM4/WTN16ho6PLIgvYITXvY2JV0bydEgL26b3QgNBbaw7jVTdDUTWobrpNXiOPTM/z UbRt8+n/7uYy3DzS0LsnwmUoykZGUKWZ/XdeSJ9E= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 429EC3850F0F for ; Thu, 22 Dec 2022 21:45:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 429EC3850F0F Received: by mail-wr1-x42c.google.com with SMTP id co23so2948847wrb.4 for ; Thu, 22 Dec 2022 13:45:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Fq0BwIlQYMS7VUMF+5mYrroz/YQemrjXoNsWBi7kVGk=; b=wSYS1s1INgjwcxf27hlfMZfZdX9HEP5d7/qjU/IbM/RrQGWq4zTKv7boyXvaf9ep5V fihip/+CRKsZaWQlEmXBmBjOmeL6CNzu9TFS+vztJhozXzSBbCzejiEBEPugnt8GwKUS zeWjt3oZQY0COKwX/GsvH4slh0zB4NLiPpcvcmvJTPw6mmmB/vMFnJNQLg4RjxMG27cM 9vVpCZItSG2L80A43iscLX1Mn3KblknT7xKDEIgsZz0AfOD+PsQmcT9HhoQFrxb/PAG8 J6t+wNRoQVVcmUy7Z5pj+fBKWB+++HtvQ/glxZW2FMZ8H9CdOdM3kJsg4jYRyxq0rB8p TETA== X-Gm-Message-State: AFqh2koql5ltGIbTV+R6Qqyp42iISSxneDF63nHG940da0fF4gw1Add8 XtwngO5deTYMTgO3diN9ijCmczW6EsA= X-Google-Smtp-Source: AMrXdXth0e2fQect3acFnV6G6a+021QpryYNx11rvlFQs22yGxwZjLtLaHioUCm8Z11AyiS7r5l9oA== X-Received: by 2002:adf:f183:0:b0:255:96ed:950b with SMTP id h3-20020adff183000000b0025596ed950bmr4593525wro.60.1671745499007; Thu, 22 Dec 2022 13:44:59 -0800 (PST) Received: from asus5775.alejandro-colomar.es ([170.253.36.171]) by smtp.googlemail.com with ESMTPSA id j10-20020adff54a000000b002420d51e581sm1501295wrp.67.2022.12.22.13.44.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 13:44:58 -0800 (PST) X-Google-Original-From: Alejandro Colomar To: libc-alpha@sourceware.org Cc: Alejandro Colomar Subject: [PATCH 1/1] string: Add stpecpy(3) Date: Thu, 22 Dec 2022 22:42:19 +0100 Message-Id: <20221222214217.1619716-2-alx@kernel.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20221222214217.1619716-1-alx@kernel.org> References: <20221222214217.1619716-1-alx@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3557; i=alx@kernel.org; h=from:subject; bh=ahNVoJaC8IUoB680e8FoI515BLCH3j7FAfax6MfDNwU=; b=owEBbQKS/ZANAwAKAZ6MGvu+/9syAcsmYgBjpM84fdt5o5CH8KWh7kxG/14aa3uwSgc8XBOZel7P m2o2CiuJAjMEAAEKAB0WIQTqOofwpOugMORd8kCejBr7vv/bMgUCY6TPOAAKCRCejBr7vv/bMtW8D/ 9reJF4pBhkBWaSZbsowQ7lINsjfdK/QWDkVquSLlQPixD5bZ6nWCFGpVOVs3UZaR2+uLXldasJWhtH BgSUg4iAQQRVqfB5iuIVEId3PvXVOTWoOEUU2HXm+IfGZGmkOkL6uuy/JX8LrrohizdtJ8D6wvCS6Q btuOST7a1kyPg/GgSQC4Wciecxgsu/9T9hcFCSqD2VNPNhI198vdA4zUFvw1PwZDjISNBa/G7GLEwi sEujrEeU1Phvf2MylxoNRx8FFjxT/x1oVUJojz3EekpJ0gGVa7/eR2LTO7j1KO8cPKCuQrqNvvq/Hu Gj2SZvUvcT4kFjWpodgeyVTkVJJulbwEqZGRL0fWV5ZyHVorO11Px2UQ+lvwzK/e1egUlY51UsK3zV E1AkdgXChqJeOuBaLUW65rRzhKfY5idhBMaFZKrRFH1wZaHDfu4GgZPd0F7lIwD1GC70z4VNR6ohcR xi9hhITKBUDUrcgK6+oTRuYL8TWwHQ8PkYx77qSp2WNCKrFUtlCO0Tqo/W6S8VEv0Wx77wrC4C+uE9 lCfUZ1sc0ui8xnwgdF2dySTIAt4shnwr5EQJjZBCAV8c379j0s8li7P9JVb31swhSC3G8h3AdRQqRb EEvsdY72qkqsqzLyeEV5KKq/3/eFuxQOEdeuGHfbkS0Z5K0MM/Wn+H3AKjxA== X-Developer-Key: i=alx@kernel.org; a=openpgp; fpr=A9348594CE31283A826FBDD8D57633D441E25BB5 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, 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 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-Patchwork-Original-From: Alejandro Colomar via Libc-alpha From: Alejandro Colomar Reply-To: Alejandro Colomar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Glibc didn't provide any function that copies a string with truncation. It only provided strncpy(3) and stpncpy(3), which copy from a string into a null-padded character sequence at the destination fixed-width buffer, with truncation. Those old functions, which don't produce a string, have been misused for a long time as a poor-man's replacement for strlcpy(3), but doing that is a source of bugs, since it's hard to calculate the right size that should be passed to the function, and it's also necessary to explicitly terminate the buffer with a null byte. Detecting truncation is yet another problem. stpecpy(3), described in the new string_copying(7) manual page, is similar to OpenBSD's strlcpy(3)/strlcat(3), but: - It's simpler to implement. - It's faster. - It's simpler to detect truncation. Signed-off-by: Alejandro Colomar --- Of course this is still a very early patch. I just compiled it, but we'd need to write tests for it. I didn't want to do all of that work before the discussion. Since the source code has been copied from libstp, I can at least say that the function works, since I already used that library, but this still needs a lot of work to adapt to glibc, I guess. string/Makefile | 1 + string/stpecpy.c | 39 +++++++++++++++++++++++++++++++++++++++ string/string.h | 7 +++++++ 3 files changed, 47 insertions(+) create mode 100644 string/stpecpy.c diff --git a/string/Makefile b/string/Makefile index 938f528b8d..95e9ebce6d 100644 --- a/string/Makefile +++ b/string/Makefile @@ -73,6 +73,7 @@ routines := \ sigabbrev_np \ sigdescr_np \ stpcpy \ + stpecpy \ stpncpy \ strcasecmp \ strcasecmp_l \ diff --git a/string/stpecpy.c b/string/stpecpy.c new file mode 100644 index 0000000000..e6194559ee --- /dev/null +++ b/string/stpecpy.c @@ -0,0 +1,39 @@ +/* Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3.0 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +char * +stpecpy (char *dest, char *end, const char *restrict src) +{ + char *p; + + if (dest == end) + return end; + if (dest == NULL) // Allow chaining with stpeprintf(3). + return NULL; + if (dest > end) + __builtin_unreachable(); + + p = memccpy(dest, src, '\0', end - dest); + if (p != NULL) + return p - 1; + + /* Truncation detected. */ + end[-1] = '\0'; + return end; +} diff --git a/string/string.h b/string/string.h index 54dd8344de..966a8cb744 100644 --- a/string/string.h +++ b/string/string.h @@ -502,6 +502,13 @@ extern char *stpncpy (char *__restrict __dest, #endif #ifdef __USE_GNU +/* Copy the string SRC into a null-terminated string at DEST, + truncating if it would run after END. Return a pointer to + the terminating null byte, or END if the string was truncated, + or NULL if DEST was NULL. */ +extern char *stpecpy (char *__dest, char *__end, const char *__restrict __src) + __THROW __nonnull ((2, 3)); + /* Compare S1 and S2 as strings holding name & indices/version numbers. */ extern int strverscmp (const char *__s1, const char *__s2) __THROW __attribute_pure__ __nonnull ((1, 2)); From patchwork Wed Dec 28 23:17:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 62471 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B8A293857C51 for ; Wed, 28 Dec 2022 23:19:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B8A293857C51 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1672269553; bh=K8hzbh+cDnNzEDI6/zdf0g7DmHNEQmK9sguAkmhCpmA=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=ikZ9bHLI9FLoOJre2kquiTcz4wZqlfBhqFuvKxajC+XlSAMVoL66MgdgutqwVXRfy OunkHGYum0VBZScc7odWEte9prFmYO6jA9F8S82vLA1sC6AA02wl31La+697qMnprR C9CYGYIJyLEX1zRBAEfnlylyNMQC9Zq8igpbge7Q= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by sourceware.org (Postfix) with ESMTPS id C3608385840A for ; Wed, 28 Dec 2022 23:18:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C3608385840A Received: by mail-wm1-x334.google.com with SMTP id i17-20020a05600c355100b003d99434b1cfso1966644wmq.1 for ; Wed, 28 Dec 2022 15:18:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=K8hzbh+cDnNzEDI6/zdf0g7DmHNEQmK9sguAkmhCpmA=; b=VgJnJNkYYxkKt3xaGY8v4mB4KemPRjw7jVeSYvDGtTdd0o+qTHopUvPIeydSTlJMro jyCCiWbwHt0U+YNPe5fFh9nP6QvO9rXMdEMTvHzRx4Um0QPaoFnie06CPQlehalbn48z lU5STwkG0+3/7W6eHSsMZFgElftgUg00KYpNiIhN9yhGqofFQGUfb3O+/A/MCgQA1akj QqSF+pitGwj480Nw20WC9lExMSbOqfj2xh8UShKCcPGDWhKnuqgkQsSVdz8XSyVaoOOg 20eQivTUkePix4obAGq3mlCophf2CzLbd0qApJEsXGmJVR6FJkpTYpfXDOeu+iuy/hie u2+A== X-Gm-Message-State: AFqh2kpxz1KELeKxOH9qGgeRxA+Z7gMfVojPZfJcl2E4wA7AHKDRRYVz nqROgQjGEda6LLY5c0hpLSsOGPNVVB8= X-Google-Smtp-Source: AMrXdXt6/FpQbxU1x48I87BiYkOLZ+ExdOu599WghY7rvEHfw2s5Ymeqr/ZLc2nj7gMaEOr1voTImg== X-Received: by 2002:a05:600c:4255:b0:3d3:3d34:5d63 with SMTP id r21-20020a05600c425500b003d33d345d63mr19256505wmm.8.1672269491659; Wed, 28 Dec 2022 15:18:11 -0800 (PST) Received: from asus5775.alejandro-colomar.es ([170.253.36.171]) by smtp.googlemail.com with ESMTPSA id l27-20020a05600c1d1b00b003cfd4e6400csm24357367wms.19.2022.12.28.15.18.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Dec 2022 15:18:11 -0800 (PST) X-Google-Original-From: Alejandro Colomar To: libc-alpha@sourceware.org Cc: Alejandro Colomar Subject: [PATCH v2 2/3] stdio: Add vstpeprintf() Date: Thu, 29 Dec 2022 00:17:41 +0100 Message-Id: <20221228231741.125945-3-alx@kernel.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20221222214217.1619716-1-alx@kernel.org> References: <20221222214217.1619716-1-alx@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4288; i=alx@kernel.org; h=from:subject; bh=1AmAQxPfsfy0iDyVR3Xerp/tpdqPWSyXKi3+nlkN/Ps=; b=owEBbQKS/ZANAwAKAZ6MGvu+/9syAcsmYgBjrM6NZg1SRTiuY9slvWzfSyKAUbdYm+bMn9NEEENz f2Bmq0CJAjMEAAEKAB0WIQTqOofwpOugMORd8kCejBr7vv/bMgUCY6zOjQAKCRCejBr7vv/bMqdgD/ 4vwhdjrIy7iWcrMPNeM7X60gK7DrYV7XMg5se5McNb4nZ+KTpWKssOkrdFhZ9F8mc8ammSz4jc9XXS J8PWQe1sHoAv8UKSZ7W48SiPhq5AFhHD1Nz0k1l9icgjh9jgvhISMR1fA/oBugyGVfGM4UHCLk5For kXWHW+nUpPGapXRhLUIQ/WV1etpSg+ORwaxJvIMktEwErmBRBkvW41LgModn8Z7J7GqznZ9z0ctnrX +4MzCvBlMr/e82NiflcvgmAMLbLFKMWVSO0X2RuX+yJrpj346EQSDyUr5x0Lmgry3w82e3E0ZBL8Si TGv5zK2nwtYXznutPSxz3VlqosIKq7kmJUgkq175J/n0MRlTeUmnEuWc8ssLRRo4pzCqg8aW6lc2En BZ/jjDT5hw5c+vu5xM1fuD5AFe+Sn3GPL+oZFfSwrIC9/wqTcYRHyedtr0ebFpPtLFqUObX1LveheH RKW1kpx24Jx3p7kEYBgtYPFGpjD3WuYkrcrXEBx9sE1MdDHDXzHf4GHiMUBvs7EtxNtBx5WKUEUEet B5y9s1RMFW/twWhCdalTZwKEll4dY2d153kQ0iNL+dttiImjGl7eEOfXy8y8DHzGFbmz3Mzs+9v3Aj BhAI9roUi9kZW15Xj3Bt9N0+9vRw2YPz9lQDMp3ZiKQDYj0LzMywB1Eca65g== X-Developer-Key: i=alx@kernel.org; a=openpgp; fpr=A9348594CE31283A826FBDD8D57633D441E25BB5 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, 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 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-Patchwork-Original-From: Alejandro Colomar via Libc-alpha From: Alejandro Colomar Reply-To: Alejandro Colomar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" [v]snprintf(3) is error-prone, since it doesn't allow easy catenation or chaining. To catenate a formatted string after an existing string, the only possibility was to call [v]snprintf(3), adjusting the sizes and pointers. However, that is very error-prone, and has caused several bugs in existing software. I found several just in a small investigation in some noteworthy open-source projects. This API solves that problem by receiving a pointer to the end of the destination buffer, so there's no recalculation involved. It also always returns a pointer suitable for chaining with other calls to this function, or calls to stpecpy(3). Signed-off-by: Alejandro Colomar --- libio/Makefile | 4 ++-- libio/stdio.h | 6 ++++++ libio/vstpeprintf.c | 52 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 libio/vstpeprintf.c diff --git a/libio/Makefile b/libio/Makefile index 64398ab1ee..1924f7a65c 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -43,8 +43,8 @@ routines := \ \ clearerr feof ferror fileno fputc freopen fseek getc getchar \ memstream pclose putc putchar rewind setbuf setlinebuf vasprintf \ - iovdprintf vscanf vsnprintf obprintf fcloseall fseeko ftello \ - freopen64 fseeko64 ftello64 \ + iovdprintf vscanf vsnprintf vstpeprintf obprintf fcloseall \ + fseeko ftello freopen64 fseeko64 ftello64 \ \ __fbufsize __freading __fwriting __freadable __fwritable __flbf \ __fpurge __fpending __fsetlocking \ diff --git a/libio/stdio.h b/libio/stdio.h index 0e0f16b464..59b8047ecc 100644 --- a/libio/stdio.h +++ b/libio/stdio.h @@ -384,6 +384,12 @@ extern int vsnprintf (char *__restrict __s, size_t __maxlen, __THROWNL __attribute__ ((__format__ (__printf__, 3, 0))); #endif +#if __USE_GNU +extern char *vstpeprintf (char *__dest, char *__end, + const char *__restrict __fmt, __gnuc_va_list __arg) + __THROWNL __attribute__ ((__format__ (__printf__, 3, 0))); +#endif + #if __GLIBC_USE (LIB_EXT2) /* Write formatted output to a string dynamically allocated with `malloc'. Store the address of the string in *PTR. */ diff --git a/libio/vstpeprintf.c b/libio/vstpeprintf.c new file mode 100644 index 0000000000..a8becdc682 --- /dev/null +++ b/libio/vstpeprintf.c @@ -0,0 +1,52 @@ +/* Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . + + As a special exception, if you link the code in this file with + files compiled with a GNU compiler to produce an executable, + that does not cause the resulting executable to be covered by + the GNU Lesser General Public License. This exception does not + however invalidate any other reasons why the executable file + might be covered by the GNU Lesser General Public License. + This exception applies to code released by its copyright holders + in files containing the exception. */ + +#include +#include + + +char * +vstpeprintf(char *dst, char *end, const char *restrict fmt, va_list ap) +{ + int dsize, len; + + if (dst == end) + return end; + if (dst == NULL) + return NULL; + if (dst > end) + __builtin_unreachable(); + + dsize = end - dst; + len = __vsnprintf_internal(dst, dsize, fmt, ap, 0); + + if (len == -1) + return NULL; + if (len >= dsize) + return end; + + return dst + len; +} From patchwork Wed Dec 28 23:17:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 62472 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 87D863858C66 for ; Wed, 28 Dec 2022 23:19:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87D863858C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1672269558; bh=DoZtCDOkrM6EHl1iFqQN+zASRMnaJqWoyBzRsyneBuY=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=yR+Umln029FTM/NIO6ZcQXqhKKzSxtb9RJjp5lulu9agbhxZR4Eh6SAkDRZpuS6to U7GC2klyRfEVrGB/dydvQMBgGw3bjvBjPvhYr9sq1JzRISKJNPscgEFm3tXUnu94xq gwdOGAUEQv/kGJa6MQvlJQhBD2EmbekWalfvUwco= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 8C6DA385840E for ; Wed, 28 Dec 2022 23:18:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8C6DA385840E Received: by mail-wm1-x329.google.com with SMTP id g25-20020a7bc4d9000000b003d97c8d4941so6611462wmk.4 for ; Wed, 28 Dec 2022 15:18:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DoZtCDOkrM6EHl1iFqQN+zASRMnaJqWoyBzRsyneBuY=; b=sZXxw9+bAR/g5eCvMZidYsxe6B4fhcya+plgBGNmoZriTtwlmPphwqHbe1fBbNs7Co j13/xh+CuDeFtNVP9e93v6JnFd2J5e85ihIvjsbDEL7xyB+geVgM3CdzINdt4y5cYcVO veJRKvkddAvAM9QEkrC5jOREneVb7nq4TXpJ5A1FpO3b2m26JDUd7jfd1H3/dZvZVFYs NkyhwVvCmGmWQOiWkYOGbXWaRiWeWWNXPFSdoaGMoSh3cTglib6b7L3rKMgjlbTq/3X9 HyH1+JyV+MIFd8MwBiBrTOZRiSR8yB69cWhdapeEWXup73XYIg2ChK5fCuLhKcM5t1U+ Ycpw== X-Gm-Message-State: AFqh2krsQDYrdZmqj4eLrKqC+daQBvCiv5e1FK8LJ3sQ9Fps5qfOZwf1 u0EnKHZMcxx6nop27RcniqPRrsAVyP8= X-Google-Smtp-Source: AMrXdXuTE6GO6n3hjCvth8C9M0s82A4Q1KBGH/biD42Fk1fgSIO8TMx/97UIvqXI7U704/ahAYcDTQ== X-Received: by 2002:a05:600c:5022:b0:3d3:5893:f4f8 with SMTP id n34-20020a05600c502200b003d35893f4f8mr18528044wmr.23.1672269492343; Wed, 28 Dec 2022 15:18:12 -0800 (PST) Received: from asus5775.alejandro-colomar.es ([170.253.36.171]) by smtp.googlemail.com with ESMTPSA id l27-20020a05600c1d1b00b003cfd4e6400csm24357367wms.19.2022.12.28.15.18.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Dec 2022 15:18:12 -0800 (PST) X-Google-Original-From: Alejandro Colomar To: libc-alpha@sourceware.org Cc: Alejandro Colomar Subject: [PATCH v2 3/3] stdio: Add stpeprintf() Date: Thu, 29 Dec 2022 00:17:42 +0100 Message-Id: <20221228231741.125945-4-alx@kernel.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20221222214217.1619716-1-alx@kernel.org> References: <20221222214217.1619716-1-alx@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2474; i=alx@kernel.org; h=from:subject; bh=jRfHgxMuuV9d9q5jrJ21qiWg0ydbJD3ABOsvXrNYPjo=; b=owEBbQKS/ZANAwAKAZ6MGvu+/9syAcsmYgBjrM6N+DPBQjRj0CoGlNT1Zws8wAZ5MBp0pbjRr0cp vkmOD3CJAjMEAAEKAB0WIQTqOofwpOugMORd8kCejBr7vv/bMgUCY6zOjQAKCRCejBr7vv/bMqp1D/ 4/9FPcrfaZAq8WRFnupmj6YdvtQ19NzJLK0U56RFH6W+wMvxDBRE54OxpNzHB4n5NVw+i2tXdMD/QL a7O71e161DzJ8n6Er7kzSD6ZtfjXPmNDfKEdvAhYUHiH5l1mNOcT+q32vXlHKT/bkrutARJv98nws4 D0+Q6DeEkxQxwTHc6Nv6ipiRpB8DRSKDnboZhdSPU9tLtLR26smkAEcsC4BTFmhwhCawzSHRcOiXSr U+11OVoDZSuE76p1mX4xgVqPhRPeQvDtWtyfsGnT0N4ptomSXcTDJDp2Kb1m8w68U5Ecs2n9QCMA/z x5FxpSH4j/tNEwFWLqLzgfhnOAVJq6BdyiV/AMkc8rEkGF5aovdEdCbEwoB3QEpKk/cuPz4Dn572nL a0TEFVsc5w7pXOIpc4ZmFq56M0qaT3Ky2tsDtpNGCtggtV0HJcAduXJ+QqPSG9jpz/vZ7jd4z5z8yE YmmXKD/2mGJvwZWu1ayhADVCM3SPBx3UdB/JPHrPO16yz8S9tFU8NNS6UEQOo94CostGAqCWnUxFrd p4I1/+JZMjYlqZfOLc7jKTYrg/hlMzv1TM6Dzoontuj8pBjRbwYoOKv1GZ1Vcfc0Gq6NFptRgu7YwS MaYJfeec+J5Z8+12qAmCQJZbw22+ibOVsUidiT59VCxkHtOUEPhzm1+GBsgw== X-Developer-Key: i=alx@kernel.org; a=openpgp; fpr=A9348594CE31283A826FBDD8D57633D441E25BB5 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, 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 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-Patchwork-Original-From: Alejandro Colomar via Libc-alpha From: Alejandro Colomar Reply-To: Alejandro Colomar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Add variadic-argument version of vstpeprintf(3). Signed-off-by: Alejandro Colomar --- libio/stdio.h | 4 ++++ stdio-common/Makefile | 1 + stdio-common/stpeprintf.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 stdio-common/stpeprintf.c diff --git a/libio/stdio.h b/libio/stdio.h index 59b8047ecc..7456ea6885 100644 --- a/libio/stdio.h +++ b/libio/stdio.h @@ -385,6 +385,10 @@ extern int vsnprintf (char *__restrict __s, size_t __maxlen, #endif #if __USE_GNU +extern char *stpeprintf (char *__dest, char *__end, + const char *__restrict __fmt, ...) + __THROWNL __attribute__ ((__format__ (__printf__, 3, 4))); + extern char *vstpeprintf (char *__dest, char *__end, const char *__restrict __fmt, __gnuc_va_list __arg) __THROWNL __attribute__ ((__format__ (__printf__, 3, 0))); diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 3e0c574ca5..d92942a86f 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -79,6 +79,7 @@ routines := \ snprintf \ sprintf \ sscanf \ + stpeprintf \ tempnam \ tempname \ tmpfile \ diff --git a/stdio-common/stpeprintf.c b/stdio-common/stpeprintf.c new file mode 100644 index 0000000000..7b953be3ee --- /dev/null +++ b/stdio-common/stpeprintf.c @@ -0,0 +1,32 @@ +/* Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +char * +stpeprintf(char *dst, char *end, const char *restrict fmt, ...) +{ + char *p; + va_list ap; + + va_start(ap, fmt); + p = vstpeprintf(dst, end, fmt, ap); + va_end(ap); + + return p; +}