summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-10-06 08:06:16 +0200
committerPetr Štetiar <ynezz@true.cz>2020-10-06 08:08:10 +0200
commit19770b6949b9a95038f3216da773204eadfdd6bc (patch)
treeb59693f90fe4136eaacf6f0dca3a9585087b1374
parentaa46546794acf5fb1f1f9bfb15152ea007e2c3c1 (diff)
downloaduci-19770b6949b9a95038f3216da773204eadfdd6bc.tar.gz
file: use dynamic memory allocation for tempfile name
Allocating a 4KB stack space buffer just for formatting a tempfile name does not seem ideal. Fixes: aa46546794ac ("file: uci_file_commit: fix memory leak") Signed-off-by: Jo-Philipp Wich <jo@mein.io> Signed-off-by: Petr Štetiar <ynezz@true.cz>
-rw-r--r--file.c13
1 files changed, 5 insertions, 8 deletions
diff --git a/file.c b/file.c
index 5502a42..ce1acb2 100644
--- a/file.c
+++ b/file.c
@@ -33,10 +33,6 @@
#include "uci.h"
#include "uci_internal.h"
-#ifndef MAX_PATH
-#define MAX_PATH 4096
-#endif
-
#define LINEBUF 32
/*
@@ -727,10 +723,10 @@ static void uci_file_commit(struct uci_context *ctx, struct uci_package **packag
FILE *f1, *f2 = NULL;
char *volatile name = NULL;
char *volatile path = NULL;
- char filename[MAX_PATH] = {0};
+ char *filename = NULL;
struct stat statbuf;
volatile bool do_rename = false;
- int fd;
+ int fd, sz;
if (!p->path) {
if (overwrite)
@@ -739,8 +735,9 @@ static void uci_file_commit(struct uci_context *ctx, struct uci_package **packag
UCI_THROW(ctx, UCI_ERR_INVAL);
}
- if (snprintf(filename, MAX_PATH, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name) < 0)
- UCI_THROW(ctx, UCI_ERR_MEM);
+ sz = snprintf(NULL, 0, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name);
+ filename = alloca(sz + 1);
+ snprintf(filename, sz + 1, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name);
/* open the config file for writing now, so that it is locked */
f1 = uci_open_stream(ctx, p->path, NULL, SEEK_SET, true, true);