Browse Source

util: Fix error path

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

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
Hauke Mehrtens 4 years ago
parent
commit
2c8e4a347b
1 changed files with 6 additions and 2 deletions
  1. 6 2
      util.c

+ 6 - 2
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: