diff --git a/src/RagePhoto.c b/src/RagePhoto.c
index 139c9f2..9d98fbe 100644
--- a/src/RagePhoto.c
+++ b/src/RagePhoto.c
@@ -926,6 +926,10 @@ bool ragephoto_savefilef(ragephoto_t instance_t, const char *filename, uint32_t
 #else
     FILE *file = fopen(filename, "wb");
 #endif
+    if (!file) {
+        free(data);
+        return false;
+    }
     const size_t fileWsize = fwrite(data, sizeof(char), fileSize, file);
     fclose(file);
     free(data);