Skip to content

Conversation

@alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Dec 2, 2024

Constify the input, and move code out of the loop.

This introduces strprefix(), a string-handling API that I'll use soon for other purposes. (Done.)


Revisions:

v2
  • Add const correctness. Now strprefix() is a const-generic macro that will return a pointer that is const-qualified iff the input string is const-qualified.
$ git range-diff master gh/rmdir_leading rmdir_leading 
1:  6bc6e17b ! 1:  68279715 lib/string/strcmp/: strprefix(): Add function
    @@ Metadata
     Author: Alejandro Colomar <[email protected]>
     
      ## Commit message ##
    -    lib/string/strcmp/: strprefix(): Add function
    +    lib/string/strcmp/: strprefix(): Add API
     
         Signed-off-by: Alejandro Colomar <[email protected]>
     
    @@ lib/string/strcmp/strprefix.c (new)
     +#include "string/strcmp/strprefix.h"
     +
     +
    -+extern inline char *strprefix(const char *s, const char *prefix);
    ++extern inline const char *strprefix_(const char *s, const char *prefix);
     
      ## lib/string/strcmp/strprefix.h (new) ##
     @@
    @@ lib/string/strcmp/strprefix.h (new)
     +#include <string.h>
     +
     +#include "attr.h"
    ++#include "cast.h"
    ++
    ++
    ++#define strprefix(s, prefix)                                          \
    ++(                                                                     \
    ++  _Generic(s,                                                   \
    ++          const char *:               strprefix_(s, p),         \
    ++          const void *:               strprefix_(s, p),         \
    ++          char *:  const_cast(char *, strprefix_(s, p)),        \
    ++          void *:  const_cast(char *, strprefix_(s, p))         \
    ++  )                                                             \
    ++)
     +
     +
     +ATTR_STRING(1)
     +ATTR_STRING(2)
    -+inline char *strprefix(const char *s, const char *prefix);
    ++inline const char *strprefix_(const char *s, const char *prefix);
     +
     +
     +/*
     + * Return NULL if s does not start with prefix.
     + * Return `s + strlen(prefix)` if s starts with prefix.
     + */
    -+inline char *
    -+strprefix(const char *s, const char *prefix)
    ++inline const char *
    ++strprefix_(const char *s, const char *prefix)
     +{
     +  if (strncmp(s, prefix, strlen(prefix)) != 0)
     +          return NULL;
     +
    -+  return (char *) s + strlen(prefix);
    ++  return s + strlen(prefix);
     +}
     +
     +
2:  5c5b69fd = 2:  8ba0d74a lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  293831c2 = 3:  4d073b01 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v2b
  • Simplify intermediate diff.
  • wsfix
