From edee4db8d1fd85495f189293041311bfff56d23c Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 29 Aug 2024 17:19:40 +0200 Subject: [PATCH] Handle failure getting VNC directories Although rare, there are cases where we might fail to determine our base directories. Make sure the code can handle it. --- vncviewer/vncviewer.cxx | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/vncviewer/vncviewer.cxx b/vncviewer/vncviewer.cxx index 0aec1910..c206427b 100644 --- a/vncviewer/vncviewer.cxx +++ b/vncviewer/vncviewer.cxx @@ -540,28 +540,47 @@ migrateDeprecatedOptions() static void create_base_dirs() { - char *confdir = strdup(os::getvncconfigdir()); + const char *dir; + + dir = os::getvncconfigdir(); + if (dir == nullptr) { + vlog.error(_("Could not determine VNC config directory path")); + return; + } + #ifndef WIN32 - char *dotdir = strrchr(confdir, '.'); + const char *dotdir = strrchr(dir, '.'); if (dotdir != nullptr && strcmp(dotdir, ".vnc") == 0) vlog.info(_("~/.vnc is deprecated, please consult 'man vncviewer' for paths to migrate to.")); #else - char *vncdir = strrchr(confdir, '\\'); + const char *vncdir = strrchr(dir, '\\'); if (vncdir != nullptr && strcmp(vncdir, "vnc") == 0) vlog.info(_("%%APPDATA%%\\vnc is deprecated, please switch to the %%APPDATA%%\\TigerVNC location.")); #endif - if (os::mkdir_p(os::getvncconfigdir(), 0755) == -1) { + if (os::mkdir_p(dir, 0755) == -1) { if (errno != EEXIST) vlog.error(_("Could not create VNC config directory: %s"), strerror(errno)); } - if (os::mkdir_p(os::getvncdatadir(), 0755) == -1) { + dir = os::getvncdatadir(); + if (dir == nullptr) { + vlog.error(_("Could not determine VNC data directory path")); + return; + } + + if (os::mkdir_p(dir, 0755) == -1) { if (errno != EEXIST) vlog.error(_("Could not create VNC data directory: %s"), strerror(errno)); } - if (os::mkdir_p(os::getvncstatedir(), 0755) == -1) { + dir = os::getvncstatedir(); + if (dir == nullptr) { + vlog.error(_("Could not determine VNC state directory path")); + return; + } + + if (os::mkdir_p(dir, 0755) == -1) { if (errno != EEXIST) vlog.error(_("Could not create VNC state directory: %s"), strerror(errno)); } -- 2.39.5