From 3341c830c89dd7aac41deda2059b2f7a85e74548 Mon Sep 17 00:00:00 2001 From: Zygmunt Krynicki Date: Wed, 26 Jun 2019 19:02:48 +0200 Subject: cmd/libsnap: fix cleanup and error compatiblity Some variables have automatic cleanup handlers. Some other variables need special paired function calls (va_start, va_end). This makes is more challenging to use "goto out" error handling strategy. Fix the mistakes I made earlier. Signed-off-by: Zygmunt Krynicki --- cmd/libsnap-confine-private/infofile.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cmd/libsnap-confine-private/infofile.c b/cmd/libsnap-confine-private/infofile.c index 45e9d994d0..eb2e850562 100644 --- a/cmd/libsnap-confine-private/infofile.c +++ b/cmd/libsnap-confine-private/infofile.c @@ -30,6 +30,8 @@ int sc_infofile_query(FILE *stream, sc_error **err_out, ...) { va_list ap; va_start(ap, err_out); sc_error *err = NULL; + size_t line_size = 0; + char *line_buf SC_CLEANUP(sc_cleanup_string) = NULL; fpos_t start_pos; if (fgetpos(stream, &start_pos) < 0) { @@ -37,8 +39,6 @@ int sc_infofile_query(FILE *stream, sc_error **err_out, ...) { goto out; } - size_t line_size = 0; - char *line_buf SC_CLEANUP(sc_cleanup_string) = NULL; for (;;) { /* This loop advances through the keys we are looking for. */ const char *key = va_arg(ap, const char *); if (key == NULL) { @@ -65,6 +65,8 @@ int sc_infofile_query(FILE *stream, sc_error **err_out, ...) { if (nread <= 0) { break; /* There is nothing more to read. */ } + /* NOTE: beyond this line the buffer is never empty. */ + /* Guard against malformed input that may contain NUL bytes that * would confuse the code below. */ if (memchr(line_buf, '\0', nread) != NULL) { @@ -85,7 +87,7 @@ int sc_infofile_query(FILE *stream, sc_error **err_out, ...) { continue; } /* Replace the newline character, if any, with the NUL byte. */ - if (nread > 0 && line_buf[nread - 1] == '\n') { + if (line_buf[nread - 1] == '\n') { line_buf[nread - 1] = '\0'; nread--; } @@ -98,13 +100,12 @@ int sc_infofile_query(FILE *stream, sc_error **err_out, ...) { } } } - va_end(ap); if (fsetpos(stream, &start_pos) < 0) { err = sc_error_init_from_errno(errno, "cannot set stream position"); goto out; } - out: + va_end(ap); sc_error_forward(err_out, err); return err != NULL ? -1 : 0; } -- cgit v1.2.3