[OpenWrt-Devel] [PATCH uci 1/2] util: Fix error path

Hauke Mehrtens hauke at hauke-m.de
Fri Nov 1 12:06:33 EDT 2019


Unlock and close the stream in case some file operations in
uci_open_stream() fail.

Signed-off-by: Hauke Mehrtens <hauke at hauke-m.de>
---
 util.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/util.c b/util.c
index 12aec9b..8572e81 100644
--- 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:
-- 
2.20.1


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list