From 6d5dafa489bcb576e0a2a3c5f2b30c32b033d64d Mon Sep 17 00:00:00 2001 From: mothcompute <94941435+mothcompute@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:00:43 -0700 Subject: [PATCH] fix segfault when passing invalid filename on command line (#276) * fix segfault when passing invalid filename on command line * fix formatting --- src/tracker/sdl/SDL_Main.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/tracker/sdl/SDL_Main.cpp b/src/tracker/sdl/SDL_Main.cpp index 75ffa1df..35a95b6b 100644 --- a/src/tracker/sdl/SDL_Main.cpp +++ b/src/tracker/sdl/SDL_Main.cpp @@ -65,6 +65,7 @@ #include #include #include +#include #include #include "SDL_KeyTranslation.h" @@ -971,13 +972,21 @@ int main(int argc, char *argv[]) if (loadFile) { - PPSystemString newCwd = path.getCurrent(); - path.change(oldCwd); - SendFile(realpath(loadFile, loadFileAbsPath)); - path.change(newCwd); - pp_uint16 chr[3] = {VK_RETURN, 0, 0}; - PPEvent event(eKeyDown, &chr, sizeof(chr)); - RaiseEventSerialized(&event); + struct stat statBuf; + if (stat(loadFile, &statBuf) != 0) + { + fprintf(stderr, "could not open %s: %s\n", loadFile, strerror(errno)); + } + else + { + PPSystemString newCwd = path.getCurrent(); + path.change(oldCwd); + SendFile(realpath(loadFile, loadFileAbsPath)); + path.change(newCwd); + pp_uint16 chr[3] = {VK_RETURN, 0, 0}; + PPEvent event(eKeyDown, &chr, sizeof(chr)); + RaiseEventSerialized(&event); + } } // Main event loop