summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetr Štetiar <ynezz@true.cz>2020-10-03 09:14:35 +0200
committerPetr Štetiar <ynezz@true.cz>2020-10-03 09:46:18 +0200
commitaa46546794acf5fb1f1f9bfb15152ea007e2c3c1 (patch)
treec8b8ac42168d2c1e7eee97b6f58e2fa2968dcdbe
parent671c7554bfdeab758f605d1e1a2420117e96a628 (diff)
downloaduci-aa46546794acf5fb1f1f9bfb15152ea007e2c3c1.tar.gz
file: uci_file_commit: fix memory leak
Fixes following memory leak: 26 bytes in 1 blocks are definitely lost in loss record 1 of 1 at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x52DA68F: vasprintf (vasprintf.c:73) by 0x52B71D3: asprintf (asprintf.c:35) by 0x4E40F67: uci_file_commit (file.c:738) by 0x4E3FD94: uci_commit (libuci.c:193) by 0x401ED9: uci_do_import (cli.c:408) by 0x401ED9: uci_cmd (cli.c:685) by 0x4016FA: main (cli.c:776) Signed-off-by: Petr Štetiar <ynezz@true.cz>
-rw-r--r--file.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/file.c b/file.c
index 23bf49a..5502a42 100644
--- a/file.c
+++ b/file.c
@@ -33,6 +33,10 @@
#include "uci.h"
#include "uci_internal.h"
+#ifndef MAX_PATH
+#define MAX_PATH 4096
+#endif
+
#define LINEBUF 32
/*
@@ -723,7 +727,7 @@ 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 = NULL;
+ char filename[MAX_PATH] = {0};
struct stat statbuf;
volatile bool do_rename = false;
int fd;
@@ -735,7 +739,7 @@ static void uci_file_commit(struct uci_context *ctx, struct uci_package **packag
UCI_THROW(ctx, UCI_ERR_INVAL);
}
- if ((asprintf(&filename, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name) < 0) || !filename)
+ if (snprintf(filename, MAX_PATH, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name) < 0)
UCI_THROW(ctx, UCI_ERR_MEM);
/* open the config file for writing now, so that it is locked */
@@ -808,7 +812,6 @@ done:
}
free(path);
}
- free(filename);
if (ctx->err)
UCI_THROW(ctx, ctx->err);
}