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.master
parent
eed0090e32
commit
dbbe267d71
20
src/files.c
20
src/files.c
|
@ -686,32 +686,32 @@ bool close_buffer(void)
|
||||||
* warnings. */
|
* warnings. */
|
||||||
int is_file_writable(const char *filename)
|
int is_file_writable(const char *filename)
|
||||||
{
|
{
|
||||||
struct stat fileinfo, fileinfo2;
|
char *full_filename;
|
||||||
|
struct stat fileinfo;
|
||||||
int fd;
|
int fd;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
char *full_filename;
|
|
||||||
bool result = TRUE;
|
bool result = TRUE;
|
||||||
|
|
||||||
if (ISSET(VIEW_MODE))
|
if (ISSET(VIEW_MODE))
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
||||||
/* Get the specified file's full path. */
|
|
||||||
full_filename = get_full_path(filename);
|
full_filename = get_full_path(filename);
|
||||||
|
|
||||||
/* Okay, if we can't stat the absolute path due to some component's
|
/* If the absolute path is unusable, use the given relative one. */
|
||||||
* permissions, just try the relative one. */
|
if (full_filename == NULL || stat(full_filename, &fileinfo) == -1)
|
||||||
if (full_filename == NULL ||
|
|
||||||
(stat(full_filename, &fileinfo) == -1 && stat(filename, &fileinfo2) != -1))
|
|
||||||
full_filename = mallocstrcpy(NULL, filename);
|
full_filename = mallocstrcpy(NULL, filename);
|
||||||
|
|
||||||
if ((fd = open(full_filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR |
|
if ((fd = open(full_filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR |
|
||||||
S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) == -1 ||
|
S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) == -1)
|
||||||
(f = fdopen(fd, "a")) == NULL)
|
result = FALSE;
|
||||||
|
else {
|
||||||
|
if ((f = fdopen(fd, "a")) == NULL)
|
||||||
result = FALSE;
|
result = FALSE;
|
||||||
else
|
else
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
free(full_filename);
|
free(full_filename);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
Loading…
Reference in New Issue