summaryrefslogtreecommitdiff
path: root/cmd/libsnap-confine-private
diff options
authorZygmunt Krynicki <me@zygoon.pl>2019-06-26 19:02:48 +0200
committerZygmunt Krynicki <me@zygoon.pl>2019-06-26 19:02:48 +0200
commit3341c830c89dd7aac41deda2059b2f7a85e74548 (patch)
tree6780c4e33a18c789ff2555355a733d0285988ae3 /cmd/libsnap-confine-private
parent936be97b41f6c803f18cf6527f6d310b28a15749 (diff)
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 <me@zygoon.pl>
Diffstat (limited to 'cmd/libsnap-confine-private')
-rw-r--r--cmd/libsnap-confine-private/infofile.c11
1 files 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;
}