[Bf-blender-cvs] [d2a4a038906] master: Fix reading directory as file content on Linux

Campbell Barton noreply at git.blender.org
Mon Jul 15 11:41:42 CEST 2019


Commit: d2a4a038906ab455b7af4e90d6f183d95a6ba10e
Author: Campbell Barton
Date:   Mon Jul 15 19:24:59 2019 +1000
Branches: master
https://developer.blender.org/rBd2a4a038906ab455b7af4e90d6f183d95a6ba10e

Fix reading directory as file content on Linux

Reading a directory as a file on Linux was attempting to allocate LONG_MAX,
this happens in template file lists (fix for that coming next).

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

M	source/blender/blenlib/intern/storage.c

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

diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c
index bdaa7be60cf..39af73ac175 100644
--- a/source/blender/blenlib/intern/storage.c
+++ b/source/blender/blenlib/intern/storage.c
@@ -303,12 +303,24 @@ void *BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *
   void *mem = NULL;
 
   if (fp) {
-    fseek(fp, 0L, SEEK_END);
+    struct stat st;
+    if (fstat(fileno(fp), &st) == -1) {
+      goto finally;
+    }
+    if (S_ISDIR(st.st_mode)) {
+      goto finally;
+    }
+    if (fseek(fp, 0L, SEEK_END) == -1) {
+      goto finally;
+    }
+    /* Don't use the 'st_size' because it may be the symlink. */
     const long int filelen = ftell(fp);
     if (filelen == -1) {
       goto finally;
     }
-    fseek(fp, 0L, SEEK_SET);
+    if (fseek(fp, 0L, SEEK_SET) == -1) {
+      goto finally;
+    }
 
     mem = MEM_mallocN(filelen + pad_bytes, __func__);
     if (mem == NULL) {
@@ -344,12 +356,24 @@ void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t
   void *mem = NULL;
 
   if (fp) {
-    fseek(fp, 0L, SEEK_END);
+    struct stat st;
+    if (fstat(fileno(fp), &st) == -1) {
+      goto finally;
+    }
+    if (S_ISDIR(st.st_mode)) {
+      goto finally;
+    }
+    if (fseek(fp, 0L, SEEK_END) == -1) {
+      goto finally;
+    }
+    /* Don't use the 'st_size' because it may be the symlink. */
     const long int filelen = ftell(fp);
     if (filelen == -1) {
       goto finally;
     }
-    fseek(fp, 0L, SEEK_SET);
+    if (fseek(fp, 0L, SEEK_SET) == -1) {
+      goto finally;
+    }
 
     mem = MEM_mallocN(filelen + pad_bytes, __func__);
     if (mem == NULL) {



More information about the Bf-blender-cvs mailing list