From a9dbaf3755ac1abad109970e0fff190bb7418ac2 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 18 Feb 2015 07:26:10 +1100 Subject: [PATCH] Ensure BLI_stat() return value is checked. also add function attrs on BLI_fileops to ensure they're used correctly. --- source/blender/blenkernel/intern/bpath.c | 2 +- source/blender/blenkernel/intern/packedFile.c | 2 +- source/blender/blenkernel/intern/text.c | 16 ++++-- source/blender/blenlib/BLI_fileops.h | 52 ++++++++++--------- source/blender/blenlib/intern/storage.c | 5 +- source/blender/imbuf/intern/thumbs.c | 12 +++-- 6 files changed, 51 insertions(+), 38 deletions(-) diff --git a/source/blender/blenkernel/intern/bpath.c b/source/blender/blenkernel/intern/bpath.c index fc3c1a20700..3488cff7315 100644 --- a/source/blender/blenkernel/intern/bpath.c +++ b/source/blender/blenkernel/intern/bpath.c @@ -233,7 +233,7 @@ static int findFileRecursive(char *filename_new, BLI_join_dirfile(path, sizeof(path), dirname, de->d_name); - if (BLI_stat(path, &status) != 0) + if (BLI_stat(path, &status) == -1) continue; /* cant stat, don't bother with this file, could print debug info here */ if (S_ISREG(status.st_mode)) { /* is file */ diff --git a/source/blender/blenkernel/intern/packedFile.c b/source/blender/blenkernel/intern/packedFile.c index d186b4299a5..b989bb2760f 100644 --- a/source/blender/blenkernel/intern/packedFile.c +++ b/source/blender/blenkernel/intern/packedFile.c @@ -376,7 +376,7 @@ int checkPackedFile(const char *filename, PackedFile *pf) BLI_strncpy(name, filename, sizeof(name)); BLI_path_abs(name, G.main->name); - if (BLI_stat(name, &st)) { + if (BLI_stat(name, &st) == -1) { ret_val = PF_NOFILE; } else if (st.st_size != pf->size) { diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index c5a8cbe68b2..9f441b45db9 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -375,8 +375,12 @@ int BKE_text_reload(Text *text) fclose(fp); - BLI_stat(str, &st); - text->mtime = st.st_mtime; + if (BLI_stat(str, &st) != -1) { + text->mtime = st.st_mtime; + } + else { + text->mtime = 0; + } text_from_buf(text, buffer, len); @@ -431,8 +435,12 @@ Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const fclose(fp); - BLI_stat(str, &st); - ta->mtime = st.st_mtime; + if (BLI_stat(str, &st) != -1) { + ta->mtime = st.st_mtime; + } + else { + ta->mtime = 0; + } text_from_buf(ta, buffer, len); diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h index 86a8134376a..7898a54002f 100644 --- a/source/blender/blenlib/BLI_fileops.h +++ b/source/blender/blenlib/BLI_fileops.h @@ -45,6 +45,8 @@ extern "C" { #include /* for PATH_MAX */ +#include "BLI_compiler_attrs.h" + #ifndef PATH_MAX # define PATH_MAX 4096 #endif @@ -53,12 +55,12 @@ struct gzFile; /* Common */ -int BLI_exists(const char *path); -int BLI_copy(const char *path, const char *to); -int BLI_rename(const char *from, const char *to); -int BLI_delete(const char *path, bool dir, bool recursive); -int BLI_move(const char *path, const char *to); -int BLI_create_symlink(const char *path, const char *to); +int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +int BLI_copy(const char *path, const char *to) ATTR_NONNULL(); +int BLI_rename(const char *from, const char *to) ATTR_NONNULL(); +int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL(); +int BLI_move(const char *path, const char *to) ATTR_NONNULL(); +int BLI_create_symlink(const char *path, const char *to) ATTR_NONNULL(); /* keep in sync with the definition of struct direntry in BLI_fileops_types.h */ #ifdef WIN32 @@ -73,7 +75,7 @@ typedef struct _stat BLI_stat_t; typedef struct stat BLI_stat_t; #endif -int BLI_stat(const char *path, BLI_stat_t *buffer); +int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); #ifdef WIN32 int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer); #endif @@ -82,11 +84,11 @@ int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer); struct direntry; -bool BLI_is_dir(const char *path); -bool BLI_is_file(const char *path); -void BLI_dir_create_recursive(const char *dir); -double BLI_dir_free_space(const char *dir); -char *BLI_current_working_dir(char *dir, const size_t maxlen); +bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +void BLI_dir_create_recursive(const char *dir) ATTR_NONNULL(); +double BLI_dir_free_space(const char *dir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +char *BLI_current_working_dir(char *dir, const size_t maxlen) ATTR_NONNULL(); /* Filelist */ @@ -98,28 +100,28 @@ void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries, void ( /* Files */ -FILE *BLI_fopen(const char *filename, const char *mode); -void *BLI_gzopen(const char *filename, const char *mode); -int BLI_open(const char *filename, int oflag, int pmode); -int BLI_access(const char *filename, int mode); +FILE *BLI_fopen(const char *filename, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +void *BLI_gzopen(const char *filename, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +int BLI_open(const char *filename, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +int BLI_access(const char *filename, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); -bool BLI_file_is_writable(const char *file); -bool BLI_file_touch(const char *file); +bool BLI_file_is_writable(const char *file) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +bool BLI_file_touch(const char *file) ATTR_NONNULL(); #if 0 /* UNUSED */ -int BLI_file_gzip(const char *from, const char *to); +int BLI_file_gzip(const char *from, const char *to) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); #endif -char *BLI_file_ungzip_to_mem(const char *from_file, int *r_size); +char *BLI_file_ungzip_to_mem(const char *from_file, int *r_size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); -size_t BLI_file_descriptor_size(int file); -size_t BLI_file_size(const char *file); +size_t BLI_file_descriptor_size(int file) ATTR_WARN_UNUSED_RESULT; +size_t BLI_file_size(const char *file) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); /* compare if one was last modified before the other */ -bool BLI_file_older(const char *file1, const char *file2); +bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); /* read ascii file as lines, empty list if reading fails */ -struct LinkNode *BLI_file_read_as_lines(const char *file); -void BLI_file_free_lines(struct LinkNode *lines); +struct LinkNode *BLI_file_read_as_lines(const char *file) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +void BLI_file_free_lines(struct LinkNode *lines) ATTR_NONNULL(); /* this weirdo pops up in two places ... */ #if !defined(WIN32) diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index c6b65171478..46c5a11949c 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -253,8 +253,9 @@ static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname) file->relname = dlink->name; file->path = BLI_strdupcat(dirname, dlink->name); BLI_join_dirfile(fullname, sizeof(fullname), dirname, dlink->name); - BLI_stat(fullname, &file->s); - file->type = file->s.st_mode; + if (BLI_stat(fullname, &file->s) != -1) { + file->type = file->s.st_mode; + } file->flags = 0; dir_ctx->nrfiles++; file++; diff --git a/source/blender/imbuf/intern/thumbs.c b/source/blender/imbuf/intern/thumbs.c index 94e95c06fea..118f0405303 100644 --- a/source/blender/imbuf/intern/thumbs.c +++ b/source/blender/imbuf/intern/thumbs.c @@ -343,8 +343,9 @@ ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, Im } if (img != NULL) { - BLI_stat(path, &info); - BLI_snprintf(mtime, sizeof(mtime), "%ld", (long int)info.st_mtime); + if (BLI_stat(path, &info) != -1) { + BLI_snprintf(mtime, sizeof(mtime), "%ld", (long int)info.st_mtime); + } BLI_snprintf(cwidth, sizeof(cwidth), "%d", img->x); BLI_snprintf(cheight, sizeof(cheight), "%d", img->y); } @@ -363,8 +364,9 @@ ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, Im } IMB_free_anim(anim); } - BLI_stat(path, &info); - BLI_snprintf(mtime, sizeof(mtime), "%ld", (long int)info.st_mtime); + if (BLI_stat(path, &info) != -1) { + BLI_snprintf(mtime, sizeof(mtime), "%ld", (long int)info.st_mtime); + } } if (!img) return NULL; @@ -461,7 +463,7 @@ ImBuf *IMB_thumb_manage(const char *path, ThumbSize size, ThumbSource source) BLI_stat_t st; ImBuf *img = NULL; - if (BLI_stat(path, &st)) { + if (BLI_stat(path, &st) == -1) { return NULL; } if (!uri_from_filename(path, uri)) {