aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2025-02-06 14:30:34 -0500
committerDavid Robillard <d@drobilla.net>2025-02-06 14:32:55 -0500
commitf9e2884809cd6f0e11f76b25af9b09047aa9a800 (patch)
tree314d7e8405b29c445cf5eed724e55506ff4c2172 /examples
parent40baf35b078b841cefbc55487b657aee25ee3b5f (diff)
downloadpugl-f9e2884809cd6f0e11f76b25af9b09047aa9a800.tar.gz
pugl-f9e2884809cd6f0e11f76b25af9b09047aa9a800.tar.bz2
pugl-f9e2884809cd6f0e11f76b25af9b09047aa9a800.zip
Gracefully handle failing return from ftell()
Again I don't think this is actually a concern, but it appeases clang-tidy.
Diffstat (limited to 'examples')
-rw-r--r--examples/pugl_shader_demo.c18
-rw-r--r--examples/pugl_vulkan_cpp_demo.cpp7
2 files changed, 17 insertions, 8 deletions
diff --git a/examples/pugl_shader_demo.c b/examples/pugl_shader_demo.c
index 2b122a9..c05a9dd 100644
--- a/examples/pugl_shader_demo.c
+++ b/examples/pugl_shader_demo.c
@@ -230,10 +230,15 @@ loadShader(const char* const programPath, const char* const name)
free(path);
fseek(file, 0, SEEK_END);
- const size_t fileSize = (size_t)ftell(file);
+ const long filePos = ftell(file);
+ if (filePos <= 0) {
+ fclose(file);
+ return NULL;
+ }
fseek(file, 0, SEEK_SET);
- char* source = (char*)calloc(1, fileSize + 1U);
+ const size_t fileSize = (size_t)filePos;
+ char* source = (char*)calloc(1, fileSize + 1U);
if (fread(source, 1, fileSize, file) != fileSize) {
free(source);
@@ -351,17 +356,16 @@ setupGl(PuglTestApp* app)
char* const fragmentSource =
loadShader(app->programPath, SHADER_DIR "rect.frag");
- if (!vertexSource || !fragmentSource) {
- logError("Failed to load shader sources\n");
- return PUGL_FAILURE;
+ // Compile rectangle shaders and program
+ if (headerSource && vertexSource && fragmentSource) {
+ app->drawRect = compileProgram(headerSource, vertexSource, fragmentSource);
}
- // Compile rectangle shaders and program
- app->drawRect = compileProgram(headerSource, vertexSource, fragmentSource);
free(fragmentSource);
free(vertexSource);
free(headerSource);
if (!app->drawRect.program) {
+ logError("Failed to compile shader program\n");
return PUGL_FAILURE;
}
diff --git a/examples/pugl_vulkan_cpp_demo.cpp b/examples/pugl_vulkan_cpp_demo.cpp
index a1d6954..52976e6 100644
--- a/examples/pugl_vulkan_cpp_demo.cpp
+++ b/examples/pugl_vulkan_cpp_demo.cpp
@@ -715,7 +715,12 @@ readFile(const char* const programPath, const std::string& filename)
}
fseek(file.get(), 0, SEEK_END);
- const auto fileSize = static_cast<size_t>(ftell(file.get()));
+ const auto filePos = ftell(file.get());
+ if (filePos <= 0) {
+ return {};
+ }
+
+ const auto fileSize = static_cast<size_t>(filePos);
fseek(file.get(), 0, SEEK_SET);
const auto numWords = fileSize / sizeof(uint32_t);