]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
nstat: use stack space for history file name
authorDenis Kirjanov <kirjanov@gmail.com>
Wed, 28 Feb 2024 13:58:57 +0000 (08:58 -0500)
committerDavid Ahern <dsahern@kernel.org>
Sun, 3 Mar 2024 22:32:46 +0000 (22:32 +0000)
as the name doesn't require a lot of storage put
it on the stack. Moreover the memory allocated via
malloc wasn't returned.

Signed-off-by: Denis Kirjanov <dkirjanov@suse.de>
Signed-off-by: David Ahern <dsahern@kernel.org>
misc/nstat.c

index 3a58885d994a35433c69607b91137b0f6e19481a..ea96ccb03a2ea534ccaeda952e7c3619c59305a9 100644 (file)
@@ -580,7 +580,7 @@ static const struct option longopts[] = {
 
 int main(int argc, char *argv[])
 {
-       char *hist_name;
+       char hist_name[128];
        struct sockaddr_un sun;
        FILE *hist_fp = NULL;
        int ch;
@@ -668,10 +668,11 @@ int main(int argc, char *argv[])
        patterns = argv;
        npatterns = argc;
 
-       if ((hist_name = getenv("NSTAT_HISTORY")) == NULL) {
-               hist_name = malloc(128);
-               sprintf(hist_name, "/tmp/.nstat.u%d", getuid());
-       }
+       if (getenv("NSTAT_HISTORY"))
+               snprintf(hist_name, sizeof(hist_name),
+                        "%s", getenv("NSTAT_HISTORY"));
+       else
+               snprintf(hist_name, sizeof(hist_name), "/tmp/.nstat.u%d", getuid());
 
        if (reset_history)
                unlink(hist_name);