uci: libuci leaking memory on non-existent config file
[project/uci.git] / file.c
diff --git a/file.c b/file.c
index b471307cddf4308e7a19dc4150943c94054549c5..d104933e8d4f9fb30d7380c72a1a39938bca75f2 100644 (file)
--- a/file.c
+++ b/file.c
@@ -60,7 +60,10 @@ __private void uci_getln(struct uci_context *ctx, int offset)
                ofs += strlen(p);
                if (pctx->buf[ofs - 1] == '\n') {
                        pctx->line++;
-                       pctx->buf[ofs - 1] = 0;
+                       if (ofs >= 2 && pctx->buf[ofs - 2] == '\r')
+                               pctx->buf[ofs - 2] = 0;
+                       else
+                               pctx->buf[ofs - 1] = 0;
                        return;
                }
 
@@ -566,7 +569,7 @@ static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
                struct uci_section *sec = uci_to_section(s);
                fprintf(stream, "\nconfig %s", uci_escape(ctx, sec->type));
                if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
-                       fprintf(stream, " %s", uci_escape(ctx, sec->e.name));
+                       fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
                fprintf(stream, "\n");
                uci_foreach_element(&sec->options, o) {
                        struct uci_option *opt = uci_to_option(o);
@@ -834,9 +837,9 @@ static struct uci_package *uci_file_load(struct uci_context *ctx, const char *na
                break;
        }
 
+       UCI_TRAP_SAVE(ctx, done);
        file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
        ctx->err = 0;
-       UCI_TRAP_SAVE(ctx, done);
        UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
        UCI_TRAP_RESTORE(ctx);
 
@@ -848,8 +851,10 @@ static struct uci_package *uci_file_load(struct uci_context *ctx, const char *na
 
 done:
        uci_close_stream(file);
-       if (ctx->err)
+       if (ctx->err) {
+               free(filename);
                UCI_THROW(ctx, ctx->err);
+       }
        return package;
 }