Skip to content
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

[x][v]aprintf(): Add APIs, and use them instead of [x][v]asprintf(3) #1168

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

Conversation

alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Jan 1, 2025

Cc: @stoeckmann


Revisions:

v2
  • tfix
$ git range-diff shadow/master gh/asprintf_ asprintf_ 
 1:  cb92c9bf =  1:  cb92c9bf lib/string/sprintf/: [v]aprintf(): Add functions
 2:  565dcd46 =  2:  565dcd46 lib/tcbfuncs.c: Split conditionals
 3:  fa55fb2f !  3:  dc720127 lib/, src/: Use aprintf() instead of asprintf(3)
    @@ lib/tcbfuncs.c: shadowtcb_status shadowtcb_move (/*@NULL@*/const char *user_newn
                return SHADOWTCB_FAILURE;
        }
     -  if (asprintf(&shadow, TCB_FMT, user_newname) == -1) {
    -+  shadow = asprintF_(TCB_FMT, user_newname);
    ++  shadow = aprintf(TCB_FMT, user_newname);
     +  if (shadow == NULL) {
                OUT_OF_MEMORY;
                return SHADOWTCB_FAILURE;
 4:  05f40737 =  4:  20364c16 lib/: Use vaprintf() instead of vasprintf(3)
 5:  910ca5cf =  5:  3f036027 lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
 6:  840fd9d0 =  6:  c6c5b6f9 lib/, src/: Use xaprintf() instead of xasprintf()
 7:  ee37b595 =  7:  e91543af lib/, src/: Remove unnecessary variables
 8:  5510eb72 =  8:  36f2e2e3 lib/tcbfuncs.c: shadowtcb_path_rel(): Use strdup(3) instead of its pattern
 9:  339e14ab =  9:  d46909ca lib/tcbfuncs.c: shadowtcb_path_rel(): Centralize error handling
v2b
  • Rebase
$ git range-diff 4.17.1..gh/asprintf_ master..asprintf_ 
 1:  cb92c9bf =  1:  194632cb lib/string/sprintf/: [v]aprintf(): Add functions
 2:  565dcd46 =  2:  4477dcd2 lib/tcbfuncs.c: Split conditionals
 3:  dc720127 =  3:  1ec6264e lib/, src/: Use aprintf() instead of asprintf(3)
 4:  20364c16 =  4:  75b15ee3 lib/: Use vaprintf() instead of vasprintf(3)
 5:  3f036027 =  5:  183e3e05 lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
 6:  c6c5b6f9 =  6:  3817fcee lib/, src/: Use xaprintf() instead of xasprintf()
 7:  e91543af =  7:  0f4c4292 lib/, src/: Remove unnecessary variables
 8:  36f2e2e3 =  8:  9ee3c41b lib/tcbfuncs.c: shadowtcb_path_rel(): Use strdup(3) instead of its pattern
 9:  d46909ca =  9:  60287718 lib/tcbfuncs.c: shadowtcb_path_rel(): Centralize error handling
v2c
  • Rebase
 1:  194632cb =  1:  5887d521 lib/string/sprintf/: [v]aprintf(): Add functions
 2:  4477dcd2 =  2:  c6a553a4 lib/tcbfuncs.c: Split conditionals
 3:  1ec6264e =  3:  f4222723 lib/, src/: Use aprintf() instead of asprintf(3)
 4:  75b15ee3 =  4:  61bf169d lib/: Use vaprintf() instead of vasprintf(3)
 5:  183e3e05 =  5:  7a27b4e5 lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
 6:  3817fcee =  6:  199c1d94 lib/, src/: Use xaprintf() instead of xasprintf()
 7:  0f4c4292 =  7:  7544923c lib/, src/: Remove unnecessary variables
 8:  9ee3c41b =  8:  33dee27a lib/tcbfuncs.c: shadowtcb_path_rel(): Use strdup(3) instead of its pattern
 9:  60287718 =  9:  f7543211 lib/tcbfuncs.c: shadowtcb_path_rel(): Centralize error handling
v2d
  • Rebase
$ git range-diff master..gh/asprintf_ shadow/master..asprintf_ 
 1:  5887d521 =  1:  aff879b5 lib/string/sprintf/: [v]aprintf(): Add functions
 2:  c6a553a4 =  2:  4bb3fe90 lib/tcbfuncs.c: Split conditionals
 3:  f4222723 =  3:  d44da1a4 lib/, src/: Use aprintf() instead of asprintf(3)
 4:  61bf169d =  4:  082648bd lib/: Use vaprintf() instead of vasprintf(3)
 5:  7a27b4e5 =  5:  8a8e877f lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
 6:  199c1d94 !  6:  1e7bb9cb lib/, src/: Use xaprintf() instead of xasprintf()
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
     +  string/sprintf/xaprintf.h \
        string/strchr/strchrcnt.c \
        string/strchr/strchrcnt.h \
    -   string/strchr/stpspn.c \
    +   string/strchr/strchrscnt.c \
     
      ## lib/copydir.c ##
     @@
    @@ src/userdel.c
      #include "string/strcmp/streq.h"
      #include "string/strdup/xstrdup.h"
      
    -@@ src/userdel.c: static int remove_mailbox (void)
    +@@ src/userdel.c: static bool remove_mailbox (void)
        }
      
        if (prefix[0]) {
 7:  7544923c =  7:  801ed4d5 lib/, src/: Remove unnecessary variables
 8:  33dee27a =  8:  a33c76f8 lib/tcbfuncs.c: shadowtcb_path_rel(): Use strdup(3) instead of its pattern
 9:  f7543211 =  9:  fb328c96 lib/tcbfuncs.c: shadowtcb_path_rel(): Centralize error handling
v2e
  • Rebase
$ git range-diff master..gh/asprintf_ shadow/master..asprintf_ 
 1:  aff879b5 =  1:  3a9bb509 lib/string/sprintf/: [v]aprintf(): Add functions
 2:  4bb3fe90 =  2:  8905808f lib/tcbfuncs.c: Split conditionals
 3:  d44da1a4 =  3:  bff2994b lib/, src/: Use aprintf() instead of asprintf(3)
 4:  082648bd =  4:  aaaa092f lib/: Use vaprintf() instead of vasprintf(3)
 5:  8a8e877f =  5:  9b5bfceb lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
 6:  1e7bb9cb =  6:  77cee64b lib/, src/: Use xaprintf() instead of xasprintf()
 7:  801ed4d5 =  7:  0b4a0c0e lib/, src/: Remove unnecessary variables
 8:  a33c76f8 =  8:  bc1885f3 lib/tcbfuncs.c: shadowtcb_path_rel(): Use strdup(3) instead of its pattern
 9:  fb328c96 =  9:  b76ee5dc lib/tcbfuncs.c: shadowtcb_path_rel(): Centralize error handling
v2f
  • Rebase
$ git range-diff master..gh/asprintf_ shadow/master..asprintf_ 
 1:  3a9bb509 =  1:  f9904a7a lib/string/sprintf/: [v]aprintf(): Add functions
 2:  8905808f =  2:  94d676f0 lib/tcbfuncs.c: Split conditionals
 3:  bff2994b !  3:  6204313d lib/, src/: Use aprintf() instead of asprintf(3)
    @@ src/useradd.c
     +#include "string/sprintf/aprintf.h"
      #include "string/sprintf/snprintf.h"
      #include "string/sprintf/xasprintf.h"
    - #include "string/strcmp/streq.h"
    + #include "string/strcmp/strcaseeq.h"
     @@ src/useradd.c: get_defaults(void)
        const char  *ccp;
      
 4:  aaaa092f =  4:  52f0783e lib/: Use vaprintf() instead of vasprintf(3)
 5:  9b5bfceb =  5:  828e2f14 lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
 6:  77cee64b !  6:  be71b233 lib/, src/: Use xaprintf() instead of xasprintf()
    @@ lib/getdef.c
     +#include "string/sprintf/xaprintf.h"
      #include "string/strchr/stpspn.h"
      #include "string/strchr/strrspn.h"
    - #include "string/strcmp/streq.h"
    + #include "string/strcmp/strcaseeq.h"
     @@ lib/getdef.c: void setdef_config_file (const char* file)
      #ifdef USE_ECONF
        char  *cp;
    @@ src/useradd.c
      #include "string/sprintf/snprintf.h"
     -#include "string/sprintf/xasprintf.h"
     +#include "string/sprintf/xaprintf.h"
    + #include "string/strcmp/strcaseeq.h"
      #include "string/strcmp/streq.h"
      #include "string/strdup/xstrdup.h"
    - #include "string/strtok/stpsep.h"
     @@ src/useradd.c: get_defaults(void)
                        if (prefix[0]) {
                                char  *dt;
 7:  0b4a0c0e !  7:  c3ca2204 lib/, src/: Remove unnecessary variables
    @@ src/useradd.c: get_defaults(void)
                                def_usrtemplate = xstrdup(ccp);
                        }
     @@ src/useradd.c: static void process_flags (int argc, char **argv)
    -                   exit (E_BAD_ARG);
    +                   exit (E_BAD_NAME);
                }
                if (!dflg) {
     -                  char  *uh;
 8:  bc1885f3 =  8:  eb2c1bf3 lib/tcbfuncs.c: shadowtcb_path_rel(): Use strdup(3) instead of its pattern
 9:  b76ee5dc =  9:  ca768f28 lib/tcbfuncs.c: shadowtcb_path_rel(): Centralize error handling
v3
  • Add comments spelling out the meaning of the letter-soup API names. [@hallyn ]
$ git range-diff master gh/asprintf_ asprintf_ 
 1:  f9904a7a !  1:  28dee096 lib/string/sprintf/: [v]aprintf(): Add functions
    @@ lib/string/sprintf/aprintf.h (new)
     +inline char *vaprintf(const char *restrict fmt, va_list ap);
     +
     +
    ++// allocate print formatted
     +// Like asprintf(3), but simpler; omit the length.
     +inline char *
     +aprintf(const char *restrict fmt, ...)
 2:  94d676f0 =  2:  302f506d lib/tcbfuncs.c: Split conditionals
 3:  6204313d =  3:  e5d2142a lib/, src/: Use aprintf() instead of asprintf(3)
 4:  52f0783e =  4:  8e7201e4 lib/: Use vaprintf() instead of vasprintf(3)
 5:  828e2f14 !  5:  e382f6db lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
    @@ lib/string/sprintf/xaprintf.h (new)
     +inline char *xvaprintf(const char *restrict fmt, va_list ap);
     +
     +
    ++// exit-on-error allocate print formatted
     +inline char *
     +xaprintf(const char *restrict fmt, ...)
     +{
 6:  be71b233 =  6:  2df9b32b lib/, src/: Use xaprintf() instead of xasprintf()
 7:  c3ca2204 =  7:  86149753 lib/, src/: Remove unnecessary variables
 8:  eb2c1bf3 =  8:  bc54adda lib/tcbfuncs.c: shadowtcb_path_rel(): Use strdup(3) instead of its pattern
 9:  ca768f28 =  9:  66e9eb5b lib/tcbfuncs.c: shadowtcb_path_rel(): Centralize error handling
v3b
  • Rebase
$ git range-diff master..gh/asprintf_ shadow/master..asprintf_ 
 1:  28dee096 =  1:  ff0e552f lib/string/sprintf/: [v]aprintf(): Add functions
 2:  302f506d =  2:  c9469168 lib/tcbfuncs.c: Split conditionals
 3:  e5d2142a =  3:  f47454fa lib/, src/: Use aprintf() instead of asprintf(3)
 4:  8e7201e4 =  4:  33848ec4 lib/: Use vaprintf() instead of vasprintf(3)
 5:  e382f6db =  5:  3cacf5a2 lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
 6:  2df9b32b !  6:  3a811a08 lib/, src/: Use xaprintf() instead of xasprintf()
    @@ lib/getdef.c
      #include "shadowlog_internal.h"
     -#include "string/sprintf/xasprintf.h"
     +#include "string/sprintf/xaprintf.h"
    - #include "string/strchr/stpspn.h"
    - #include "string/strchr/strrspn.h"
      #include "string/strcmp/strcaseeq.h"
    + #include "string/strcmp/streq.h"
    + #include "string/strspn/stpspn.h"
     @@ lib/getdef.c: void setdef_config_file (const char* file)
      #ifdef USE_ECONF
        char  *cp;
    @@ lib/setupenv.c
      #include "shadowlog.h"
     -#include "string/sprintf/xasprintf.h"
     +#include "string/sprintf/xaprintf.h"
    - #include "string/strchr/stpspn.h"
      #include "string/strcmp/streq.h"
      #include "string/strdup/xstrdup.h"
    + #include "string/strspn/stpspn.h"
     @@ lib/setupenv.c: addenv_path(const char *varname, const char *dirname, const char *filename)
      {
        char  *buf;
 7:  86149753 =  7:  0c3db424 lib/, src/: Remove unnecessary variables
 8:  bc54adda =  8:  ecfec672 lib/tcbfuncs.c: shadowtcb_path_rel(): Use strdup(3) instead of its pattern
 9:  66e9eb5b =  9:  ac143467 lib/tcbfuncs.c: shadowtcb_path_rel(): Centralize error handling
v3c
  • Rebase
$ git range-diff master..gh/asprintf_ shadow/master..asprintf_ 
 1:  ff0e552f !  1:  689eb1d6 lib/string/sprintf/: [v]aprintf(): Add functions
    @@ Commit message
     
      ## lib/Makefile.am ##
     @@ lib/Makefile.am: libshadow_la_SOURCES = \
    -   sssd.h \
    +   string/ctype/strisascii/strisdigit.h \
        string/memset/memzero.c \
        string/memset/memzero.h \
     +  string/sprintf/aprintf.c \
 2:  c9469168 =  2:  d511de71 lib/tcbfuncs.c: Split conditionals
 3:  f47454fa =  3:  0edba1e0 lib/, src/: Use aprintf() instead of asprintf(3)
 4:  33848ec4 =  4:  d2cca417 lib/: Use vaprintf() instead of vasprintf(3)
 5:  3cacf5a2 =  5:  42b0e0f0 lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
 6:  3a811a08 =  6:  debbc05b lib/, src/: Use xaprintf() instead of xasprintf()
 7:  0c3db424 =  7:  ac08cac7 lib/, src/: Remove unnecessary variables
 8:  ecfec672 =  8:  1662a650 lib/tcbfuncs.c: shadowtcb_path_rel(): Use strdup(3) instead of its pattern
 9:  ac143467 =  9:  932734c4 lib/tcbfuncs.c: shadowtcb_path_rel(): Centralize error handling
v3d
  • Rebase
$ git range-diff master..gh/asprintf_ shadow/master..asprintf_ 
 1:  689eb1d6 =  1:  ef3710d8 lib/string/sprintf/: [v]aprintf(): Add functions
 2:  d511de71 =  2:  4877357a lib/tcbfuncs.c: Split conditionals
 3:  0edba1e0 =  3:  7212cb7e lib/, src/: Use aprintf() instead of asprintf(3)
 4:  d2cca417 =  4:  fe5c70f7 lib/: Use vaprintf() instead of vasprintf(3)
 5:  42b0e0f0 !  5:  2aa9e80f lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
    @@ tests/unit/Makefile.am: test_typetraits_LDADD = \
      
     
      ## tests/unit/test_xasprintf.c => tests/unit/test_xaprintf.c ##
    -@@
    --/*
    -- * SPDX-FileCopyrightText: 2023, Alejandro Colomar <[email protected]>
    -- * SPDX-License-Identifier: BSD-3-Clause
    -- */
    -+// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <[email protected]>
    -+// SPDX-License-Identifier: BSD-3-Clause
    - 
    - 
    - #include <setjmp.h>
     @@
      #include <stdint.h>  // Required by <cmocka.h>
      #include <cmocka.h>
    @@ tests/unit/test_xasprintf.c => tests/unit/test_xaprintf.c
     +#include "string/sprintf/xaprintf.h"
      
      
    + #define smock()               _Generic(mock(), uintmax_t: (intmax_t) mock())
      #define assert_unreachable()  assert_true(0)
      
     -#define XASPRINTF_CALLED  (-36)
 6:  debbc05b !  6:  70b87dbc lib/, src/: Use xaprintf() instead of xasprintf()
    @@ src/usermod.c
      #include "string/strdup/xstrdup.h"
      #include "time/day_to_str.h"
     @@ src/usermod.c: static char *new_pw_passwd (char *pw_pass)
    -                         "updating passwd", user_newname, user_newid, 0);
    +                         "updating-passwd", user_newname, user_newid, 1);
      #endif
                SYSLOG ((LOG_INFO, "lock user '%s' password", user_newname));
     -          xasprintf(&buf, "!%s", pw_pass);
 7:  ac08cac7 !  7:  47c5c727 lib/, src/: Remove unnecessary variables
    @@ src/usermod.c: usage (int status)
     -
      #ifdef WITH_AUDIT
                audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
    -                         "updating passwd", user_newname, user_newid, 0);
    +                         "updating-passwd", user_newname, user_newid, 1);
      #endif
                SYSLOG ((LOG_INFO, "lock user '%s' password", user_newname));
     -          buf = xaprintf("!%s", pw_pass);
 8:  1662a650 =  8:  1c39960b lib/tcbfuncs.c: shadowtcb_path_rel(): Use strdup(3) instead of its pattern
 9:  932734c4 =  9:  ed62940e lib/tcbfuncs.c: shadowtcb_path_rel(): Centralize error handling

