diff options
author | Syoyo Fujita <syoyo@lighttransport.com> | 2017-02-10 17:19:28 +0900 |
---|---|---|
committer | Syoyo Fujita <syoyo@lighttransport.com> | 2017-02-10 17:19:28 +0900 |
commit | 9134c1d3951fc2b3a83dd0691cb79b4245c94523 (patch) | |
tree | 775b245d1d34b7e0189e82e775247a6e8854ae07 | |
parent | cc9897316fd68556d8b7c96fb55392260d2a029a (diff) | |
download | tinyobjloader-9134c1d3951fc2b3a83dd0691cb79b4245c94523.tar.gz |
Add file size check.
-rw-r--r-- | experimental/premake5.lua (renamed from experimental/premake4.lua) | 19 | ||||
-rw-r--r-- | experimental/viewer.cc | 28 |
2 files changed, 41 insertions, 6 deletions
diff --git a/experimental/premake4.lua b/experimental/premake5.lua index 0273ed2..4a94af9 100644 --- a/experimental/premake4.lua +++ b/experimental/premake5.lua @@ -8,6 +8,16 @@ newoption { description = "Build with ZStandard compression." } +newoption { + trigger = "clang", + description = "Use clang compiler." +} + +newoption { + trigger = "asan", + description = "Enable AddressSanitizer(gcc or clang only)." +} + solution "objview" -- location ( "build" ) configurations { "Release", "Debug" } @@ -24,11 +34,20 @@ solution "objview" flags { "c++11" } --buildoptions { "-std=c++11" } + if _OPTIONS['clang'] then + toolset "clang" + end + if _OPTIONS['with-zlib'] then defines { 'ENABLE_ZLIB' } links { 'z' } end + if _OPTIONS['asan'] then + buildoptions { '-fsanitize=address' } + linkoptions { '-fsanitize=address' } + end + if _OPTIONS['with-zstd'] then print("with-zstd") defines { 'ENABLE_ZSTD' } diff --git a/experimental/viewer.cc b/experimental/viewer.cc index 3555b54..be5053f 100644 --- a/experimental/viewer.cc +++ b/experimental/viewer.cc @@ -110,11 +110,20 @@ const char *mmap_file(size_t *len, const char* filename) #else - FILE* f = fopen(filename, "r" ); + FILE* f = fopen(filename, "rb" ); + if (!f) { + fprintf(stderr, "Failed to open file : %s\n", filename); + return nullptr; + } fseek(f, 0, SEEK_END); long fileSize = ftell(f); fclose(f); + if (fileSize < 16) { + fprintf(stderr, "Empty or invalid .obj : %s\n", filename); + return nullptr; + } + struct stat sb; char *p; int fd; @@ -122,29 +131,29 @@ const char *mmap_file(size_t *len, const char* filename) fd = open (filename, O_RDONLY); if (fd == -1) { perror ("open"); - return NULL; + return nullptr; } if (fstat (fd, &sb) == -1) { perror ("fstat"); - return NULL; + return nullptr; } if (!S_ISREG (sb.st_mode)) { fprintf (stderr, "%s is not a file\n", "lineitem.tbl"); - return NULL; + return nullptr; } p = (char*)mmap (0, fileSize, PROT_READ, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { perror ("mmap"); - return NULL; + return nullptr; } if (close (fd) == -1) { perror ("close"); - return NULL; + return nullptr; } (*len) = fileSize; @@ -298,6 +307,7 @@ const char* get_file_data(size_t *len, const char* filename) } else { data = mmap_file(&data_len, filename); + } (*len) = data_len; @@ -642,6 +652,12 @@ int main(int argc, char **argv) exit(-1); return false; } + + if (data_len < 4) { + printf("Empty file\n"); + exit(-1); + return false; + } printf("filesize: %d\n", (int)data_len); tinyobj_opt::LoadOption option; option.req_num_threads = num_threads; |