Skip to content

Conversation

@alejandro-colomar
Copy link
Collaborator

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

I've managed to remove one of the static, but most of them seem necessary, since these shadow APIs are either libc APIs or similar to libc APIs, and they're specified as using static memory.

I've also done a lot of simplification and de-duplication work in surrounding code, especially in code transforming CSVs to lists.


Revisions:

v1b
  • Rebase
$ git rd 
 1:  bc196a4c =  1:  e030f9f9 lib/shadow/group/sgetgrent.c: Move free(3) call outside of helper
 2:  03fd5229 =  2:  922cd79c lib/shadow/gshadow/sgetsgent.c: Don't exit from library code
 3:  da4f193d =  3:  6087810b lib/, src/: Use more readable syntax for empty loops
 4:  969c53f6 =  4:  2b9b392d lib/: Deduplicate code
 5:  6bd67e17 =  5:  5a34d66d lib/list.c: comma_to_list(): Allow a trailing comma
 6:  140d5c09 =  6:  7ff2273b lib/list.c: comma_to_list(): Use xastrsep2ls() instead of its pattern
 7:  c1cf0676 =  7:  d6c5e912 lib/shadow/group/sgetgrent.c: Remove redundant code
 8:  09918ba6 =  8:  156085ca lib/list.c: comma_to_list(): Use build_list() instead of its pattern
 9:  06062942 =  9:  f1ee9cd3 lib/: Rename build_list() => acsv2ls()
10:  f0e13f5b = 10:  4f27d718 lib/, src/: Move list.c prototypes to new file list.h
v1c
  • Rebase
$ git rd 
 1:  e030f9f9 =  1:  fdba8427 lib/shadow/group/sgetgrent.c: Move free(3) call outside of helper
 2:  922cd79c =  2:  721a5846 lib/shadow/gshadow/sgetsgent.c: Don't exit from library code
 3:  6087810b =  3:  baac1e9f lib/, src/: Use more readable syntax for empty loops
 4:  2b9b392d =  4:  1b10b043 lib/: Deduplicate code
 5:  5a34d66d =  5:  56803058 lib/list.c: comma_to_list(): Allow a trailing comma
 6:  7ff2273b =  6:  2ad1c605 lib/list.c: comma_to_list(): Use xastrsep2ls() instead of its pattern
 7:  d6c5e912 =  7:  070df553 lib/shadow/group/sgetgrent.c: Remove redundant code
 8:  156085ca =  8:  5ae417e6 lib/list.c: comma_to_list(): Use build_list() instead of its pattern
 9:  f1ee9cd3 =  9:  f3e1a203 lib/: Rename build_list() => acsv2ls()
10:  4f27d718 = 10:  5ec6addd lib/, src/: Move list.c prototypes to new file list.h
v1d
  • Rebase
$ git rd 
 1:  fdba8427c =  1:  3ed5c07dd lib/shadow/group/sgetgrent.c: Move free(3) call outside of helper
 2:  721a5846d !  2:  a5f67fa47 lib/shadow/gshadow/sgetsgent.c: Don't exit from library code
    @@ Commit message
         Signed-off-by: Alejandro Colomar <[email protected]>
     
      ## lib/shadow/gshadow/sgetsgent.c ##
    -@@
    - 
    - #include "shadow/gshadow/sgrp.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"
    - 
    - 
    - #if defined(SHADOWGRP) && !__has_include(<gshadow.h>)
     @@ lib/shadow/gshadow/sgetsgent.c: sgetsgent(const char *s)
        sgroup.sg_adm = build_list(fields[2]);
        sgroup.sg_mem = build_list(fields[3]);
 3:  baac1e9ff =  3:  b148e9e58 lib/, src/: Use more readable syntax for empty loops
 4:  1b10b0432 !  4:  35f0cf989 lib/: Deduplicate code
    @@ lib/list.c
     @@
      #include "string/strchr/strchrcnt.h"
      #include "string/strcmp/streq.h"
    - #include "string/strdup/xstrdup.h"
    + #include "string/strdup/strdup.h"
     +#include "string/strtok/astrsep2ls.h"
      #include "string/strtok/strsep2ls.h"
      
 5:  568030588 =  5:  aecafd272 lib/list.c: comma_to_list(): Allow a trailing comma
 6:  2ad1c6058 !  6:  dbaa72cad lib/list.c: comma_to_list(): Use xastrsep2ls() instead of its pattern
    @@ Commit message
     
      ## lib/list.c ##
     @@
    - #include "alloc/x/xmalloc.h"
    + #include "alloc/malloc.h"
      #include "prototypes.h"
      #include "defines.h"
     -#include "string/strchr/strchrcnt.h"
      #include "string/strcmp/streq.h"
    - #include "string/strdup/xstrdup.h"
    + #include "string/strdup/strdup.h"
      #include "string/strtok/astrsep2ls.h"
     -#include "string/strtok/strsep2ls.h"
     +#include "string/strtok/xastrsep2ls.h"
 7:  070df5533 =  7:  904bbf690 lib/shadow/group/sgetgrent.c: Remove redundant code
 8:  5ae417e6d !  8:  0e6a1845b lib/list.c: comma_to_list(): Use build_list() instead of its pattern
    @@ lib/list.c
      
     @@
      #include "string/strcmp/streq.h"
    - #include "string/strdup/xstrdup.h"
    + #include "string/strdup/strdup.h"
      #include "string/strtok/astrsep2ls.h"
     -#include "string/strtok/xastrsep2ls.h"
      
 9:  f3e1a203f =  9:  4e16128ca lib/: Rename build_list() => acsv2ls()
