Skip to content

X #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

X #1263

wants to merge 8 commits into from

Conversation

alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Jun 4, 2025

Simplify and centralize the implementation of function variants that exit on error.


Revisions:

v1b
  • Rebase
$ git rd 
1:  db513577 = 1:  a0d169d5 lib/: X(): Add macro to exit() on error
2:  2d05cfcc = 2:  3cafcd16 lib/string/strdup/: xstrdup(): Reimplement xstrdup() in terms of X()
3:  f9ccb172 = 3:  f95e2ea3 lib/alloc/x/: X*ALLOC(): Reimplement in terms of X()
4:  727b967a = 4:  b4f10a23 lib/string/sprintf/: xaprintf(): Reimplement in terms of X()
5:  d99d82da = 5:  ec706b48 lib/string/strdup/: XSTRNDUP(): Reimplement in terms of X()
6:  80aec6fb = 6:  56aac974 lib/string/strdup/xstrndup.h: Add STRNDUP()
7:  4dc8c9e6 ! 7:  98d83757 lib/, src/, tests/: Move x*() definitions to non-x* header files
    @@ lib/getdef.c
      #include "string/strcmp/streq.h"
      #include "string/strcmp/strprefix.h"
     
    - ## lib/gshadow.c ##
    -@@
    - 
    - #include "alloc/malloc.h"
    - #include "alloc/realloc.h"
    --#include "alloc/x/xmalloc.h"
    - #include "defines.h"
    - #include "prototypes.h"
    - #include "string/strchr/strchrcnt.h"
    -
      ## lib/idmapping.c ##
     @@
      #endif
    @@ lib/list.c
      #include "string/strcmp/streq.h"
     -#include "string/strdup/xstrdup.h"
     +#include "string/strdup/strdup.h"
    + #include "string/strtok/strsep2ls.h"
      
      
    - /*
     
      ## lib/mail.c ##
     @@
    @@ src/newusers.c
     -#include "string/strdup/xstrdup.h"
     +#include "string/strdup/strdup.h"
      #include "string/strtok/stpsep.h"
    - 
    + #include "string/strtok/strsep2arr.h"
      
     
      ## src/passwd.c ##
v2
  • Do likewise with the newly added xastrsep2ls().
$ git range-diff shadow/master gh/x x 
1:  a0d169d5 = 1:  a0d169d5 lib/: X(): Add macro to exit() on error
2:  3cafcd16 = 2:  3cafcd16 lib/string/strdup/: xstrdup(): Reimplement xstrdup() in terms of X()
3:  f95e2ea3 = 3:  f95e2ea3 lib/alloc/x/: X*ALLOC(): Reimplement in terms of X()
4:  b4f10a23 = 4:  b4f10a23 lib/string/sprintf/: xaprintf(): Reimplement in terms of X()
5:  ec706b48 = 5:  ec706b48 lib/string/strdup/: XSTRNDUP(): Reimplement in terms of X()
6:  56aac974 = 6:  56aac974 lib/string/strdup/xstrndup.h: Add STRNDUP()
-:  -------- > 7:  1775ed65 lib/string/strtok/: xastrsep2ls() Reimplement in terms of X()
7:  98d83757 ! 8:  14d0c3e9 lib/, src/, tests/: Move x*() definitions to non-x* header files
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
        string/strftime.c \
        string/strftime.h \
        string/strspn/stpspn.c \
    +@@ lib/Makefile.am: libshadow_la_SOURCES = \
    +   string/strtok/strsep2arr.h \
    +   string/strtok/strsep2ls.c \
    +   string/strtok/strsep2ls.h \
    +-  string/strtok/xastrsep2ls.c \
    +-  string/strtok/xastrsep2ls.h \
    +   strtoday.c \
    +   sub.c \
    +   subordinateio.h \
     
      ## lib/alloc/calloc.h ##
     @@
    @@ lib/getdef.c
      #include "string/strcmp/streq.h"
      #include "string/strcmp/strprefix.h"
     
    + ## lib/gshadow.c ##
    +@@
    + #include "defines.h"
    + #include "prototypes.h"
    + #include "string/strcmp/streq.h"
    ++#include "string/strtok/astrsep2ls.h"
    + #include "string/strtok/stpsep.h"
    + #include "string/strtok/strsep2arr.h"
    +-#include "string/strtok/xastrsep2ls.h"
    + 
    + 
    + static /*@null@*/FILE *shadow;
    +
      ## lib/idmapping.c ##
     @@
      #endif
    @@ lib/string/strdup/xstrdup.c (deleted)
     -
     -#include "string/strdup/xstrdup.h"
     
    + ## lib/string/strtok/astrsep2ls.h ##
    +@@
    +-// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
    ++// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <[email protected]>
    + // SPDX-License-Identifier: BSD-3-Clause
    + 
    + 
    +@@
    + #include "attr.h"
    + #include "string/strchr/strchrscnt.h"
    + #include "string/strtok/strsep2ls.h"
    ++#include "x.h"
    ++
    ++
    ++#define xastrsep2ls(s, delim, np)  X(astrsep2ls(s, delim, np))
    + 
    + 
    + ATTR_ACCESS(read_write, 1) ATTR_ACCESS(write_only, 3)
    +
    + ## lib/string/strtok/xastrsep2ls.c (deleted) ##
    +@@
    +-// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <[email protected]>
    +-// SPDX-License-Identifier: BSD-3-Clause
    +-
    +-
    +-#include <config.h>
    +-
    +-#include "string/strtok/xastrsep2ls.h"
    +
    + ## lib/string/strtok/xastrsep2ls.h (deleted) ##
    +@@
    +-// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <[email protected]>
    +-// SPDX-License-Identifier: BSD-3-Clause
    +-
    +-
    +-#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_XASTRSEP2LS_H_
    +-#define SHADOW_INCLUDE_LIB_STRING_STRTOK_XASTRSEP2LS_H_
    +-
    +-
    +-#include <config.h>
    +-
    +-#include "string/strtok/astrsep2ls.h"
    +-#include "x.h"
    +-
    +-
    +-#define xastrsep2ls(s, delim, np)  X(astrsep2ls(s, delim, np))
    +-
    +-
    +-#endif  // include guard
    +
      ## lib/utmp.c ##
     @@
      #include <string.h>
