From 727b6835a63d2a163dae434e1090a14aa7011373 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 8 Aug 2022 23:31:49 -0400 Subject: Handle fread() errors in examples --- examples/pugl_shader_demo.c | 6 +++++- examples/pugl_vulkan_cpp_demo.cpp | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/pugl_shader_demo.c b/examples/pugl_shader_demo.c index 8f2c2e7..d018db3 100644 --- a/examples/pugl_shader_demo.c +++ b/examples/pugl_shader_demo.c @@ -204,7 +204,11 @@ loadShader(const char* const programPath, const char* const name) fseek(file, 0, SEEK_SET); char* source = (char*)calloc(1, fileSize + 1u); - fread(source, 1, fileSize, file); + if (fread(source, 1, fileSize, file) != fileSize) { + free(source); + source = NULL; + } + fclose(file); return source; diff --git a/examples/pugl_vulkan_cpp_demo.cpp b/examples/pugl_vulkan_cpp_demo.cpp index 69f360d..c901de5 100644 --- a/examples/pugl_vulkan_cpp_demo.cpp +++ b/examples/pugl_vulkan_cpp_demo.cpp @@ -692,7 +692,10 @@ readFile(const char* const programPath, const std::string& filename) const auto numWords = fileSize / sizeof(uint32_t); std::vector buffer(numWords); - fread(buffer.data(), sizeof(uint32_t), numWords, file.get()); + if (fread(buffer.data(), sizeof(uint32_t), numWords, file.get()) != + numWords) { + buffer.clear(); + } return buffer; } -- cgit v1.2.1