10:  5ec6adddb ! 10:  df842fb78 lib/, src/: Move list.c prototypes to new file list.h
    @@ lib/list.c
      
      #include <assert.h>
      
    - #include "alloc/x/xmalloc.h"
    + #include "alloc/malloc.h"
     -#include "prototypes.h"
      #include "defines.h"
      #include "string/strcmp/streq.h"
    - #include "string/strdup/xstrdup.h"
    + #include "string/strdup/strdup.h"
     
      ## lib/list.h (new) ##
     @@
    @@ src/groupadd.c
     
      ## src/groupmems.c ##
     @@
    - #include "alloc/x/xmalloc.h"
    + #include "alloc/malloc.h"
      #include "defines.h"
      #include "groupio.h"
     +#include "list.h"

@alejandro-colomar alejandro-colomar marked this pull request as draft June 10, 2025 12:35
@alejandro-colomar alejandro-colomar force-pushed the ls branch 3 times, most recently from 08a1db1 to 866b069 Compare July 20, 2025 14:38
@alejandro-colomar alejandro-colomar force-pushed the ls branch 3 times, most recently from 6e05e52 to afca498 Compare October 2, 2025 10:42
@alejandro-colomar alejandro-colomar force-pushed the ls branch 2 times, most recently from c69ff9b to f0e13f5 Compare October 7, 2025 09:16
@alejandro-colomar alejandro-colomar marked this pull request as ready for review October 7, 2025 09:17
@alejandro-colomar alejandro-colomar force-pushed the ls branch 2 times, most recently from 4f27d71 to 5ec6add Compare October 19, 2025 06:39
This is for consistency with how this is done in sgetsgent().

Now we need to make sure that the list is NULL before the first call,
which means that the 'grent' structure must be cleared.  Since it's
a static object, it's already true, but let's be explicit about it.

Since we remember the address of the list in grent.gr_mem (which is
static), we don't need to keep 'members' being static too, so we get rid
of one static thing.

Signed-off-by: Alejandro Colomar <[email protected]>
Imitate how this is done in sgetgrent().

BE CAREFUL:  The NULL check needs to be performed after *both*
build_list() calls, since we need to make sure that either a NULL or a
valid pointer is stored in both .sg_adm and .sg_mem, since we'll call
free(3) next time sgetsgent() is called.

Signed-off-by: Alejandro Colomar <[email protected]>
Move build_list() to "lib/list.c", where we can reuse it.

Signed-off-by: Alejandro Colomar <[email protected]>
In some places we call comma_to_list(), and in others build_list(), and
they're essentially the same thing: they transform a comma-separated
list into an array of strings.

Both of them consider an empty list to have 0 members instead of one.
However, they differ in how they treat trailing commas:

-  comma_to_list() interprets that as an empty field.
-  build_list() ignores the trailing comma.

The behavior of build_list() seems more appropriate, since it allows
tools to generate the CSVs more easily.  Also, these lists are used to
represent user names or group names, so an empty user name makes no
sense.

This patch makes both functions be more consistent, which will
eventually allow us to de-duplicate code.

Signed-off-by: Alejandro Colomar <[email protected]>
get_gid() will fail to parse a number if the field is empty.

Signed-off-by: Alejandro Colomar <[email protected]>
This name tells better what this API does.

The name has a leading 'a', as it allocates memory, and then 'csv2ls'
because it parses a CSV into a list.

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