Skip to content

Conversation

@alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Aug 13, 2025

Queued after #1148

Revisions:


v1b
  • Rebase
$ git rd 
1:  34bbd688 = 1:  b70d59b7 lib/string/strcpy/: strtcat(), STRTCAT(): Add APIs
2:  79541620 = 2:  1b15cf5d lib/salt.c: gensalt(): Use STRTCPY() instead of its pattern
v2
  • Reimplement in terms of stpecpy(). This is much simpler, and thus safer. It also allows us using size_t instead of ssize_t in the input, which is more common (it was weird to need ssize_t in the input, just to avoid compiler diangostics).
$ git range-diff shadow/master gh/strtcat strtcat 
1:  b70d59b7 ! 1:  3f2d3a8e lib/string/strcpy/: strtcat(), STRTCAT(): Add APIs
    @@ lib/string/strcpy/strtcat.c (new)
     +
     +#include "string/strcpy/strtcat.h"
     +
    -+#include <sys/types.h>
    ++#include <stddef.h>
     +
     +
     +extern inline ssize_t strtcat(char *restrict dst, const char *restrict src,
    -+    ssize_t dsize);
    ++    size_t dsize);
     
      ## lib/string/strcpy/strtcat.h (new) ##
     @@
    @@ lib/string/strcpy/strtcat.h (new)
     +
     +#include "config.h"
     +
    -+#include <string.h>
    -+#include <sys/types.h>
    ++#include <stddef.h>
     +
     +#include "attr.h"
     +#include "sizeof.h"
    -+#include "string/strcpy/strtcpy.h"
    ++#include "string/strchr/strnul.h"
    ++#include "string/strcpy/stpecpy.h"
     +
     +
     +#define STRTCAT(dst, src)  strtcat(dst, src, countof(dst))
    @@ lib/string/strcpy/strtcat.h (new)
     +
     +ATTR_STRING(2)
     +inline ssize_t strtcat(char *restrict dst, const char *restrict src,
    -+    ssize_t dsize);
    ++    size_t dsize);
     +
     +
     +// string truncate catenate
     +// Returns new length, or -1 on truncation.
     +inline ssize_t
    -+strtcat(char *restrict dst, const char *restrict src, ssize_t dsize)
    ++strtcat(char *restrict dst, const char *restrict src, size_t dsize)
     +{
    -+  ssize_t  oldlen, n;
    ++  char  *p, *end;
     +
    -+  oldlen = strlen(dst);
    ++  end = dst + dsize;
     +
    -+  n = strtcpy(dst + oldlen, src, dsize - oldlen);
    -+  if (n == -1)
    ++  p = stpecpy(strnul(dst), end, src);
    ++  if (p == NULL)
     +          return -1;
     +
    -+  return oldlen + n;
    ++  return p - dst;
     +}
     +
     +
2:  1b15cf5d = 2:  ae41236f lib/salt.c: gensalt(): Use STRTCPY() instead of its pattern
v2b
  • Update lib/string/README.
$ git range-diff shadow/master gh/strtcat strtcat 
1:  3f2d3a8e ! 1:  8fd61b28 lib/string/strcpy/: strtcat(), STRTCAT(): Add APIs
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
        string/strcpy/strtcpy.h \
        string/strdup/strndupa.c \
     
    + ## lib/string/README ##
    +@@ lib/string/README: strcpy/ - String copying
    +     STRTCPY()
    +   Like strtcpy(), but takes an array.
    + 
    +-    strtcat()  // Unimplemented
    ++    strtcat()
    +   Catenate a string after an existing string, with truncation.
    +   Rarely useful.  Consider using stpecpy() instead.
    +-    STRTCAT()  // Unimplemented
    ++    STRTCAT()
    +   Like strtcat(), but takes an array.
    + 
    +     stpecpy()
    +
      ## lib/string/strcpy/strtcat.c (new) ##
     @@
     +// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
2:  ae41236f = 2:  72e56458 lib/salt.c: gensalt(): Use STRTCPY() instead of its pattern
v2c
  • Rebase
$ git rd 
1:  8fd61b28 = 1:  f229c84c lib/string/strcpy/: strtcat(), STRTCAT(): Add APIs
2:  72e56458 = 2:  7700f0fc lib/salt.c: gensalt(): Use STRTCPY() instead of its pattern
v2d
  • Rebase
$ git rd 
1:  f229c84c ! 1:  e25872f9 lib/string/strcpy/: strtcat(), STRTCAT(): Add APIs
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
     +  string/strcpy/strtcat.h \
        string/strcpy/strtcpy.c \
        string/strcpy/strtcpy.h \
    -   string/strdup/strndupa.c \
    +   string/strdup/strdup.c \
     
      ## lib/string/README ##
     @@ lib/string/README: strcpy/ - String copying
2:  7700f0fc = 2:  bf414636 lib/salt.c: gensalt(): Use STRTCPY() instead of its pattern
v3
  • s/STRTCAT/strtcat_a/
$ git rd 
1:  e25872f90 ! 1:  01e32cebf lib/string/strcpy/: strtcat(), STRTCAT(): Add APIs
    @@ Metadata
     Author: Alejandro Colomar <[email protected]>
     
      ## Commit message ##
    -    lib/string/strcpy/: strtcat(), STRTCAT(): Add APIs
    +    lib/string/strcpy/: strtcat[_a](): Add APIs
     
         Signed-off-by: Alejandro Colomar <[email protected]>
     
    @@ lib/string/README: strcpy/ - String copying
        Catenate a string after an existing string, with truncation.
        Rarely useful.  Consider using stpecpy() instead.
     -    STRTCAT()  // Unimplemented
    -+    STRTCAT()
    ++    strtcat_a()
        Like strtcat(), but takes an array.
      
          stpecpy()
    @@ lib/string/strcpy/strtcat.h (new)
     +#include "string/strcpy/stpecpy.h"
     +
     +
    -+#define STRTCAT(dst, src)  strtcat(dst, src, countof(dst))
    ++// strtcat_a - string truncate catenate array
    ++#define strtcat_a(dst, src)  strtcat(dst, src, countof(dst))
     +
     +
     +ATTR_STRING(2)
    @@ lib/string/strcpy/strtcat.h (new)
     +    size_t dsize);
     +
     +
    -+// string truncate catenate
    ++// strtcat - string truncate catenate
     +// Returns new length, or -1 on truncation.
     +inline ssize_t
     +strtcat(char *restrict dst, const char *restrict src, size_t dsize)
2:  bf4146369 ! 2:  b1906dffc lib/salt.c: gensalt(): Use STRTCPY() instead of its pattern
    @@ lib/salt.c: static /*@observer@*/const char *gensalt (size_t salt_size)
     -  /* Concatenate a pseudo random salt. */
     -  strncat (result, gensalt (salt_len),
     -           GENSALT_SETTING_SIZE - strlen (result) - 1);
    -+  assert(STRTCAT(result, gensalt(salt_len)) != -1);
    ++  assert(strtcat_a(result, gensalt(salt_len)) != -1);
      
        return result;
      #endif /* USE_XCRYPT_GENSALT */

@alejandro-colomar alejandro-colomar changed the title Add strtcat(), STRTCAT(), and use it instead of its pattern Add strtcat[_a](), and use it instead of its pattern Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant