commit dbbe267d715212cf0303dc82fdab34bc7202ff20
parent eed0090e32bae484c66d4b0892c78abe4858e54f
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Tue, 15 Aug 2017 12:51:01 +0200
files: don't close a file descriptor when opening failed [coverity scan]
Also, don't depend on statting the relative path, because if that would
fail, we would try to open a NULL pointer.
Diffstat:
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/src/files.c b/src/files.c
@@ -686,32 +686,32 @@ bool close_buffer(void)
* warnings. */
int is_file_writable(const char *filename)
{
- struct stat fileinfo, fileinfo2;
+ char *full_filename;
+ struct stat fileinfo;
int fd;
FILE *f;
- char *full_filename;
bool result = TRUE;
if (ISSET(VIEW_MODE))
return TRUE;
- /* Get the specified file's full path. */
full_filename = get_full_path(filename);
- /* Okay, if we can't stat the absolute path due to some component's
- * permissions, just try the relative one. */
- if (full_filename == NULL ||
- (stat(full_filename, &fileinfo) == -1 && stat(filename, &fileinfo2) != -1))
+ /* If the absolute path is unusable, use the given relative one. */
+ if (full_filename == NULL || stat(full_filename, &fileinfo) == -1)
full_filename = mallocstrcpy(NULL, filename);
if ((fd = open(full_filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR |
- S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) == -1 ||
- (f = fdopen(fd, "a")) == NULL)
+ S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) == -1)
result = FALSE;
- else
- fclose(f);
+ else {
+ if ((f = fdopen(fd, "a")) == NULL)
+ result = FALSE;
+ else
+ fclose(f);
+ close(fd);
+ }
- close(fd);
free(full_filename);
return result;