commit 20e44f4bcc2bda061dc801c820345628e05b5900
parent 35bad530f5318c4d256be192b17b09d8608b1404
Author: Michael Forney <mforney@mforney.org>
Date: Tue, 17 Mar 2026 02:38:51 -0700
tar: avoid unnecessary VLA
An assignment expression is not a constant expression, so while the
length of fname is always constant, it becomes a VLA.
The `l` variable never changes, so just use `sizeof fname` for
clarity.
Diffstat:
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/tar.c b/tar.c
@@ -471,8 +471,8 @@ bad:
static void
xt(int argc, char *argv[], int mode)
{
- long size, l;
- char b[BLKSIZ], fname[l = PATH_MAX + 1], *p, *q = NULL;
+ long size;
+ char b[BLKSIZ], fname[PATH_MAX + 1], *p, *q = NULL;
int i, m, n;
int (*fn)(char *, ssize_t, char[BLKSIZ]) = (mode == 'x') ? unarchive : print;
struct timespec times[2];
@@ -491,7 +491,7 @@ xt(int argc, char *argv[], int mode)
/* Read header only up to size of fname buffer */
for (q = fname; q < fname+size; q += BLKSIZ) {
- if (q + BLKSIZ >= fname + l)
+ if (q + BLKSIZ >= fname + sizeof fname)
eprintf("name exceeds buffer: %.*s\n", q-fname, fname);
eread(tarfd, q, BLKSIZ);
}
@@ -518,7 +518,7 @@ xt(int argc, char *argv[], int mode)
if (!q) {
m = sizeof h->prefix, n = sizeof h->name;
p = "/" + !h->prefix[0];
- snprintf(fname, l, "%.*s%s%.*s", m, h->prefix, p, n, h->name);
+ snprintf(fname, sizeof fname, "%.*s%s%.*s", m, h->prefix, p, n, h->name);
}
q = NULL;