From 2a95ec73ba99759f57b98a68aefbd5a67aaa754b Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Sun, 16 Oct 2022 07:59:54 +0100 Subject: [PATCH] Fix Coverity Scan 1491166. --- common/misc_mosq.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/common/misc_mosq.c b/common/misc_mosq.c index 93265333..3b167943 100644 --- a/common/misc_mosq.c +++ b/common/misc_mosq.c @@ -130,32 +130,32 @@ FILE *mosquitto__fopen(const char *path, const char *mode, bool restrict_read) } } #else - if(mode[0] == 'r'){ - struct stat statbuf; - if(stat(path, &statbuf) < 0){ - return NULL; - } - - if(!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)){ -#ifdef WITH_BROKER - log__printf(NULL, MOSQ_LOG_ERR, "Error: %s is not a file.", path); -#endif - return NULL; - } - } + FILE *fptr; if(restrict_read){ - FILE *fptr; mode_t old_mask; old_mask = umask(0077); fptr = fopen(path, mode); umask(old_mask); - - return fptr; }else{ - return fopen(path, mode); + fptr = fopen(path, mode); + } + + struct stat statbuf; + if(fstat(fileno(fptr), &statbuf) < 0){ + fclose(fptr); + return NULL; + } + + if(!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)){ +#ifdef WITH_BROKER + log__printf(NULL, MOSQ_LOG_ERR, "Error: %s is not a file.", path); +#endif + fclose(fptr); + return NULL; } + return fptr; #endif }