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

@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