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

View File

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

View File

@@ -160,6 +160,7 @@ static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
int failed = -1; int failed = -1;
int infd, outfd; int infd, outfd;
size_t realsize = 0; size_t realsize = 0;
bool close_fd = false;
if (maxlen < 1) if (maxlen < 1)
return failed; return failed;
@@ -169,8 +170,10 @@ static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
if (infd == -1) { if (infd == -1) {
infd = STDIN_FILENO; infd = STDIN_FILENO;
outfd = STDERR_FILENO; outfd = STDERR_FILENO;
} else } else {
outfd = infd; outfd = infd;
close_fd = true;
}
if (tcgetattr(infd, &orig)) if (tcgetattr(infd, &orig))
goto out; goto out;
@@ -193,7 +196,7 @@ out:
if (realsize == maxlen) if (realsize == maxlen)
log_dbg("Read stopped at maximal interactive input length, passphrase can be trimmed."); log_dbg("Read stopped at maximal interactive input length, passphrase can be trimmed.");
if (infd != STDIN_FILENO) if (close_fd)
close(infd); close(infd);
return failed; return failed;
} }