Commit: aca40295e8ac868b6e3aec40bbcd388177508c1f
Author: Campbell Barton
Date:   Fri Jun 5 11:46:01 2015 +1000
Branches: master
https://developer.blender.org/rBaca40295e8ac868b6e3aec40bbcd388177508c1f

Check ftell return values

===================================================================

M       source/blender/blenkernel/BKE_text.h
M       source/blender/blenkernel/intern/text.c
M       source/blender/blenlib/intern/storage.c
M       source/blender/makesdna/intern/makesdna.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_text.h 
b/source/blender/blenkernel/BKE_text.h
index c5f47ad..a5a59d1 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 c901fa4..8945676 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 046dba2..6394187 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 7f3e9b7..a7ff424 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;

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to