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/blender/blenkernel/intern/text.c | |
parent | b2e5c017a1a93ec9b6f001bae385c7cc9b756cca (diff) |
Check ftell return values
Diffstat (limited to 'source/blender/blenkernel/intern/text.c')
-rw-r--r-- | source/blender/blenkernel/intern/text.c | 44 |
1 files changed, 28 insertions, 16 deletions
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 |