From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id 520073858D1E for ; Thu, 22 Dec 2022 21:42:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 520073858D1E 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-wr1-x42e.google.com with SMTP id h16so2909616wrz.12 for ; Thu, 22 Dec 2022 13:42:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=FhwP30KBLZJt4iAdt45IHimf0NzvJgDLrb69Ln1J55A=; b=k6FwjFb/YPhblLslsxIfMPAz7yrhtDW1nDujJDAbDWjd8iP8sVJCSV6kRA+aO/Io0P Uv2ixIr/LO+08SVBMs8fQr5JsRyT8Hq/hpR7KFTNtFPTVPtXhnS+Tbzd9uFenPjFLku6 V14sM8eOdwXeeGSWMK5NfkupLEDMa341Y0LHm1eVzJsplusSZ9wL/0ydHQOKas+SNRde BsaHduxoz4Jtf0Ow/+t3jlp8jxOLVjiuJldMJa6FSC1C/jz81Q2mx0myD6Qqil6EJzBf EmRkofmpD5fiOhso6r/hMTkjlooPgs6FNh4Hq6Sk3Mr12gXdErDyfNrVX3MqOr/9//N+ toMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FhwP30KBLZJt4iAdt45IHimf0NzvJgDLrb69Ln1J55A=; b=gt2NF/Y8jsJ4LI6Q3XQe3Ia09TFl1PG4EvcWMT5WG3z5bDygP8BZbJ1XkhNbu3AIfo XCKgUC3oCyutuuM045n12l0T1HlBGgWlsskoSuLLvbeY9WNcJMaoHzdZ1+7AB+rZOj6T axuHaypa8vtuxpRKHfS/rDnQipm+M8nLqmp3oIfLo3Y5+k3qIPPzFTUeo2Fsdq7dABXM y1nQw5bparlXN9yH/DILbIBPrZewEZ4LvUmmTE0JjQMogzwIzm/1vMD9+LIkTqSFhHGY 7J2tSRwA7WDLbpvUgE5hpsH/jnrwh+FPEFmTVi4NH+bIgGHOgb75w3F0ye4XJ1QsAftX whKA== X-Gm-Message-State: AFqh2kpwroudBh00g0KVzyux6ctGPxUdcTCC2JHJx3FZHo4gqC+q3DBF R54Zt42Jk1fW7BV/YXYv9K0yXFzaTiM= X-Google-Smtp-Source: AMrXdXvNWbTGUyoFCwa7ucyUz6GazEqPjKwQEhiIx32TUJDddCZVl9Q9Fovz+xpyVBXjrxrHyv5dyQ== X-Received: by 2002:a05:6000:799:b0:272:e9b2:1fad with SMTP id bu25-20020a056000079900b00272e9b21fadmr613226wrb.33.1671745347885; Thu, 22 Dec 2022 13:42:27 -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.42.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 13:42:27 -0800 (PST) From: Alejandro Colomar X-Google-Original-From: Alejandro Colomar To: libc-alpha@sourceware.org Cc: Alejandro Colomar Subject: [PATCH 0/1] string: Add stpecpy(3) Date: Thu, 22 Dec 2022 22:42:17 +0100 Message-Id: <20221222214217.1619716-1-alx@kernel.org> X-Mailer: git-send-email 2.39.0 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6253; i=alx@kernel.org; h=from:subject; bh=c2KH5GpaPCLNZUW5Ykl3R8kfRJvKilaPJUzVsQJBK3I=; b=owEBbQKS/ZANAwAKAZ6MGvu+/9syAcsmYgBjpM84dXCwgs1T284oAeFu+FneR2VzniVHBaJhde0H lhLZDsqJAjMEAAEKAB0WIQTqOofwpOugMORd8kCejBr7vv/bMgUCY6TPOAAKCRCejBr7vv/bMjryEA CeGEOn9ULItCfBbZx8G5qxD/0sot0eSPcxVPyCwA3Y3QvEOFdxTm6n/19lQqyR0kohzFo1F5UR71Dl /0sFkHd45gdNbnwBIDdD52d+5VKfaBKFZTulcmNXcPqJhlLkl5XCXHDQ5BDgaw/cio2MwbuYre79uw vy8QWmzt7srJBR2BU5OHmNy9lV97LslGgXrlJeNYLGmyADXX7UFzzp6utd+fY1PVipGYwf8SVARP+W 3nUAM2jkFfmxPlSGbkEeVkhjRHTfWD6UdWyODz5JzZ7AILEeAw1nJiWhjrXyKrvNebDH4uZMxZOvqP Pxkc0lvGInrNpp/doelnw+oJ7ldwYFRPYdHP8pWadXjv44qvp/5MaeIjXCPl4M4PTX+bh4oriQPGdl /l0E2GteFqYWL0/jIi2n6h/gfBUEUtgD8T3hFnIsJP3ipseB66YPecS08i7ZvVhvv2H4eZTWCjmLo0 DJq5Y/RWr2MG0So3X3yc0b7gYC4RXGvAmF8S+mJPJhGgaFs9wIJszTqXXwj5gFFuSv7eoiYypO3Yk0 128Lvgfs4o+njpR5MwphCpQ1UrNQPLFIcyBoQlOhh3c30m28TQa+5IJinN5vxA86XIqF765Joh6ZML sYnN5Li3F6rBJQuVet5K9Jxuj5Nsh+fMAMmKlVyhEa8AarysGvrgKuZXb4ew== X-Developer-Key: i=alx@kernel.org; a=openpgp; fpr=A9348594CE31283A826FBDD8D57633D441E25BB5 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00,BODY_8BITS,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: Hi! I recently rewrite the Linux man-pages for string-copying functions, and just a moment ago, I released the new version. There are some important gaps in the string-copying library, and they should be addressed, however it is done, but ignoring it won't solve the problem. I know this has been suggested in the past (I did once), and has never progressed, but I'll try to justify it as much as I can. The gaps are: - No function for copying strings with truncation. (strlcpy(3) or strscpy(9)) - No function for catenating strings with truncation. (strlcat(3)) - No function for chain-copying strings with truncation. (stpecpy(3)) This is similar to strcpy(3)/strcat(3)/stpcpy(3), where stpcpy(3) is faster and more versatile than the other two, but it's also slightly more complex to use (only slightly). We wouldn't need to add the 3, but at least stpecpy(3) or both strlcpy(3) and strlcat(3). Since stpecpy(3) is significantly faster than the other two, I suggest at least adding stpecpy(3). Also, strlcpy(3)/cat(3) can be more easily implemented in terms of stpecpy(3). There are a few other gaps, but they are much less important, since there are relatively good workarounds. I don't want to overload the discussion either, so I prefer first adding the most necessary function, and only after that deciding if we want to support other string-copying functions. I had added this function this week to a new string library that I was writing, so I already had a manual page written for it. I'll copy it below, to document all the details of the API.o Cheers, Alex --- Alejandro Colomar (1): string: Add stpecpy(3) string/Makefile | 1 + string/stpecpy.c | 39 +++++++++++++++++++++++++++++++++++++++ string/string.h | 7 +++++++ 3 files changed, 47 insertions(+) create mode 100644 string/stpecpy.c stpecpy(3) Library Functions Manual stpecpy(3) NAME stpecpy, stpecpyx - copy a string with truncation LIBRARY Stp string library (libstp, pkgconf ‐‐cflags ‐‐libs libstp) SYNOPSIS #include char *_Nullable stpecpy(char *_Nullable dst, char end[0], const char *restrict src); char *_Nullable stpecpyx(char *_Nullable dst, char end[0], const char *restrict src); DESCRIPTION These functions copy the string pointer to by src, into a string at the buffer pointer to by dst. If the destination buffer, limited by a pointer to its end —one after its last element—, isn’t large enough to hold the copy, the resulting string is truncated. stpecpyx(3) forces a SIGSEGV if the input is not a string, by travers‐ ing it entirely. These functions can be chained with calls to stpeprintf(3) and vstpeprintf(3). An implementation of these functions might be /* This code is in the public domain. */ char * stpecpy(char *dst, char end[0], const char *restrict src) { char *p; if (dst == end || dst == NULL) return dst; p = memccpy(dst, src, '\0', end - dst); if (p != NULL) return p - 1; /* truncation detected */ end[-1] = '\0'; return end; } char * stpecpyx(char *dst, char end[0], const char *restrict src) { if (src[strlen(src)] != '\0') raise(SIGSEGV); return stpecpy(dst, end, src); } RETURN VALUE NULL If dst was NULL. end • If this call truncated. • If dst was equal to end (a previous call to these functions truncated). dst + strlen(dst) On success, these functions return a pointer to the terminating null byte. ATTRIBUTES For an explanation of the terms used in this section, see attrib‐ utes(7). ┌────────────────────────────────────────────┬───────────────┬─────────┐ │Interface │ Attribute │ Value │ ├────────────────────────────────────────────┼───────────────┼─────────┤ │stpecpy(3), stpecpyx(3) │ Thread safety │ MT‐Safe │ └────────────────────────────────────────────┴───────────────┴─────────┘ STANDARDS None. EXAMPLES $ cc ./stpecpy.c $(pkgconf --cflags --libs libbsd-overlay libstp) $ ./a.out [len = 12]: Hello world! $ // stpecpy.c #include #include #include #include #include int main(void) { char *p, *end; char buf[BUFSIZ]; size_t len; end = buf + BUFSIZ; p = buf; p = stpecpy(p, end, "Hello, "); p = stpeprintf(p, end, "%d worlds", 22); p = stpecpy(p, end, "!"); if (p == NULL) err(EXIT_FAILURE, "stpeprintf()"); if (p == end) { p--; warnx("Truncated"); } len = p - buf; printf("[len = %zu]: ", len); puts(buf); exit(EXIT_SUCCESS); } SEE ALSO stpeprintf(3), string_copying(7) libstp (unreleased) (date) stpecpy(3) -- 2.39.0