-
Notifications
You must be signed in to change notification settings - Fork 241
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
Simplify #1109
Simplify #1109
Conversation
It's amazing how much could be removed just from that function. :) |
ac58bc2
to
7622cc1
Compare
I do not be a 3-star programmer! :-) commit 1abba41f37c14187cff53892300a7be49b781c5e (HEAD -> S, gh/S)
Author: Alejandro Colomar <alx@kernel.org>
Date: Tue Nov 5 15:13:35 2024 +0100
lib/gshadow.c: build_list(): Remove second parameter
We've simplified the function so much in the previous commits, that now
$2 is rather useless. It only sets the output parameter to the same
value that the function returns. It's simpler if the caller just sets
it itself after the call.
This removes the only 3-star pointer type declaration in the entire
project. :)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
diff --git a/lib/gshadow.c b/lib/gshadow.c
index 5caf4f16..7de52aaf 100644
--- a/lib/gshadow.c
+++ b/lib/gshadow.c
@@ -35,7 +35,7 @@ static struct sgrp sgroup;
static /*@null@*/char **
-build_list(char *s, char ***lp)
+build_list(char *s)
{
char **l;
size_t n;
@@ -51,8 +51,6 @@ build_list(char *s, char ***lp)
l = XREALLOC(l, n + 1, char *);
l[n] = NULL;
- *lp = l;
-
return l;
}
@@ -119,8 +117,8 @@ sgetsgent(const char *string)
free(admins);
free(members);
- sgroup.sg_adm = build_list(fields[2], &admins);
- sgroup.sg_mem = build_list(fields[3], &members);
+ sgroup.sg_adm = admins = build_list(fields[2]);
+ sgroup.sg_mem = members = build_list(fields[3]);
return &sgroup;
} |
3135946
to
28e8a8c
Compare
This comment was marked as outdated.
This comment was marked as outdated.
|
f30e83a
to
483bd5f
Compare
74ded0e
to
e70946e
Compare
list cannot be NULL in the first iteration, so we don't need a do-while. Just in case it's not obvious: we know it's not NULL in the first iteration because right above, in line 772, we've already dereferenced it. Signed-off-by: Alejandro Colomar <alx@kernel.org>
list ($2) is a pointer to a list of strings. We were declaring it as an array of pointers to strings, which was bogus. It worked out of luck, because array parameters are transformed into pointers by the compiler, but it was incorrect. Just look at how we're calling this function. $ grep build_list lib/gshadow.c build_list(char *s, char ***list, size_t *nlist) sgroup.sg_adm = build_list (fields[2], &admins, &nadmins); sgroup.sg_mem = build_list (fields[3], &members, &nmembers); $ grep '^static .*\<admins\>' lib/gshadow.c static /*@null@*//*@only@*/char **admins = NULL; $ grep '^static .*\<members\>' lib/gshadow.c static /*@null@*//*@only@*/char **members = NULL; Fixes: 8e167d2 ("[svn-upgrade] Integrating new upstream version, shadow (4.0.8)") Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
It was hard to understand what each variable is. Use a consistent scheme, where a 'p' means a pointer, 'l' means list, and 'n' means number of elements. Those should be obvious from the name of the function and the context, and will make it easier to read the code. Also, the shorter names will allow focusing on the rest of the code. Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
If n was 0, it doesn't hurt to set it again to 0; and the list would be NULL, so it doesn't hurt free(3)ing it and setting to NULL again either. Signed-off-by: Alejandro Colomar <alx@kernel.org>
This makes build_list() less dependent on the context. It starts from clean, whatever the state before the call was. I was having a hard time understanding the reallocation, until I saw that we were zeroing everything right before the call. Signed-off-by: Alejandro Colomar <alx@kernel.org>
Nothing is using that value outside of build_list(). Keep it as an local variable. Signed-off-by: Alejandro Colomar <alx@kernel.org>
Use instead automatic variables as much as possible. This reduces the number of dereferences, enhancing readability. Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
0 is a horrible null-pointer constant. Don't use it. Especially, when just a few lines above, in the same function, we've used NULL for the same thing. Signed-off-by: Alejandro Colomar <alx@kernel.org>
We've simplified the function so much in the previous commits, that now $2 is rather useless. It only sets the output parameter to the same value that the function returns. It's simpler if the caller just sets it itself after the call. This removes the only 3-star pointer in the entire project. :) Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Instead of reallocating 1 more meber per iteration, calculate the total amount that we want by counting the number of commas (delimiters) in the string, plus one for the last element, plus one for the terminating NULL. This might result in overallocation of one element if the string is an empty string, or if there's a trailing comma; however, that's not an issue. We can afford overallocating one element in certain cases, and we get in exchange a much simpler function. Signed-off-by: Alejandro Colomar <alx@kernel.org>
And 'n' is now an iterator. Rename it to 'i' as usual. Signed-off-by: Alejandro Colomar <alx@kernel.org>
I laughed, I cried, it was more suspenseful than war and peace. |
Heh, it was very fun to write this patch set/story. At any point you would believe that the function would be so simple after all. :-) |
Revisions:
v1b
v2
v3
v4
v5
v6
v7
v7b
v8
v8b
v8c
v9
v9b
v10
v10b
v10c
v10d
v10e
v10f