diff mbox series

[iproute2,2/3] nstat: use stack space for history file name

Message ID 20240228135858.3258-2-dkirjanov@suse.de (mailing list archive)
State Accepted
Commit 2f8b36e146a555fd6a96590bf191ac634e3b350b
Delegated to: David Ahern
Headers show
Series [iproute2,1/3] nstat: constify name argument in generic_proc_open | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Denis Kirjanov Feb. 28, 2024, 1:58 p.m. UTC
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>
---
 misc/nstat.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/misc/nstat.c b/misc/nstat.c
index 3a58885d..ea96ccb0 100644
--- a/misc/nstat.c
+++ b/misc/nstat.c
@@ -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);