From 8bb05ac73a5b448b339ce0bc8d396c82c459b47f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 21:33:32 +0200 Subject: Merging upstream version 2.40. Signed-off-by: Daniel Baumann --- libmount/src/utils.c | 99 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 79 insertions(+), 20 deletions(-) (limited to 'libmount/src/utils.c') diff --git a/libmount/src/utils.c b/libmount/src/utils.c index 3817b39..a2f8ea0 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -526,7 +526,7 @@ static int add_filesystem(char ***filesystems, char *name) if (n == 0 || !((n + 1) % MYCHUNK)) { size_t items = ((n + 1 + MYCHUNK) / MYCHUNK) * MYCHUNK; - char **x = realloc(*filesystems, items * sizeof(char *)); + char **x = reallocarray(*filesystems, items, sizeof(char *)); if (!x) goto err; @@ -1032,7 +1032,7 @@ int mnt_open_uniq_filename(const char *filename, char **name) rc = asprintf(&n, "%s.XXXXXX", filename); if (rc <= 0) - return -errno; + return -ENOMEM; /* This is for very old glibc and for compatibility with Posix, which says * nothing about mkstemp() mode. All sane glibc use secure mode (0600). @@ -1134,7 +1134,7 @@ char *mnt_get_kernel_cmdline_option(const char *name) int val = 0; char *p, *res = NULL, *mem = NULL; char buf[BUFSIZ]; /* see kernel include/asm-generic/setup.h: COMMAND_LINE_SIZE */ - const char *path = _PATH_PROC_CMDLINE; + const char *path; if (!name || !name[0]) return NULL; @@ -1143,6 +1143,8 @@ char *mnt_get_kernel_cmdline_option(const char *name) path = getenv("LIBMOUNT_KERNEL_CMDLINE"); if (!path) path = _PATH_PROC_CMDLINE; +#else + path = _PATH_PROC_CMDLINE; #endif f = fopen(path, "r" UL_CLOEXECSTR); if (!f) @@ -1287,8 +1289,12 @@ done: } #ifdef TEST_PROGRAM -static int test_match_fstype(struct libmnt_test *ts, int argc, char *argv[]) +static int test_match_fstype(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 3) + return -1; + char *type = argv[1]; char *pattern = argv[2]; @@ -1296,8 +1302,12 @@ static int test_match_fstype(struct libmnt_test *ts, int argc, char *argv[]) return 0; } -static int test_match_options(struct libmnt_test *ts, int argc, char *argv[]) +static int test_match_options(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 3) + return -1; + char *optstr = argv[1]; char *pattern = argv[2]; @@ -1305,8 +1315,12 @@ static int test_match_options(struct libmnt_test *ts, int argc, char *argv[]) return 0; } -static int test_startswith(struct libmnt_test *ts, int argc, char *argv[]) +static int test_startswith(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 3) + return -1; + char *optstr = argv[1]; char *pattern = argv[2]; @@ -1314,8 +1328,12 @@ static int test_startswith(struct libmnt_test *ts, int argc, char *argv[]) return 0; } -static int test_endswith(struct libmnt_test *ts, int argc, char *argv[]) +static int test_endswith(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 3) + return -1; + char *optstr = argv[1]; char *pattern = argv[2]; @@ -1323,8 +1341,12 @@ static int test_endswith(struct libmnt_test *ts, int argc, char *argv[]) return 0; } -static int test_mountpoint(struct libmnt_test *ts, int argc, char *argv[]) +static int test_mountpoint(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 2) + return -1; + char *path = canonicalize_path(argv[1]), *mnt = path ? mnt_get_mountpoint(path) : NULL; @@ -1334,13 +1356,17 @@ static int test_mountpoint(struct libmnt_test *ts, int argc, char *argv[]) return 0; } -static int test_filesystems(struct libmnt_test *ts, int argc, char *argv[]) +static int test_filesystems(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { char **filesystems = NULL; int rc; - rc = mnt_get_filesystems(&filesystems, argc ? argv[1] : NULL); - if (!rc) { + if (argc != 1 && argc != 2) + return -1; + + rc = mnt_get_filesystems(&filesystems, argc == 2 ? argv[1] : NULL); + if (!rc && filesystems) { char **p; for (p = filesystems; *p; p++) printf("%s\n", *p); @@ -1349,8 +1375,12 @@ static int test_filesystems(struct libmnt_test *ts, int argc, char *argv[]) return rc; } -static int test_chdir(struct libmnt_test *ts, int argc, char *argv[]) +static int test_chdir(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 2) + return -1; + int rc; char *path = canonicalize_path(argv[1]), *last = NULL; @@ -1368,8 +1398,12 @@ static int test_chdir(struct libmnt_test *ts, int argc, char *argv[]) return rc; } -static int test_kernel_cmdline(struct libmnt_test *ts, int argc, char *argv[]) +static int test_kernel_cmdline(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 2) + return -1; + char *name = argv[1]; char *res; @@ -1387,7 +1421,8 @@ static int test_kernel_cmdline(struct libmnt_test *ts, int argc, char *argv[]) } -static int test_guess_root(struct libmnt_test *ts, int argc, char *argv[]) +static int test_guess_root(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { int rc; char *real; @@ -1413,10 +1448,14 @@ static int test_guess_root(struct libmnt_test *ts, int argc, char *argv[]) return 0; } -static int test_mkdir(struct libmnt_test *ts, int argc, char *argv[]) +static int test_mkdir(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { int rc; + if (argc != 2) + return -1; + rc = ul_mkdir_p(argv[1], S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); @@ -1425,11 +1464,15 @@ static int test_mkdir(struct libmnt_test *ts, int argc, char *argv[]) return rc; } -static int test_statfs_type(struct libmnt_test *ts, int argc, char *argv[]) +static int test_statfs_type(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { struct statfs vfs; int rc; + if (argc != 2) + return -1; + rc = statfs(argv[1], &vfs); if (rc) printf("%s: statfs failed: %m\n", argv[1]); @@ -1440,8 +1483,12 @@ static int test_statfs_type(struct libmnt_test *ts, int argc, char *argv[]) return rc; } -static int tests_parse_uid(struct libmnt_test *ts, int argc, char *argv[]) +static int tests_parse_uid(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 2) + return -1; + char *str = argv[1]; uid_t uid = (uid_t) -1; int rc; @@ -1455,8 +1502,12 @@ static int tests_parse_uid(struct libmnt_test *ts, int argc, char *argv[]) return rc; } -static int tests_parse_gid(struct libmnt_test *ts, int argc, char *argv[]) +static int tests_parse_gid(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 2) + return -1; + char *str = argv[1]; gid_t gid = (gid_t) -1; int rc; @@ -1470,8 +1521,12 @@ static int tests_parse_gid(struct libmnt_test *ts, int argc, char *argv[]) return rc; } -static int tests_parse_mode(struct libmnt_test *ts, int argc, char *argv[]) +static int tests_parse_mode(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 2) + return -1; + char *str = argv[1]; mode_t mod = (mode_t) -1; int rc; @@ -1488,8 +1543,12 @@ static int tests_parse_mode(struct libmnt_test *ts, int argc, char *argv[]) return rc; } -static int tests_stat(struct libmnt_test *ts, int argc, char *argv[]) +static int tests_stat(struct libmnt_test *ts __attribute__((unused)), + int argc, char *argv[]) { + if (argc != 2) + return -1; + char *path = argv[1]; struct stat st; int rc; -- cgit v1.2.3