src/: Use str2[u]l() instead of atoi(3)

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>
This commit is contained in:
Alejandro Colomar
2024-01-06 22:12:06 +01:00
parent 24695e6f38
commit 678c2a23ee
3 changed files with 26 additions and 10 deletions
+6 -2
View 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
+16 -7
View 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);
+4 -1
View 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