From d2a4a038906ab455b7af4e90d6f183d95a6ba10e Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 15 Jul 2019 19:24:59 +1000 Subject: 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). --- source/blender/blenlib/intern/storage.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to '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) { -- cgit v1.2.3