v2b
  • Rebase
$ git rd
1:  a0d169d5 = 1:  2457b0b3 lib/: X(): Add macro to exit() on error
2:  3cafcd16 = 2:  caa136e7 lib/string/strdup/: xstrdup(): Reimplement xstrdup() in terms of X()
3:  f95e2ea3 = 3:  ea734fb4 lib/alloc/x/: X*ALLOC(): Reimplement in terms of X()
4:  b4f10a23 = 4:  a90b89fb lib/string/sprintf/: xaprintf(): Reimplement in terms of X()
5:  ec706b48 = 5:  c06652d3 lib/string/strdup/: XSTRNDUP(): Reimplement in terms of X()
6:  56aac974 = 6:  a62faca1 lib/string/strdup/xstrndup.h: Add STRNDUP()
7:  1775ed65 = 7:  f57f94de lib/string/strtok/: xastrsep2ls() Reimplement in terms of X()
8:  14d0c3e9 = 8:  2ac1426e lib/, src/, tests/: Move x*() definitions to non-x* header files

Writing an x*() variant of several functions is unnecessary.  It's
simpler to write a generic X() macro that can be chained with any other
calls.

For example:

	-  X(malloc(...)) instead of xmalloc(...);
	-  X(strdup(...)) instead of xstrdup(...);

If an error is detected, log an error, and exit(13).  About why 13, I
don't really know.  It's just what was used previously in xmalloc().

Signed-off-by: Alejandro Colomar <[email protected]>
This is much simpler.

Signed-off-by: Alejandro Colomar <[email protected]>
Now that all of these are one-liners, they don't need a separate header
file.  Compact stuff.

Signed-off-by: Alejandro Colomar <[email protected]>
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