From 0cee0d1d11b8f26897a1b017db8ce24005005fd9 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 26 Oct 2022 10:06:38 +0100 Subject: [PATCH] Fix Coverity Scan 1486944 (backport from develop) --- lib/misc_mosq.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/misc_mosq.c b/lib/misc_mosq.c index 5004125a..2529d794 100644 --- a/lib/misc_mosq.c +++ b/lib/misc_mosq.c @@ -22,6 +22,8 @@ Contributors: #include "config.h" #include +#include +#include #include #include #include @@ -33,8 +35,10 @@ Contributors: # include # include # include +# define PATH_MAX MAX_PATH #else # include +# include #endif #include "misc_mosq.h" @@ -126,30 +130,33 @@ 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)){ - log__printf(NULL, MOSQ_LOG_ERR, "Error: %s is not a file.", path); - return NULL; - } - } + FILE *fptr; + struct stat statbuf; 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); + } + if(!fptr) return NULL; + + 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 }