]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
src/: Use str2[u]l() instead of atoi(3)
authorAlejandro Colomar <alx@kernel.org>
Sat, 6 Jan 2024 21:12:06 +0000 (22:12 +0100)
committerAlejandro Colomar <alx@kernel.org>
Sat, 29 Jun 2024 18:00:18 +0000 (20:00 +0200)
atoi(3) easily triggers Undefined Behavior.  Replace it by str2[u]l(),
which are safe from that, and add type safety too.

Reviewed-by: "Serge E. Hallyn" <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
src/free_subid_range.c
src/get_subid_owners.c
src/new_subid_range.c

index 441c22772b2eb9b1411a08234d9c4225cb46915e..4bdacfb7dc76673617ab9478f00505f59f6bf303 100644 (file)
@@ -1,12 +1,16 @@
 /* SPDX-License-Identifier: BSD-3-Clause */
 
+
 #include <stdio.h>
 #include <unistd.h>
+
+#include "atoi/str2i.h"
 #include "subid.h"
 #include "stdlib.h"
 #include "prototypes.h"
 #include "shadowlog.h"
 
+
 /* Test program for the subid freeing routine */
 
 static const char Prog[] = "free_subid_range";
@@ -38,8 +42,8 @@ int main(int argc, char *argv[])
        if (argc < 3)
                usage();
        range.owner = argv[0];
-       range.start = atoi(argv[1]);
-       range.count = atoi(argv[2]);
+       str2ul(&range.start, argv[1]);
+       str2ul(&range.count, argv[2]);
        if (group)
                ok = subid_ungrant_gid_range(&range);
        else
index e1c1e795eae9086181b39190e7f3e0d895ea030a..9fe07b62182325517abf62a6b87afd7e0bd0b8e0 100644 (file)
@@ -1,13 +1,18 @@
 /* SPDX-License-Identifier: BSD-3-Clause */
 
+
 #include <stdio.h>
+
+#include "atoi/str2i.h"
 #include "subid.h"
 #include "stdlib.h"
 #include "prototypes.h"
 #include "shadowlog.h"
 
+
 static const char Prog[] = "get_subid_owners";
 
+
 static void usage(void)
 {
        fprintf(stderr, "Usage: [-g] %s subuid\n", Prog);
@@ -18,20 +23,24 @@ static void usage(void)
 
 int main(int argc, char *argv[])
 {
-       int i, n;
-       uid_t *uids;
+       int    i, n;
+       long   l;
+       uid_t  *uids;
 
        log_set_progname(Prog);
        log_set_logfd(stderr);
        if (argc < 2) {
                usage();
        }
-       if (argc == 3 && strcmp(argv[1], "-g") == 0)
-               n = subid_get_gid_owners(atoi(argv[2]), &uids);
-       else if (argc == 2 && strcmp(argv[1], "-h") == 0)
+       if (argc == 3 && strcmp(argv[1], "-g") == 0) {
+               str2sl(&l, argv[2]);
+               n = subid_get_gid_owners(l, &uids);
+       } else if (argc == 2 && strcmp(argv[1], "-h") == 0) {
                usage();
-       else
-               n = subid_get_uid_owners(atoi(argv[1]), &uids);
+       } else {
+               str2sl(&l, argv[1]);
+               n = subid_get_uid_owners(l, &uids);
+       }
        if (n < 0) {
                fprintf(stderr, "No owners found\n");
                exit(1);
index 1ef71f36e7554201612af7afc09d99e5a5c948a1..6be957409c9d1857674bd26bfbba1a85e2ab8ab5 100644 (file)
@@ -2,11 +2,14 @@
 
 #include <stdio.h>
 #include <unistd.h>
+
+#include "atoi/str2i.h"
 #include "subid.h"
 #include "stdlib.h"
 #include "prototypes.h"
 #include "shadowlog.h"
 
+
 /* Test program for the subid creation routine */
 
 static const char Prog[] = "new_subid_range";
@@ -45,7 +48,7 @@ int main(int argc, char *argv[])
        range.start = 0;
        range.count = 65536;
        if (argc > 1)
-               range.count = atoi(argv[1]);
+               str2ul(&range.count, argv[1]);
        if (group)
                ok = subid_grant_gid_range(&range, !makenew);
        else