$ git range-diff master gh/rmdir_leading rmdir_leading 
1:  68279715 = 1:  68279715 lib/string/strcmp/: strprefix(): Add API
2:  8ba0d74a ! 2:  e3d3332c lib/tcbfuncs.c: rmdir_leading(): Constify input
    @@ lib/tcbfuncs.c: static shadowtcb_status unlink_suffs (const char *user)
     +
        while ((ind = strrchr (path, '/'))) {
                stpcpy(ind, "");
    --          if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
    +           if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
     -                  OUT_OF_MEMORY;
     -                  return SHADOWTCB_FAILURE;
    --          }
    -+          if (asprintf(&dir, TCB_DIR "/%s", path) == -1)
     +                  goto free_path;
    +           }
     +
                if (rmdir (dir) != 0) {
                        if (errno != ENOTEMPTY) {
3:  4d073b01 ! 3:  d6259969 lib/tcbfuncs.c: rmdir_leading(): Create string just once
    @@ lib/tcbfuncs.c: static shadowtcb_status unlink_suffs (const char *user)
     -  while ((ind = strrchr (path, '/'))) {
     +  while ((ind = strrchr(p, '/'))) {
                stpcpy(ind, "");
    --          if (asprintf(&dir, TCB_DIR "/%s", path) == -1)
    +-          if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
     -                  goto free_path;
    +-          }
      
     -          if (rmdir (dir) != 0) {
     +          if (rmdir(path) != 0) {
    @@ lib/tcbfuncs.c: static shadowtcb_status unlink_suffs (const char *user)
                                fprintf (shadow_logfd,
                                         _("%s: Cannot remove directory %s: %s\n"),
     -                                   shadow_progname, dir, strerror (errno));
    -+                                   shadow_progname, path, strerror (errno));
    ++                                   shadow_progname, path, strerror(errno));
                                ret = SHADOWTCB_FAILURE;
                        }
     -                  free (dir);
v3
  • tfix
$ git range-diff master gh/rmdir_leading rmdir_leading 
1:  68279715 ! 1:  f8deaed9 lib/string/strcmp/: strprefix(): Add API
    @@ lib/string/strcmp/strprefix.h (new)
     +#define strprefix(s, prefix)                                          \
     +(                                                                     \
     +  _Generic(s,                                                   \
    -+          const char *:               strprefix_(s, p),         \
    -+          const void *:               strprefix_(s, p),         \
    -+          char *:  const_cast(char *, strprefix_(s, p)),        \
    -+          void *:  const_cast(char *, strprefix_(s, p))         \
    ++          const char *:               strprefix_(s, prefix),    \
    ++          const void *:               strprefix_(s, prefix),    \
    ++          char *:  const_cast(char *, strprefix_(s, prefix)),   \
    ++          void *:  const_cast(char *, strprefix_(s, prefix))    \
     +  )                                                             \
     +)
     +
2:  e3d3332c = 2:  3db55209 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  d6259969 = 3:  b1f60988 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4
  • Reduce repetition
$ git range-diff master gh/rmdir_leading rmdir_leading 
1:  f8deaed9 ! 1:  ec7ba07c lib/string/strcmp/: strprefix(): Add API
    @@ lib/string/strcmp/strprefix.h (new)
     +
     +
     +#define strprefix(s, prefix)                                          \
    -+(                                                                     \
    ++({                                                                    \
    ++  const char  *p_;                                              \
    ++                                                                      \
    ++  p_ = strprefix_(s, prefix);                                   \
    ++                                                                      \
     +  _Generic(s,                                                   \
    -+          const char *:               strprefix_(s, prefix),    \
    -+          const void *:               strprefix_(s, prefix),    \
    -+          char *:  const_cast(char *, strprefix_(s, prefix)),   \
    -+          void *:  const_cast(char *, strprefix_(s, prefix))    \
    -+  )                                                             \
    -+)
    ++          const char *:                     p_,                 \
    ++          const void *:                     p_,                 \
    ++          char *:        const_cast(char *, p_),                \
    ++          void *:        const_cast(char *, p_)                 \
    ++  );
    ++})
     +
     +
     +ATTR_STRING(1)
2:  3db55209 = 2:  d889874b lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  b1f60988 = 3:  7cb4a91b lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4b
  • Add missing \
$ git range-diff master gh/rmdir_leading rmdir_leading 
1:  ec7ba07c ! 1:  a165d0c7 lib/string/strcmp/: strprefix(): Add API
    @@ lib/string/strcmp/strprefix.h (new)
     +          const void *:                     p_,                 \
     +          char *:        const_cast(char *, p_),                \
     +          void *:        const_cast(char *, p_)                 \
    -+  );
    ++  );                                                            \
     +})
     +
     +
2:  d889874b = 2:  d7ce4268 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  7cb4a91b = 3:  05c4ffb4 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4c
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  a165d0c7 = 1:  064e7a50 lib/string/strcmp/: strprefix(): Add API
2:  d7ce4268 = 2:  e134d63a lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  05c4ffb4 = 3:  4cd5dedc lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4d
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  064e7a50 = 1:  024ee8e0 lib/string/strcmp/: strprefix(): Add API
2:  e134d63a = 2:  caf89f8e lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  4cd5dedc = 3:  04d59a59 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4e
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  024ee8e0 = 1:  1f0a9146 lib/string/strcmp/: strprefix(): Add API
2:  caf89f8e = 2:  0bfe0cfa lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  04d59a59 = 3:  bd50dbcc lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4f
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  1f0a9146 = 1:  962d9a6e lib/string/strcmp/: strprefix(): Add API
2:  0bfe0cfa = 2:  52770ba2 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  bd50dbcc = 3:  4c491145 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4g
  • Rebase
