file: use dynamic memory allocation for tempfile name
[project/uci.git] / file.c
diff --git a/file.c b/file.c
index 23bf49a16f631070b55a0909c733a167b432dfb4..ce1acb2024bb1b1b7d7ea560879d7096a98f2304 100644 (file)
--- a/file.c
+++ b/file.c
@@ -726,7 +726,7 @@ static void uci_file_commit(struct uci_context *ctx, struct uci_package **packag
        char *filename = NULL;
        struct stat statbuf;
        volatile bool do_rename = false;
-       int fd;
+       int fd, sz;
 
        if (!p->path) {
                if (overwrite)
@@ -735,8 +735,9 @@ 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)
-               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);
@@ -808,7 +809,6 @@ done:
                }
                free(path);
        }
-       free(filename);
        if (ctx->err)
                UCI_THROW(ctx, ctx->err);
 }