diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-06-05 04:46:01 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-06-05 04:46:01 +0300 |
commit | aca40295e8ac868b6e3aec40bbcd388177508c1f (patch) | |
tree | 5f9f329021231b8434581b9ea786dec431ecfd96 /source | |
parent | b2e5c017a1a93ec9b6f001bae385c7cc9b756cca (diff) |
Check ftell return values
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenkernel/BKE_text.h | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/text.c | 44 | ||||
-rw-r--r-- | source/blender/blenlib/intern/storage.c | 5 | ||||
-rw-r--r-- | source/blender/makesdna/intern/makesdna.c | 5 |
4 files changed, 39 insertions, 17 deletions
diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index c5f47ad6cbf..a5a59d14c92 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -46,7 +46,7 @@ void txt_set_undostate (int u); int txt_get_undostate (void); struct Text *BKE_text_add (struct Main *bmain, const char *name); int txt_extended_ascii_as_utf8(char **str); -int BKE_text_reload (struct Text *text); +bool BKE_text_reload(struct Text *text); struct Text *BKE_text_load_ex(struct Main *bmain, const char *file, const char *relpath, const bool is_internal); struct Text *BKE_text_load (struct Main *bmain, const char *file, const char *relpath); diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index c901fa40acc..89456763b95 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -330,7 +330,7 @@ static void text_from_buf(Text *text, const unsigned char *buffer, const int len text->curc = text->selc = 0; } -int BKE_text_reload(Text *text) +bool BKE_text_reload(Text *text) { FILE *fp; int len; @@ -339,13 +339,24 @@ int BKE_text_reload(Text *text) char str[FILE_MAX]; BLI_stat_t st; - if (!text->name) return 0; - + if (!text->name) { + return false; + } + BLI_strncpy(str, text->name, FILE_MAX); BLI_path_abs(str, G.main->name); fp = BLI_fopen(str, "r"); - if (fp == NULL) return 0; + if (fp == NULL) { + return false; + } + fseek(fp, 0L, SEEK_END); + len = ftell(fp); + fseek(fp, 0L, SEEK_SET); + if (UNLIKELY(len == -1)) { + fclose(fp); + return false; + } /* free memory: */ @@ -363,11 +374,6 @@ int BKE_text_reload(Text *text) MEM_freeN(text->undo_buf); init_undo_text(text); - fseek(fp, 0L, SEEK_END); - len = ftell(fp); - fseek(fp, 0L, SEEK_SET); - - buffer = MEM_mallocN(len, "text_buffer"); /* under windows fread can return less than len bytes because * of CR stripping */ @@ -385,7 +391,7 @@ int BKE_text_reload(Text *text) text_from_buf(text, buffer, len); MEM_freeN(buffer); - return 1; + return true; } Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const bool is_internal) @@ -402,8 +408,18 @@ Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const BLI_path_abs(str, relpath); fp = BLI_fopen(str, "r"); - if (fp == NULL) return NULL; - + if (fp == NULL) { + return NULL; + } + + fseek(fp, 0L, SEEK_END); + len = ftell(fp); + fseek(fp, 0L, SEEK_SET); + if (UNLIKELY(len == -1)) { + fclose(fp); + return NULL; + } + ta = BKE_libblock_alloc(bmain, ID_TXT, BLI_path_basename(str)); ta->id.us = 1; @@ -423,10 +439,6 @@ Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const /* clear undo buffer */ init_undo_text(ta); - - fseek(fp, 0L, SEEK_END); - len = ftell(fp); - fseek(fp, 0L, SEEK_SET); buffer = MEM_mallocN(len, "text_buffer"); /* under windows fread can return less than len bytes because diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index 046dba26c69..6394187d40a 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -296,6 +296,11 @@ LinkNode *BLI_file_read_as_lines(const char *name) size = (size_t)ftell(fp); fseek(fp, 0, SEEK_SET); + if (UNLIKELY(size == (size_t)-1)) { + fclose(fp); + return NULL; + } + buf = MEM_mallocN(size, "file_as_lines"); if (buf) { size_t i, last = 0; diff --git a/source/blender/makesdna/intern/makesdna.c b/source/blender/makesdna/intern/makesdna.c index 7f3e9b7c196..a7ff4244d5f 100644 --- a/source/blender/makesdna/intern/makesdna.c +++ b/source/blender/makesdna/intern/makesdna.c @@ -539,6 +539,11 @@ static void *read_file_data(char *filename, int *r_len) *r_len = ftell(fp); fseek(fp, 0L, SEEK_SET); + if (*r_len == -1) { + fclose(fp); + return NULL; + } + data = MEM_mallocN(*r_len, "read_file_data"); if (!data) { *r_len = -1; |