From: Thomas Weißschuh Date: Wed, 27 Sep 2023 19:03:08 +0000 (+0200) Subject: treewide: use reallocarray to allocated memory that will be reallocated X-Git-Tag: v2.40-rc1~222^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=343b2e4bf9ceccfc34edc914e06dcde6d0d01919;p=thirdparty%2Futil-linux.git treewide: use reallocarray to allocated memory that will be reallocated 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 --- diff --git a/login-utils/lslogins.c b/login-utils/lslogins.c index 1835e086a6..b089c51c76 100644 --- a/login-utils/lslogins.c +++ b/login-utils/lslogins.c @@ -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++) { diff --git a/misc-utils/logger.c b/misc-utils/logger.c index 6d21fcae18..5fe8b2ac6c 100644 --- a/misc-utils/logger.c +++ b/misc-utils/logger.c @@ -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); diff --git a/text-utils/more.c b/text-utils/more.c index bd19d849ac..d4db3d5eb9 100644 --- a/text-utils/more.c +++ b/text-utils/more.c @@ -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) { diff --git a/text-utils/rev.c b/text-utils/rev.c index 020f28a44f..bfc4e804b6 100644 --- a/text-utils/rev.c +++ b/text-utils/rev.c @@ -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) {