$ git range-diff alx/master..gh/rmdir_leading master..rmdir_leading 
1:  962d9a6e = 1:  5e5ec22c lib/string/strcmp/: strprefix(): Add API
2:  52770ba2 = 2:  88bf3c7a lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  4c491145 = 3:  52b40efd lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4h
  • Rebase
$ git range-diff alx/master..gh/rmdir_leading master..rmdir_leading 
1:  5e5ec22c = 1:  51b7da6b lib/string/strcmp/: strprefix(): Add API
2:  88bf3c7a = 2:  b351c7b4 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  52b40efd = 3:  200c2deb lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4i
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  51b7da6b = 1:  ca1eded7 lib/string/strcmp/: strprefix(): Add API
2:  b351c7b4 = 2:  ecc493a6 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  200c2deb = 3:  ee0f6c2e lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4j
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  ca1eded7 = 1:  0f44e36d lib/string/strcmp/: strprefix(): Add API
2:  ecc493a6 = 2:  87717dc0 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  ee0f6c2e = 3:  8866d334 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4k
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  0f44e36d ! 1:  1d477179 lib/string/strcmp/: strprefix(): Add API
    @@ Commit message
     
      ## lib/Makefile.am ##
     @@ lib/Makefile.am: libshadow_la_SOURCES = \
    -   string/strchr/strrspn.h \
    +   string/strcmp/strcaseeq.h \
        string/strcmp/streq.c \
        string/strcmp/streq.h \
     +  string/strcmp/strprefix.c \
2:  87717dc0 = 2:  c989d60d lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  8866d334 = 3:  411888ca lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4l
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  1d477179 = 1:  1bfdc1cb lib/string/strcmp/: strprefix(): Add API
2:  c989d60d = 2:  f45aab6b lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  411888ca = 3:  bf7f8cad lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4m
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  1bfdc1cb = 1:  8cc7d036 lib/string/strcmp/: strprefix(): Add API
2:  f45aab6b = 2:  815cc240 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  bf7f8cad = 3:  5e860ca7 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4n
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  8cc7d036 = 1:  efabd0ed lib/string/strcmp/: strprefix(): Add API
2:  815cc240 = 2:  61b0bff6 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  5e860ca7 = 3:  a4490364 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4o
  • Rebase
$ git range-diff db/master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  efabd0ed = 1:  435cbf06 lib/string/strcmp/: strprefix(): Add API
2:  61b0bff6 = 2:  3efbbb7e lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  a4490364 = 3:  e145231c lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4p
  • Rebase
$ git rd
1:  435cbf06 = 1:  00d64630 lib/string/strcmp/: strprefix(): Add API
2:  3efbbb7e = 2:  f59c2290 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  e145231c = 3:  7188130a lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4q
  • Rebase
$ git rd
1:  00d64630 < -:  -------- lib/string/strcmp/: strprefix(): Add API
2:  f59c2290 = 1:  dcd93bef lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  7188130a = 2:  f991a56b lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4r
  • Rebase
$ git rd
1:  dcd93bef = 1:  f7b69308 lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  f991a56b = 2:  58d393dc lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4s
  • Rebase
$ git rd
1:  f7b69308 = 1:  713eb36f lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  58d393dc = 2:  700729b9 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4t
  • Rebase
$ git rd
1:  713eb36f = 1:  5842635f lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  700729b9 = 2:  638006c8 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4u
  • Rebase
$ git rd
1:  5842635f = 1:  98ade0aa lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  638006c8 = 2:  5cca5c81 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4v
  • Rebase
$ git rd 
1:  2c7d5ca3 = 1:  30718345 lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  156a42f8 = 2:  2f6a4c51 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v5
  • Rebase
