util: Fix error path
authorHauke Mehrtens <hauke@hauke-m.de>
Fri, 1 Nov 2019 11:02:13 +0000 (12:02 +0100)
committerHauke Mehrtens <hauke@hauke-m.de>
Fri, 1 Nov 2019 15:58:14 +0000 (16:58 +0100)
Unlock and close the stream in case some file operations in
uci_open_stream() fail.

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
util.c

diff --git a/util.c b/util.c
index 12aec9bd3596103741b564198cd969e97207a165..8572e8130555c7aff49e8e8b578841b6ef387825 100644 (file)
--- a/util.c
+++ b/util.c
@@ -221,17 +221,21 @@ __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, c
 
        ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
        if ((ret < 0) && (errno != ENOSYS))
-               goto error;
+               goto error_close;
 
        ret = lseek(fd, 0, pos);
 
        if (ret < 0)
-               goto error;
+               goto error_unlock;
 
        file = fdopen(fd, (write ? "w+" : "r"));
        if (file)
                goto done;
 
+error_unlock:
+       flock(fd, LOCK_UN);
+error_close:
+       close(fd);
 error:
        UCI_THROW(ctx, UCI_ERR_IO);
 done: