From ceefacca7f9516ac550d80224140ec403b4c4ecf Mon Sep 17 00:00:00 2001 From: Ashton Fagg Date: Sun, 6 Oct 2024 12:55:03 -0400 Subject: [PATCH] Fix absolute path error on Windows --- src/util-dir.c | 10 +++++++++- src/util-dir_windows.c | 17 +++++++++++++++++ src/util.h | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/util-dir.c b/src/util-dir.c index b17deb6..26486c0 100644 --- a/src/util-dir.c +++ b/src/util-dir.c @@ -71,9 +71,15 @@ char* currentdir(void) { return append_trail_slash(q_(getcwd(buf,2048))); } +int is_valid_path(const char *path) { + return path[0] == '/'; +} + #else + char* homedir(void); char* currentdir(void); +int is_valid_path(const char *); #endif char* configdir(void) { @@ -82,9 +88,10 @@ char* configdir(void) { if (env) /* note: env can be a NULL */ { - if (env[0] != DIRSEP[0]) /* note: DIRSEP == \\ on windows, / on unix */ + if (!is_valid_path(env)) { cond_printf(0,"Error: %s must be absolute. Got: %s \n",c,env); + abort(); } s(c); /* note : this frees c. */ return append_trail_slash(q(env)); @@ -143,3 +150,4 @@ char* basedir(void) { s(cd_); return configdir(); } + diff --git a/src/util-dir_windows.c b/src/util-dir_windows.c index 822fc98..0ee7166 100644 --- a/src/util-dir_windows.c +++ b/src/util-dir_windows.c @@ -64,4 +64,21 @@ char* currentdir(void) { char buf[2048]; return append_trail_slash(q_(_getcwd(buf,2048))); } + +int is_valid_path(const char *path) { + // + // On Windows, an absolute path can technically + // start with a drive letter (i.e. c:\roswell), + // but could also be a UNC path (i.e. \\OTHERMACHINE\roswell). + // + // But, if invoked inside an msys2 shell we could get a UNIX-style path. + // + // It appears however that the latter two scenarios are not supported + // by other parts of the codebase, so we shall enforce here that only + // regular Windows-style paths are allowed. + // + + return (isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/')); +} + #endif diff --git a/src/util.h b/src/util.h index a2e3585..e7d4509 100644 --- a/src/util.h +++ b/src/util.h @@ -13,6 +13,7 @@ #include #include #ifndef HAVE_WINDOWS_H +#include #include #include #include @@ -142,6 +143,7 @@ int directory_exist_p (char* path); int change_directory(const char* path); int delete_directory(char* pathspec,int recursive); char* impldir(char* arch,char* os,char* impl,char* version); +int is_valid_path(const char *path); /*util_file.c */ int delete_file(char* pathspec); int rename_file(char* file,char* new_name);