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 5306b56c67
commit 1860d3897d
3 changed files with 19 additions and 9 deletions

View File

@@ -190,6 +190,7 @@ int crypt_keyfile_device_read(struct crypt_device *cd, const char *keyfile,
size_t buflen, i;
uint64_t file_read_size;
struct stat st;
bool close_fd = false;
if (!key || !key_size_read)
return -EINVAL;
@@ -197,11 +198,15 @@ int crypt_keyfile_device_read(struct crypt_device *cd, const char *keyfile,
*key = NULL;
*key_size_read = 0;
fd = keyfile ? open(keyfile, O_RDONLY) : STDIN_FILENO;
if (fd < 0) {
log_err(cd, _("Failed to open key file."));
return -EINVAL;
}
if (keyfile) {
fd = open(keyfile, O_RDONLY);
if (fd < 0) {
log_err(cd, _("Failed to open key file."));
return -EINVAL;
}
close_fd = true;
} else
fd = STDIN_FILENO;
if (isatty(fd)) {
log_err(cd, _("Cannot read keyfile from a terminal."));
@@ -315,7 +320,7 @@ int crypt_keyfile_device_read(struct crypt_device *cd, const char *keyfile,
*key_size_read = i;
r = 0;
out:
if (fd != STDIN_FILENO)
if (close_fd)
close(fd);
if (r)