$ git rd 
1:  30718345 ! 1:  bbc7517f lib/tcbfuncs.c: rmdir_leading(): Constify input
    @@ lib/tcbfuncs.c: static shadowtcb_status unlink_suffs (const char *user)
     +
        while ((ind = strrchr (path, '/'))) {
                stpcpy(ind, "");
    -           if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
    +           dir = aprintf(TCB_DIR "/%s", path);
    +-          if (dir == NULL) {
     -                  OUT_OF_MEMORY;
     -                  return SHADOWTCB_FAILURE;
    +-          }
    ++          if (dir == NULL)
     +                  goto free_path;
    -           }
     +
                if (rmdir (dir) != 0) {
                        if (errno != ENOTEMPTY) {
2:  2f6a4c51 ! 2:  755952c6 lib/tcbfuncs.c: rmdir_leading(): Create string just once
    @@ Commit message
     
      ## lib/tcbfuncs.c ##
     @@
    - #include "shadowio.h"
      #include "shadowlog_internal.h"
    + #include "string/sprintf/aprintf.h"
      #include "string/strcmp/streq.h"
     +#include "string/strcmp/strprefix.h"
      
    @@ lib/tcbfuncs.c: static shadowtcb_status unlink_suffs (const char *user)
        shadowtcb_status ret = SHADOWTCB_SUCCESS;
      
     -  path = strdup(relpath);
    --  if (path == NULL)
    -+  if (asprintf(&path, TCB_DIR "/%s", relpath) == -1)
    ++  path = aprintf(TCB_DIR "/%s", relpath);
    +   if (path == NULL)
                goto oom;
      
     +  p = strprefix(path, TCB_DIR "/");
    @@ lib/tcbfuncs.c: static shadowtcb_status unlink_suffs (const char *user)
     -  while ((ind = strrchr (path, '/'))) {
     +  while ((ind = strrchr(p, '/'))) {
                stpcpy(ind, "");
    --          if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
    +-          dir = aprintf(TCB_DIR "/%s", path);
    +-          if (dir == NULL)
     -                  goto free_path;
    --          }
      
     -          if (rmdir (dir) != 0) {
     +          if (rmdir(path) != 0) {
v5b
  • Rebase
$ git rd 
1:  bbc7517f = 1:  42b09cd7 lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  755952c6 = 2:  3b6df1e1 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v5c
  • Rebase
$ git rd 
1:  42b09cd7 = 1:  9d047699 lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  3b6df1e1 = 2:  3036ce24 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v5d
  • Rebase
$ git rd 
1:  9d047699 = 1:  72d31341 lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  3036ce24 = 2:  ae335d94 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v5e
  • Rebase
$ git rd 
1:  72d31341 = 1:  9ff60c37 lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  ae335d94 = 2:  3037107e lib/tcbfuncs.c: rmdir_leading(): Create string just once
v5f
  • Rebase
$ git rd 
1:  9ff60c37 = 1:  7b1951d8 lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  3037107e = 2:  7573122f lib/tcbfuncs.c: rmdir_leading(): Create string just once
v5g
  • Rebase
$ git rd 
1:  7b1951d8 = 1:  1769db20 lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  7573122f = 2:  7d9879e7 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v5h
  • Rebase
$ git rd 
1:  1769db20 = 1:  6839ed6b lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  7d9879e7 = 2:  e5a1f888 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v5i
  • Rebase
$ git rd 
1:  6839ed6bd = 1:  93747e031 lib/tcbfuncs.c: rmdir_leading(): Constify input
2:  e5a1f8881 = 2:  385e21c9b lib/tcbfuncs.c: rmdir_leading(): Create string just once

@alejandro-colomar alejandro-colomar force-pushed the rmdir_leading branch 8 times, most recently from 04d59a5 to bd50dbc Compare December 6, 2024 11:49
@alejandro-colomar
Copy link
Collaborator Author

Queued after the release of 4.17.0.

@alejandro-colomar alejandro-colomar marked this pull request as draft December 6, 2024 12:16
@alejandro-colomar alejandro-colomar marked this pull request as ready for review December 22, 2024 11:59
@alejandro-colomar alejandro-colomar force-pushed the rmdir_leading branch 2 times, most recently from 200c2de to ee0f6c2 Compare January 24, 2025 15:04
@alejandro-colomar alejandro-colomar marked this pull request as draft February 16, 2025 16:02
@alejandro-colomar
Copy link
Collaborator Author

Queued after #1152.

@alejandro-colomar alejandro-colomar force-pushed the rmdir_leading branch 2 times, most recently from bf7f8ca to 5e860ca Compare February 16, 2025 22:54
@alejandro-colomar alejandro-colomar marked this pull request as ready for review May 26, 2025 16:51
@alejandro-colomar alejandro-colomar force-pushed the rmdir_leading branch 2 times, most recently from 58d393d to 700729b Compare May 30, 2025 21:43
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