Fix leaked fd gcc analyzer warning.

These are actually false positives (fd 0 is always open as stdin),
but code is even more readable with this fix.
This commit is contained in:
Milan Broz
2024-07-09 23:13:04 +02:00
parent b1cc660df3
commit b0be186f9e
3 changed files with 19 additions and 9 deletions

View File

@@ -164,6 +164,7 @@ int tools_read_json_file(const char *file, char **json, size_t *json_size, bool
ssize_t ret;
int fd, block, r;
void *buf = NULL;
bool close_fd = false;
block = tools_signals_blocked();
if (block)
@@ -180,6 +181,7 @@ int tools_read_json_file(const char *file, char **json, size_t *json_size, bool
r = -EINVAL;
goto out;
}
close_fd = true;
}
buf = malloc(LUKS2_MAX_MDA_SIZE);
@@ -211,7 +213,7 @@ int tools_read_json_file(const char *file, char **json, size_t *json_size, bool
out:
if (block && !quit)
set_int_block(1);
if (fd >= 0 && fd != STDIN_FILENO)
if (close_fd)
close(fd);
if (r && buf) {
memset(buf, 0, LUKS2_MAX_MDA_SIZE);