]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
treewide: use reallocarray to allocated memory that will be reallocated
authorThomas Weißschuh <thomas@t-8ch.de>
Wed, 27 Sep 2023 19:03:08 +0000 (21:03 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Thu, 28 Sep 2023 06:10:59 +0000 (08:10 +0200)
GCC 13 -fanalyzer expects memory that is passed to reallocarray() to
have been allocated with reallocarray() in the first place and not with
malloc().

While this requirement seems to contradict the documentation, the usage
of reallocarray() is better than the current plain malloc() in any case.

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
login-utils/lslogins.c
misc-utils/logger.c
text-utils/more.c
text-utils/rev.c

index 1835e086a62b61e63fc3e60a8a38a2ce54afe9b4..b089c51c760feee09724b00ecc1e63800aded5e9 100644 (file)
@@ -492,7 +492,7 @@ static int parse_utmpx(const char *path, size_t *nrecords, struct utmpx **record
         * just fallback only */
        if (stat(path, &st) == 0 && (size_t) st.st_size >= sizeof(struct utmpx)) {
                imax = st.st_size / sizeof(struct utmpx);
-               ary = xmalloc(imax * sizeof(struct utmpx));
+               ary = xreallocarray(NULL, imax, sizeof(struct utmpx));
        }
 
        for (i = 0; ; i++) {
index 6d21fcae18fb979b8de0dd3ca7eb5fb93ef8387b..5fe8b2ac6c9a0e1a29b3bd7d8844225130b29943 100644 (file)
@@ -343,7 +343,7 @@ static int journald_entry(struct logger_ctl *ctl, FILE *fp)
        int n, lines = 0, vectors = 8, ret = 0, msgline = -1;
        size_t dummy = 0;
 
-       iovec = xmalloc(vectors * sizeof(struct iovec));
+       iovec = xreallocarray(NULL, vectors, sizeof(struct iovec));
        while (1) {
                buf = NULL;
                sz = getline(&buf, &dummy, fp);
index bd19d849acc3a21c9f30f0115b2052c1fe041dce..d4db3d5eb9f18a7e68a179fc6cb2c4370301361d 100644 (file)
@@ -353,7 +353,7 @@ static void env_argscan(struct more_control *ctl, const char *s)
        char *str = xstrdup(s);
        char *key = NULL, *tok;
 
-       env_argv = xmalloc(sizeof(char *) * size);
+       env_argv = xreallocarray(NULL, size, sizeof(char *));
        env_argv[0] = _("MORE environment variable");   /* program name */
        for (tok = strtok_r(str, delim, &key); tok; tok = strtok_r(NULL, delim, &key)) {
                if (size == env_argc) {
index 020f28a44f3d2331655119df2d4418f3b05b4d56..bfc4e804b66a5bd3e4de1406dfba228ad037e848 100644 (file)
@@ -157,7 +157,7 @@ int main(int argc, char *argv[])
        argc -= optind;
        argv += optind;
 
-       buf = xmalloc(bufsiz * sizeof(wchar_t));
+       buf = xreallocarray(NULL, bufsiz, sizeof(wchar_t));
 
        do {
                if (*argv) {