public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
  • [parent not found: <20231108021240.176996-1-mattlloydhouse@gmail.com>]
  • * [PATCH 0/2] Expand BUGS section of string_copying(7).
           [not found] <cfbd8674-fe6a-4430-95f1-ec8bde7da32e@jguk.org>
           [not found] ` <ZUacobMq0l_O8gjg@debian>
    @ 2023-11-12  9:17 ` Alejandro Colomar
      2023-11-12  9:18 ` [PATCH 1/2] string_copying.7: BUGS: *cat(3) functions aren't always bad Alejandro Colomar
                       ` (5 subsequent siblings)
      7 siblings, 0 replies; 77+ messages in thread
    From: Alejandro Colomar @ 2023-11-12  9:17 UTC (permalink / raw)
      To: linux-man; +Cc: Alejandro Colomar, libc-alpha
    
    [-- Attachment #1: Type: text/plain, Size: 462 bytes --]
    
    Hi,
    
    After Paul showing important problems of strlcpy(3) (and strlcat(3)),
    I've written something in string_copying(7)'s BUGS to warn against them.
    
    Cheers,
    Alex
    
    Alejandro Colomar (2):
      string_copying.7: BUGS: *cat(3) functions aren't always bad
      string_copying.7: BUGS: Document strl{cpy,cat}(3)'s performance
        problems
    
     man7/string_copying.7 | 24 +++++++++++++++++++++++-
     1 file changed, 23 insertions(+), 1 deletion(-)
    
    -- 
    2.42.0
    
    
    [-- Attachment #2: signature.asc --]
    [-- Type: application/pgp-signature, Size: 833 bytes --]
    
    ^ permalink raw reply	[flat|nested] 77+ messages in thread
  • * [PATCH 1/2] string_copying.7: BUGS: *cat(3) functions aren't always bad
           [not found] <cfbd8674-fe6a-4430-95f1-ec8bde7da32e@jguk.org>
           [not found] ` <ZUacobMq0l_O8gjg@debian>
      2023-11-12  9:17 ` [PATCH 0/2] Expand BUGS section of string_copying(7) Alejandro Colomar
    @ 2023-11-12  9:18 ` Alejandro Colomar
      2023-11-12  9:18 ` [PATCH 2/2] string_copying.7: BUGS: Document strl{cpy,cat}(3)'s performance problems Alejandro Colomar
                       ` (4 subsequent siblings)
      7 siblings, 0 replies; 77+ messages in thread
    From: Alejandro Colomar @ 2023-11-12  9:18 UTC (permalink / raw)
      To: linux-man
      Cc: Alejandro Colomar, libc-alpha, Paul Eggert, Jonny Grant,
    	DJ Delorie, Matthew House, Oskari Pirhonen, Thorsten Kukuk,
    	Adhemerval Zanella Netto, Zack Weinberg, G. Branden Robinson,
    	Carlos O'Donell, Xi Ruoyao, Stefan Puiu, Andreas Schwab
    
    [-- Attachment #1: Type: text/plain, Size: 1736 bytes --]
    
    The compiler will sometimes optimize them to normal *cpy(3) functions,
    since the length of dst is usually known, if the previous *cpy(3) is
    visible to the compiler.  And they provide for cleaner code.  If you
    know that they'll get optimized, you could use them.
    
    Cc: Paul Eggert <eggert@cs.ucla.edu>
    Cc: Jonny Grant <jg@jguk.org>
    Cc: DJ Delorie <dj@redhat.com>
    Cc: Matthew House <mattlloydhouse@gmail.com>
    Cc: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
    Cc: Thorsten Kukuk <kukuk@suse.com>
    Cc: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
    Cc: Zack Weinberg <zack@owlfolio.org>
    Cc: "G. Branden Robinson" <g.branden.robinson@gmail.com>
    Cc: Carlos O'Donell <carlos@redhat.com>
    Cc: Xi Ruoyao <xry111@xry111.site>
    Cc: Stefan Puiu <stefan.puiu@gmail.com>
    Cc: Andreas Schwab <schwab@linux-m68k.org>
    Signed-off-by: Alejandro Colomar <alx@kernel.org>
    ---
     man7/string_copying.7 | 6 ++++++
     1 file changed, 6 insertions(+)
    
    diff --git a/man7/string_copying.7 b/man7/string_copying.7
    index 1637ebc91..0254fbba6 100644
    --- a/man7/string_copying.7
    +++ b/man7/string_copying.7
    @@ -592,8 +592,14 @@ .SH BUGS
     All catenation functions share the same performance problem:
     .UR https://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/
     Shlemiel the painter
     .UE .
    +As a mitigation,
    +compilers are able to transform some calls to catenation functions
    +into normal copy functions,
    +since
    +.I strlen(dst)
    +is usually a byproduct of the previous copy.
     .\" ----- EXAMPLES :: -------------------------------------------------/
     .SH EXAMPLES
     The following are examples of correct use of each of these functions.
     .\" ----- EXAMPLES :: stpcpy(3) ---------------------------------------/
    -- 
    2.42.0
    
    
    [-- Attachment #2: signature.asc --]
    [-- Type: application/pgp-signature, Size: 833 bytes --]
    
    ^ permalink raw reply	[flat|nested] 77+ messages in thread
  • * [PATCH 2/2] string_copying.7: BUGS: Document strl{cpy,cat}(3)'s performance problems
           [not found] <cfbd8674-fe6a-4430-95f1-ec8bde7da32e@jguk.org>
                       ` (2 preceding siblings ...)
      2023-11-12  9:18 ` [PATCH 1/2] string_copying.7: BUGS: *cat(3) functions aren't always bad Alejandro Colomar
    @ 2023-11-12  9:18 ` Alejandro Colomar
      2023-11-12 11:26 ` [PATCH v2 0/3] Improve string_copying(7) Alejandro Colomar
                       ` (3 subsequent siblings)
      7 siblings, 0 replies; 77+ messages in thread
    From: Alejandro Colomar @ 2023-11-12  9:18 UTC (permalink / raw)
      To: linux-man
      Cc: Alejandro Colomar, libc-alpha, Paul Eggert, Jonny Grant,
    	DJ Delorie, Matthew House, Oskari Pirhonen, Thorsten Kukuk,
    	Adhemerval Zanella Netto, Zack Weinberg, G. Branden Robinson,
    	Carlos O'Donell, Xi Ruoyao, Stefan Puiu, Andreas Schwab
    
    [-- Attachment #1: Type: text/plain, Size: 2593 bytes --]
    
    Also point to BUGS from other sections that talk about these functions.
    
    These functions are doomed due to the design decision of mirroring
    snprintf(3)'s return value.  They must return strlen(src), which makes
    them terribly slow, and vulnerable to DoS if an attacker can control
    strlen(src).
    
    A better design would have been to return -1 when truncating.
    
    Reported-by: Paul Eggert <eggert@cs.ucla.edu>
    Cc: Jonny Grant <jg@jguk.org>
    Cc: DJ Delorie <dj@redhat.com>
    Cc: Matthew House <mattlloydhouse@gmail.com>
    Cc: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
    Cc: Thorsten Kukuk <kukuk@suse.com>
    Cc: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
    Cc: Zack Weinberg <zack@owlfolio.org>
    Cc: "G. Branden Robinson" <g.branden.robinson@gmail.com>
    Cc: Carlos O'Donell <carlos@redhat.com>
    Cc: Xi Ruoyao <xry111@xry111.site>
    Cc: Stefan Puiu <stefan.puiu@gmail.com>
    Cc: Andreas Schwab <schwab@linux-m68k.org>
    Signed-off-by: Alejandro Colomar <alx@kernel.org>
    ---
     man7/string_copying.7 | 18 +++++++++++++++++-
     1 file changed, 17 insertions(+), 1 deletion(-)
    
    diff --git a/man7/string_copying.7 b/man7/string_copying.7
    index 0254fbba6..cb3910db0 100644
    --- a/man7/string_copying.7
    +++ b/man7/string_copying.7
    @@ -226,9 +226,9 @@ .SS Truncate or not?
     .IP \[bu]
     .BR strlcpy (3bsd)
     and
     .BR strlcat (3bsd)
    -are similar, but less efficient when chained.
    +are similar, but have important performance problems; see BUGS.
     .IP \[bu]
     .BR stpncpy (3)
     and
     .BR strncpy (3)
    @@ -417,8 +417,10 @@ .SS Functions
     the resulting string is truncated
     (but it is guaranteed to be null-terminated).
     They return the length of the total string they tried to create.
     .IP
    +Check BUGS before using these functions.
    +.IP
     .BR stpecpy (3)
     is a simpler alternative to these functions.
     .\" ----- DESCRIPTION :: Functions :: stpncpy(3) ----------------------/
     .TP
    @@ -598,8 +600,22 @@ .SH BUGS
     into normal copy functions,
     since
     .I strlen(dst)
     is usually a byproduct of the previous copy.
    +.P
    +.BR strlcpy (3)
    +and
    +.BR strlcat (3)
    +need to read the entire
    +.I src
    +string,
    +even if the destination buffer is small.
    +This makes them vulnerable to Denial of Service (DoS) attacks
    +if an attacker can control the length of the
    +.I src
    +string.
    +And if not,
    +they're still unnecessarily slow.
     .\" ----- EXAMPLES :: -------------------------------------------------/
     .SH EXAMPLES
     The following are examples of correct use of each of these functions.
     .\" ----- EXAMPLES :: stpcpy(3) ---------------------------------------/
    -- 
    2.42.0
    
    
    [-- Attachment #2: signature.asc --]
    [-- Type: application/pgp-signature, Size: 833 bytes --]
    
    ^ permalink raw reply	[flat|nested] 77+ messages in thread
  • * [PATCH v2 0/3] Improve string_copying(7)
           [not found] <cfbd8674-fe6a-4430-95f1-ec8bde7da32e@jguk.org>
                       ` (3 preceding siblings ...)
      2023-11-12  9:18 ` [PATCH 2/2] string_copying.7: BUGS: Document strl{cpy,cat}(3)'s performance problems Alejandro Colomar
    @ 2023-11-12 11:26 ` Alejandro Colomar
      2023-11-12 11:26 ` [PATCH v2 1/3] string_copying.7: BUGS: *cat(3) functions aren't always bad Alejandro Colomar
                       ` (2 subsequent siblings)
      7 siblings, 0 replies; 77+ messages in thread
    From: Alejandro Colomar @ 2023-11-12 11:26 UTC (permalink / raw)
      To: linux-man, Guillem Jover
      Cc: Alejandro Colomar, libc-alpha, Paul Eggert, Jonny Grant,
    	DJ Delorie, Matthew House, Oskari Pirhonen, Thorsten Kukuk,
    	Adhemerval Zanella Netto, Zack Weinberg, G. Branden Robinson,
    	Carlos O'Donell, Xi Ruoyao, Stefan Puiu, Andreas Schwab
    
    [-- Attachment #1: Type: text/plain, Size: 907 bytes --]
    
    
    Hi,
    
    v3:
    
    -  Patches 1/3 and 2/3 are identical to v2, except that I CCd libbsd's
       maintainer (Guillem) in 2/3 so he's aware that we're documenting BUGS
       for strlcpy(3).  Since the strlcpy(3bsd) manual page is part of
       libbsd, it may be interesting to also add a BUGS section in that
       page.
    
    -  Add 3/3, which adds strtcpy(3), a function almost identical to
       strscpy(9), and very similar to strlcpy(3), which doesn't share its
       bugs.
    
    Cheers,
    Alex
    
    Alejandro Colomar (3):
      string_copying.7: BUGS: *cat(3) functions aren't always bad
      string_copying.7: BUGS: Document strl{cpy,cat}(3)'s performance
        problems
      strtcpy.3, string_copying.7: Add strtcpy(3)
    
     man3/strtcpy.3        |   1 +
     man7/string_copying.7 | 121 +++++++++++++++++++++++++++++++-----------
     2 files changed, 92 insertions(+), 30 deletions(-)
     create mode 100644 man3/strtcpy.3
    
    -- 
    2.42.0
    
    
    [-- Attachment #2: signature.asc --]
    [-- Type: application/pgp-signature, Size: 833 bytes --]
    
    ^ permalink raw reply	[flat|nested] 77+ messages in thread
  • * [PATCH v2 1/3] string_copying.7: BUGS: *cat(3) functions aren't always bad
           [not found] <cfbd8674-fe6a-4430-95f1-ec8bde7da32e@jguk.org>
                       ` (4 preceding siblings ...)
      2023-11-12 11:26 ` [PATCH v2 0/3] Improve string_copying(7) Alejandro Colomar
    @ 2023-11-12 11:26 ` Alejandro Colomar
      2023-11-12 11:26 ` [PATCH v2 2/3] string_copying.7: BUGS: Document strl{cpy,cat}(3)'s performance problems Alejandro Colomar
      2023-11-12 11:27 ` [PATCH v2 3/3] strtcpy.3, string_copying.7: Add strtcpy(3) Alejandro Colomar
      7 siblings, 0 replies; 77+ messages in thread
    From: Alejandro Colomar @ 2023-11-12 11:26 UTC (permalink / raw)
      To: linux-man
      Cc: Alejandro Colomar, libc-alpha, Guillem Jover, Paul Eggert,
    	Jonny Grant, DJ Delorie, Matthew House, Oskari Pirhonen,
    	Thorsten Kukuk, Adhemerval Zanella Netto, Zack Weinberg,
    	G. Branden Robinson, Carlos O'Donell, Xi Ruoyao, Stefan Puiu,
    	Andreas Schwab
    
    [-- Attachment #1: Type: text/plain, Size: 1736 bytes --]
    
    The compiler will sometimes optimize them to normal *cpy(3) functions,
    since the length of dst is usually known, if the previous *cpy(3) is
    visible to the compiler.  And they provide for cleaner code.  If you
    know that they'll get optimized, you could use them.
    
    Cc: Paul Eggert <eggert@cs.ucla.edu>
    Cc: Jonny Grant <jg@jguk.org>
    Cc: DJ Delorie <dj@redhat.com>
    Cc: Matthew House <mattlloydhouse@gmail.com>
    Cc: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
    Cc: Thorsten Kukuk <kukuk@suse.com>
    Cc: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
    Cc: Zack Weinberg <zack@owlfolio.org>
    Cc: "G. Branden Robinson" <g.branden.robinson@gmail.com>
    Cc: Carlos O'Donell <carlos@redhat.com>
    Cc: Xi Ruoyao <xry111@xry111.site>
    Cc: Stefan Puiu <stefan.puiu@gmail.com>
    Cc: Andreas Schwab <schwab@linux-m68k.org>
    Signed-off-by: Alejandro Colomar <alx@kernel.org>
    ---
     man7/string_copying.7 | 6 ++++++
     1 file changed, 6 insertions(+)
    
    diff --git a/man7/string_copying.7 b/man7/string_copying.7
    index 1637ebc91..0254fbba6 100644
    --- a/man7/string_copying.7
    +++ b/man7/string_copying.7
    @@ -592,8 +592,14 @@ .SH BUGS
     All catenation functions share the same performance problem:
     .UR https://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/
     Shlemiel the painter
     .UE .
    +As a mitigation,
    +compilers are able to transform some calls to catenation functions
    +into normal copy functions,
    +since
    +.I strlen(dst)
    +is usually a byproduct of the previous copy.
     .\" ----- EXAMPLES :: -------------------------------------------------/
     .SH EXAMPLES
     The following are examples of correct use of each of these functions.
     .\" ----- EXAMPLES :: stpcpy(3) ---------------------------------------/
    -- 
    2.42.0
    
    
    [-- Attachment #2: signature.asc --]
    [-- Type: application/pgp-signature, Size: 833 bytes --]
    
    ^ permalink raw reply	[flat|nested] 77+ messages in thread
  • * [PATCH v2 2/3] string_copying.7: BUGS: Document strl{cpy,cat}(3)'s performance problems
           [not found] <cfbd8674-fe6a-4430-95f1-ec8bde7da32e@jguk.org>
                       ` (5 preceding siblings ...)
      2023-11-12 11:26 ` [PATCH v2 1/3] string_copying.7: BUGS: *cat(3) functions aren't always bad Alejandro Colomar
    @ 2023-11-12 11:26 ` Alejandro Colomar
      2023-11-12 11:27 ` [PATCH v2 3/3] strtcpy.3, string_copying.7: Add strtcpy(3) Alejandro Colomar
      7 siblings, 0 replies; 77+ messages in thread
    From: Alejandro Colomar @ 2023-11-12 11:26 UTC (permalink / raw)
      To: linux-man
      Cc: Alejandro Colomar, libc-alpha, Guillem Jover, Paul Eggert,
    	Jonny Grant, DJ Delorie, Matthew House, Oskari Pirhonen,
    	Thorsten Kukuk, Adhemerval Zanella Netto, Zack Weinberg,
    	G. Branden Robinson, Carlos O'Donell, Xi Ruoyao, Stefan Puiu,
    	Andreas Schwab
    
    [-- Attachment #1: Type: text/plain, Size: 2634 bytes --]
    
    Also point to BUGS from other sections that talk about these functions.
    
    These functions are doomed due to the design decision of mirroring
    snprintf(3)'s return value.  They must return strlen(src), which makes
    them terribly slow, and vulnerable to DoS if an attacker can control
    strlen(src).
    
    A better design would have been to return -1 when truncating.
    
    Reported-by: Paul Eggert <eggert@cs.ucla.edu>
    Cc: Jonny Grant <jg@jguk.org>
    Cc: DJ Delorie <dj@redhat.com>
    Cc: Matthew House <mattlloydhouse@gmail.com>
    Cc: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
    Cc: Thorsten Kukuk <kukuk@suse.com>
    Cc: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
    Cc: Zack Weinberg <zack@owlfolio.org>
    Cc: "G. Branden Robinson" <g.branden.robinson@gmail.com>
    Cc: Carlos O'Donell <carlos@redhat.com>
    Cc: Xi Ruoyao <xry111@xry111.site>
    Cc: Stefan Puiu <stefan.puiu@gmail.com>
    Cc: Andreas Schwab <schwab@linux-m68k.org>
    Cc: Guillem Jover <guillem@hadrons.org>
    Signed-off-by: Alejandro Colomar <alx@kernel.org>
    ---
     man7/string_copying.7 | 18 +++++++++++++++++-
     1 file changed, 17 insertions(+), 1 deletion(-)
    
    diff --git a/man7/string_copying.7 b/man7/string_copying.7
    index 0254fbba6..cb3910db0 100644
    --- a/man7/string_copying.7
    +++ b/man7/string_copying.7
    @@ -226,9 +226,9 @@ .SS Truncate or not?
     .IP \[bu]
     .BR strlcpy (3bsd)
     and
     .BR strlcat (3bsd)
    -are similar, but less efficient when chained.
    +are similar, but have important performance problems; see BUGS.
     .IP \[bu]
     .BR stpncpy (3)
     and
     .BR strncpy (3)
    @@ -417,8 +417,10 @@ .SS Functions
     the resulting string is truncated
     (but it is guaranteed to be null-terminated).
     They return the length of the total string they tried to create.
     .IP
    +Check BUGS before using these functions.
    +.IP
     .BR stpecpy (3)
     is a simpler alternative to these functions.
     .\" ----- DESCRIPTION :: Functions :: stpncpy(3) ----------------------/
     .TP
    @@ -598,8 +600,22 @@ .SH BUGS
     into normal copy functions,
     since
     .I strlen(dst)
     is usually a byproduct of the previous copy.
    +.P
    +.BR strlcpy (3)
    +and
    +.BR strlcat (3)
    +need to read the entire
    +.I src
    +string,
    +even if the destination buffer is small.
    +This makes them vulnerable to Denial of Service (DoS) attacks
    +if an attacker can control the length of the
    +.I src
    +string.
    +And if not,
    +they're still unnecessarily slow.
     .\" ----- EXAMPLES :: -------------------------------------------------/
     .SH EXAMPLES
     The following are examples of correct use of each of these functions.
     .\" ----- EXAMPLES :: stpcpy(3) ---------------------------------------/
    -- 
    2.42.0
    
    
    [-- Attachment #2: signature.asc --]
    [-- Type: application/pgp-signature, Size: 833 bytes --]
    
    ^ permalink raw reply	[flat|nested] 77+ messages in thread
  • * [PATCH v2 3/3] strtcpy.3, string_copying.7: Add strtcpy(3)
           [not found] <cfbd8674-fe6a-4430-95f1-ec8bde7da32e@jguk.org>
                       ` (6 preceding siblings ...)
      2023-11-12 11:26 ` [PATCH v2 2/3] string_copying.7: BUGS: Document strl{cpy,cat}(3)'s performance problems Alejandro Colomar
    @ 2023-11-12 11:27 ` Alejandro Colomar
      7 siblings, 0 replies; 77+ messages in thread
    From: Alejandro Colomar @ 2023-11-12 11:27 UTC (permalink / raw)
      To: linux-man
      Cc: Alejandro Colomar, libc-alpha, Guillem Jover, Paul Eggert,
    	Jonny Grant, DJ Delorie, Matthew House, Oskari Pirhonen,
    	Thorsten Kukuk, Adhemerval Zanella Netto, Zack Weinberg,
    	G. Branden Robinson, Carlos O'Donell, Xi Ruoyao, Stefan Puiu,
    	Andreas Schwab
    
    [-- Attachment #1: Type: text/plain, Size: 7496 bytes --]
    
    Add this new truncating string-copying function.  It intends to fully
    replace strlcpy(3), which has important bugs (documented in the
    preceeding commit).
    
    It is almost identical to Linux kernel's strscpy(9), so reduce the
    documentation of strscpy(9) in this page to the minimum, giving
    preference to strtcpy(3).  Provide a reference implementation, since no
    libc provides it.
    
    Providing an easy, safe, and relatively fast truncating string-copying
    function should prevent users from rolling their own, in which they
    might introduce bugs accidentally.  We already made enough mistakes
    while discussing these functions, so it's certainly not something that
    should be written often.
    
    Cc: Paul Eggert <eggert@cs.ucla.edu>
    Cc: Jonny Grant <jg@jguk.org>
    Cc: DJ Delorie <dj@redhat.com>
    Cc: Matthew House <mattlloydhouse@gmail.com>
    Cc: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
    Cc: Thorsten Kukuk <kukuk@suse.com>
    Cc: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
    Cc: Zack Weinberg <zack@owlfolio.org>
    Cc: "G. Branden Robinson" <g.branden.robinson@gmail.com>
    Cc: Carlos O'Donell <carlos@redhat.com>
    Cc: Xi Ruoyao <xry111@xry111.site>
    Cc: Stefan Puiu <stefan.puiu@gmail.com>
    Cc: Andreas Schwab <schwab@linux-m68k.org>
    Cc: Guillem Jover <guillem@hadrons.org>
    Signed-off-by: Alejandro Colomar <alx@kernel.org>
    ---
     man3/strtcpy.3        |  1 +
     man7/string_copying.7 | 97 ++++++++++++++++++++++++++++++-------------
     2 files changed, 69 insertions(+), 29 deletions(-)
     create mode 100644 man3/strtcpy.3
    
    diff --git a/man3/strtcpy.3 b/man3/strtcpy.3
    new file mode 100644
    index 000000000..beb850746
    --- /dev/null
    +++ b/man3/strtcpy.3
    @@ -0,0 +1 @@
    +.so man7/string_copying.7
    diff --git a/man7/string_copying.7 b/man7/string_copying.7
    index cb3910db0..4f609e480 100644
    --- a/man7/string_copying.7
    +++ b/man7/string_copying.7
    @@ -6,8 +6,9 @@
     .\" ----- NAME :: -----------------------------------------------------/
     .SH NAME
     stpcpy,
     strcpy, strcat,
    +strtcpy,
     stpecpy,
     strlcpy, strlcat,
     stpncpy,
     strncpy,
    @@ -30,8 +31,11 @@ .SS Strings
     // Chain-copy a string with truncation.
     .BI "char *stpecpy(char *" dst ", char " end "[0], const char *restrict " src );
     .P
     // Copy/catenate a string with truncation.
    +.BI "size_t strtcpy(char " dst "[restrict ." sz "], \
    +const char *restrict " src ,
    +.BI "               size_t " sz );
     .BI "size_t strlcpy(char " dst "[restrict ." sz "], \
     const char *restrict " src ,
     .BI "               size_t " sz );
     .BI "size_t strlcat(char " dst "[restrict ." sz "], \
    @@ -220,10 +224,10 @@ .SS Truncate or not?
     .P
     Functions that truncate:
     .IP \[bu] 3
     .BR stpecpy (3)
    -is the most efficient string copy function that performs truncation.
    -It only requires to check for truncation once after all chained calls.
    +.IP \[bu]
    +.BR strtcpy (3)
     .IP \[bu]
     .BR strlcpy (3bsd)
     and
     .BR strlcat (3bsd)
    @@ -326,8 +330,10 @@ .SS String vs character sequence
     .IP \[bu]
     .BR strcpy (3),
     .BR strcat (3)
     .IP \[bu]
    +.BR strtcpy (3)
    +.IP \[bu]
     .BR stpecpy (3)
     .IP \[bu]
     .BR strlcpy (3bsd),
     .BR strlcat (3bsd)
    @@ -390,12 +396,24 @@ .SS Functions
     The return value is useless.
     .IP
     .BR stpcpy (3)
     is a faster alternative to these functions.
    +.\" ----- DESCRIPTION :: Functions :: strtcpy(3) ----------------------/
    +.TP
    +.BR strtcpy (3)
    +Copy the input string into a destination string.
    +If the destination buffer isn't large enough to hold the copy,
    +the resulting string is truncated
    +(but it is guaranteed to be null-terminated).
    +It returns the length of the string,
    +or \-1 if it truncated.
    +.IP
    +This function is not provided by any library;
    +see EXAMPLES for a reference implementation.
     .\" ----- DESCRIPTION :: Functions :: stpecpy(3) ----------------------/
     .TP
     .BR stpecpy (3)
    -Copy the input string into a destination string.
    +Chain-copy the input string into a destination string.
     If the destination buffer,
     limited by a pointer to its end,
     isn't large enough to hold the copy,
     the resulting string is truncated
    @@ -419,10 +437,12 @@ .SS Functions
     They return the length of the total string they tried to create.
     .IP
     Check BUGS before using these functions.
     .IP
    +.BR strtcpy (3)
    +and
     .BR stpecpy (3)
    -is a simpler alternative to these functions.
    +are better alternatives to these functions.
     .\" ----- DESCRIPTION :: Functions :: stpncpy(3) ----------------------/
     .TP
     .BR stpncpy (3)
     Copy the input string into
    @@ -542,8 +562,17 @@ .SH RETURN VALUE
     .BR ustpcpy (3)
     A pointer to one after the last character
     in the destination character sequence.
     .TP
    +.BR strtcpy (3)
    +The length of the string.
    +When truncation occurs, it returns \-1.
    +When
    +.I dsize
    +is
    +.BR 0 ,
    +it also returns \-1.
    +.TP
     .BR strlcpy (3bsd)
     .TQ
     .BR strlcat (3bsd)
     The length of the total string that they tried to create
    @@ -562,25 +591,14 @@ .SH RETURN VALUE
     which is useless.
     .\" ----- NOTES :: strscpy(9) -----------------------------------------/
     .SH NOTES
     The Linux kernel has an internal function for copying strings,
    -which is similar to
    -.BR stpecpy (3),
    -except that it can't be chained:
    -.TP
    -.BR strscpy (9)
    -Copy the input string into a destination string.
    -If the destination buffer,
    -limited by its size,
    -isn't large enough to hold the copy,
    -the resulting string is truncated
    -(but it is guaranteed to be null-terminated).
    -It returns the length of the destination string, or
    +.BR strscpy (9),
    +which is identical to
    +.BR strtcpy (3),
    +except that it returns
     .B \-E2BIG
    -on truncation.
    -.IP
    -.BR stpecpy (3)
    -is a simpler and faster alternative to this function.
    +instead of \-1.
     .\" ----- CAVEATS :: --------------------------------------------------/
     .SH CAVEATS
     Don't mix chain calls to truncating and non-truncating functions.
     It is conceptually wrong
    @@ -640,8 +658,17 @@ .SH EXAMPLES
     strcat(buf, "!");
     len = strlen(buf);
     puts(buf);
     .EE
    +.\" ----- EXAMPLES :: strtcpy(3) --------------------------------------/
    +.TP
    +.BR strtcpy (3)
    +.EX
    +len = strtcpy(buf, "Hello world!", sizeof(buf));
    +if (len == \-1)
    +    goto toolong;
    +puts(buf);
    +.EE
     .\" ----- EXAMPLES :: stpecpy(3) --------------------------------------/
     .TP
     .BR stpecpy (3)
     .EX
    @@ -671,17 +698,8 @@ .SH EXAMPLES
     if (len >= sizeof(buf))
         goto toolong;
     puts(buf);
     .EE
    -.\" ----- EXAMPLES :: strscpy(9) --------------------------------------/
    -.TP
    -.BR strscpy (9)
    -.EX
    -len = strscpy(buf, "Hello world!", sizeof(buf));
    -if (len == \-E2BIG)
    -    goto toolong;
    -puts(buf);
    -.EE
     .\" ----- EXAMPLES :: stpncpy(3) --------------------------------------/
     .TP
     .BR stpncpy (3)
     .EX
    @@ -765,8 +783,29 @@ .SS Implementations
     .in +4n
     .EX
     /* This code is in the public domain. */
     \&
    +.\" ----- EXAMPLES :: Implementations :: strtcpy(3) -------------------/
    +ssize_t
    +.IR strtcpy "(char *restrict dst, const char *restrict src, size_t sz)"
    +{
    +    bool    trunc;
    +    char    *p;
    +    size_t  dlen, slen;
    +\&
    +    if (dsize == 0)
    +        return \-1;
    +\&
    +    slen = strnlen(src, dsize);
    +    trunc = (slen == dsize);
    +    dlen = slen \- trunc;
    +\&
    +    p = mempcpy(dst, src, dlen);
    +    *p = \[aq]\e0\[aq];
    +
    +    return trunc ? \-1 : slen;
    +}
    +\&
     .\" ----- EXAMPLES :: Implementations :: stpecpy(3) -------------------/
     char *
     .IR stpecpy "(char *dst, char end[0], const char *restrict src)"
     {
    -- 
    2.42.0
    
    
    [-- Attachment #2: signature.asc --]
    [-- Type: application/pgp-signature, Size: 833 bytes --]
    
    ^ permalink raw reply	[flat|nested] 77+ messages in thread

  • end of thread, other threads:[~2023-11-27 23:45 UTC | newest]
    
    Thread overview: 77+ messages (download: mbox.gz / follow: Atom feed)
    -- links below jump to the message on this page --
         [not found] <cfbd8674-fe6a-4430-95f1-ec8bde7da32e@jguk.org>
         [not found] ` <ZUacobMq0l_O8gjg@debian>
         [not found]   ` <aeb55af5-1017-4ffd-9824-30b43d5748e3@jguk.org>
         [not found]     ` <ZUgl2HPJvUge7XYN@debian>
         [not found]       ` <d40fffcb-524d-44b6-a252-b55a8ddc9fee@jguk.org>
         [not found]         ` <ZUo6btEFD_z_3NcF@devuan>
         [not found]           ` <929865e3-17b4-49c4-8fa9-8383885e9904@jguk.org>
         [not found]             ` <ZUpjI1AHNOMOjdFk@devuan>
         [not found]               ` <ZUsoIbhrJar6ojux@dj3ntoo>
    2023-11-08  9:51                 ` strncpy clarify result may not be null terminated Alejandro Colomar
    2023-11-08  9:59                   ` Thorsten Kukuk
    2023-11-08 15:09                     ` Alejandro Colomar
         [not found]                     ` <6bcad2492ab843019aa63895beaea2ce@DB6PR04MB3255.eurprd04.prod.outlook.com>
    2023-11-08 15:44                       ` Thorsten Kukuk
    2023-11-08 17:26                         ` Adhemerval Zanella Netto
    2023-11-08 14:06                   ` Zack Weinberg
    2023-11-08 15:07                     ` Alejandro Colomar
    2023-11-08 21:35                       ` Carlos O'Donell
    2023-11-08 22:11                         ` Alejandro Colomar
    2023-11-08 23:31                           ` Paul Eggert
    2023-11-09  0:29                             ` Alejandro Colomar
    2023-11-09 10:13                               ` Jonny Grant
    2023-11-09 11:08                                 ` catenate vs concatenate (was: strncpy clarify result may not be null terminated) Alejandro Colomar
    2023-11-09 14:06                                   ` catenate vs concatenate Jonny Grant
    2023-11-27 14:33                                   ` catenate vs concatenate (was: strncpy clarify result may not be null terminated) Zack Weinberg
    2023-11-27 15:08                                     ` Alejandro Colomar
    2023-11-27 15:13                                       ` Alejandro Colomar
    2023-11-27 16:59                                       ` G. Branden Robinson
    2023-11-27 18:35                                         ` Zack Weinberg
    2023-11-27 23:45                                           ` G. Branden Robinson
    2023-11-09 11:13                                 ` strncpy clarify result may not be null terminated Alejandro Colomar
    2023-11-09 14:05                                   ` Jonny Grant
    2023-11-09 15:04                                     ` Alejandro Colomar
    2023-11-08 19:04                   ` DJ Delorie
    2023-11-08 19:40                     ` Alejandro Colomar
    2023-11-08 19:58                       ` DJ Delorie
    2023-11-08 20:13                         ` Alejandro Colomar
    2023-11-08 21:07                           ` DJ Delorie
    2023-11-08 21:50                             ` Alejandro Colomar
    2023-11-08 22:17                               ` [PATCH] stpncpy.3, string_copying.7: Clarify that st[rp]ncpy() do NOT produce a string Alejandro Colomar
    2023-11-08 23:06                                 ` Paul Eggert
    2023-11-08 23:28                                   ` DJ Delorie
    2023-11-09  0:24                                   ` Alejandro Colomar
    2023-11-09 14:11                                   ` Jonny Grant
    2023-11-09 14:35                                     ` Alejandro Colomar
    2023-11-09 14:47                                       ` Jonny Grant
    2023-11-09 15:02                                         ` Alejandro Colomar
    2023-11-09 17:30                                           ` DJ Delorie
    2023-11-09 17:54                                             ` Andreas Schwab
    2023-11-09 18:00                                             ` Alejandro Colomar
    2023-11-09 19:42                                             ` Jonny Grant
    2023-11-09  7:23                                 ` Oskari Pirhonen
    2023-11-09 15:20                                 ` [PATCH v2 1/2] " Alejandro Colomar
    2023-11-09 15:20                                 ` [PATCH v2 2/2] stpncpy.3, string.3, string_copying.7: Clarify that st[rp]ncpy() pad with null bytes Alejandro Colomar
    2023-11-10  5:47                                   ` Oskari Pirhonen
    2023-11-10 10:47                                     ` Alejandro Colomar
         [not found]           ` <20231108021240.176996-1-mattlloydhouse@gmail.com>
         [not found]             ` <ZUvilH5kuQfTuZjy@debian>
         [not found]               ` <20231109031345.245703-1-mattlloydhouse@gmail.com>
    2023-11-09 10:31                 ` strncpy clarify result may not be null terminated Jonny Grant
    2023-11-09 11:38                   ` Alejandro Colomar
    2023-11-09 12:43                     ` Alejandro Colomar
    2023-11-09 12:51                     ` Xi Ruoyao
    2023-11-09 14:01                       ` Alejandro Colomar
    2023-11-09 18:11                     ` Paul Eggert
    2023-11-09 23:48                       ` Alejandro Colomar
    2023-11-10  5:36                         ` Paul Eggert
    2023-11-10 11:05                           ` Alejandro Colomar
    2023-11-10 11:47                             ` Alejandro Colomar
    2023-11-10 17:58                             ` Paul Eggert
    2023-11-10 18:36                               ` Alejandro Colomar
    2023-11-10 20:19                                 ` Alejandro Colomar
    2023-11-10 23:44                                   ` Jonny Grant
    2023-11-10 19:52                               ` Alejandro Colomar
    2023-11-10 22:14                                 ` Paul Eggert
    2023-11-11 21:13                                   ` Alejandro Colomar
    2023-11-11 22:20                                     ` Paul Eggert
    2023-11-12  9:52                                     ` Jonny Grant
    2023-11-12 10:59                                       ` Alejandro Colomar
    2023-11-10 11:36                           ` Jonny Grant
    2023-11-10 13:15                             ` Alejandro Colomar
    2023-11-10 11:23                     ` Jonny Grant
         [not found]               ` <CACKs7VDsTdSNwbC6+2LtQ67J_eJiD814xkw2_5XM1Q=iMjLXJA@mail.gmail.com>
    2023-11-10 11:06                 ` Jonny Grant
    2023-11-12  9:17 ` [PATCH 0/2] Expand BUGS section of string_copying(7) Alejandro Colomar
    2023-11-12  9:18 ` [PATCH 1/2] string_copying.7: BUGS: *cat(3) functions aren't always bad Alejandro Colomar
    2023-11-12  9:18 ` [PATCH 2/2] string_copying.7: BUGS: Document strl{cpy,cat}(3)'s performance problems Alejandro Colomar
    2023-11-12 11:26 ` [PATCH v2 0/3] Improve string_copying(7) Alejandro Colomar
    2023-11-12 11:26 ` [PATCH v2 1/3] string_copying.7: BUGS: *cat(3) functions aren't always bad Alejandro Colomar
    2023-11-12 11:26 ` [PATCH v2 2/3] string_copying.7: BUGS: Document strl{cpy,cat}(3)'s performance problems Alejandro Colomar
    2023-11-12 11:27 ` [PATCH v2 3/3] strtcpy.3, string_copying.7: Add strtcpy(3) Alejandro Colomar
    

    This is a public inbox, see mirroring instructions
    for how to clone and mirror all data and code used for this inbox;
    as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).