@alejandro-colomar alejandro-colomar changed the title [x][v]asprintf_(): Add APIs, and use them [x][v]asprintf_(): Add APIs, and use them instead of [x][v]asprintf(3) Jan 1, 2025
@alejandro-colomar alejandro-colomar force-pushed the asprintf_ branch 7 times, most recently from 8426bbd to aaa727a Compare January 1, 2025 15:58
@alejandro-colomar alejandro-colomar marked this pull request as ready for review January 1, 2025 16:49
@alejandro-colomar alejandro-colomar changed the title [x][v]asprintf_(): Add APIs, and use them instead of [x][v]asprintf(3) [x][v]aprintf(): Add APIs, and use them instead of [x][v]asprintf(3) Jan 2, 2025
@alejandro-colomar alejandro-colomar force-pushed the asprintf_ branch 2 times, most recently from d8ac495 to 75c5f3f Compare January 2, 2025 02:22
@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented Jan 2, 2025

As a curiosity, this aprintf() API seems to be exactly the same that Plan9 calls smprint(3).

https://9fans.github.io/plan9port/man/man3/print.html

These functions are just like [v]asprintf(3), but simpler.

They return the newly allocated memory, which allows us to use the
[[gnu::malloc(free)]] attribute, which enhances static analysis.
They also omit the length, which we don't care about at all.

As a curiosity, Plan9 seems to provide this same API, under the name
smprint(3).

Link: <https://9fans.github.io/plan9port/man/man3/print.html>
Signed-off-by: Alejandro Colomar <[email protected]>
This is in preparation for the next commit.

Signed-off-by: Alejandro Colomar <[email protected]>
…printf()

Wrap [v]aprintf() instead of [v]asprintf(3).

Repurpose x[v]asprintf()'s tests to test x[v]aprintf().

Signed-off-by: Alejandro Colomar <[email protected]>
This makes some temporary variables unnecessary.  They'll be removed in
the next commit.

Signed-off-by: Alejandro Colomar <[email protected]>
All these 3 branches have a compatible return value, which we can check
at once after the branches.

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