From 02782489a92f5f3312673ddb27bf615700e0a6c3 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Mon, 11 Jun 2001 00:24:28 +0000 Subject: [PATCH] * path.cc (chdir): Pre-check path for validity before eating trailing space. Then, ensure that path_conv doesn't check the path for validity again. --- winsup/cygwin/ChangeLog | 6 ++++++ winsup/cygwin/path.cc | 33 ++++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 67b6e7963..2a5d87a6f 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,9 @@ +Sun Jun 10 20:19:47 2001 Christopher Faylor + + * path.cc (chdir): Pre-check path for validity before eating trailing + space. Then, ensure that path_conv doesn't check the path for validity + again. + Sun Jun 10 12:56:00 2001 Christopher Faylor * exceptions.cc (sigdelayed): Ensure that signal is cleared as diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc index 5dd13f67f..ec451d1ed 100644 --- a/winsup/cygwin/path.cc +++ b/winsup/cygwin/path.cc @@ -2991,19 +2991,21 @@ getwd (char *buf) /* chdir: POSIX 5.2.1.1 */ extern "C" int -chdir (const char *dir) +chdir (const char *in_dir) { - MALLOC_CHECK; - path_conv path (dir, PC_FULL | PC_SYM_FOLLOW); - if (path.error) + int dir_error = check_null_empty_path (in_dir); + if (dir_error) { - set_errno (path.error); - syscall_printf ("-1 = chdir (%s)", dir); + syscall_printf ("NULL or invalid input to chdir"); + set_errno (dir_error); return -1; } - syscall_printf ("dir %s", dir); + + syscall_printf ("dir '%s'", in_dir); char *s; + char dir[strlen (in_dir) + 1]; + strcpy (dir, in_dir); /* Incredibly. Windows allows you to specify a path with trailing whitespace to SetCurrentDirectory. This doesn't work too well with other parts of the API, though, apparently. So nuke trailing @@ -3011,6 +3013,23 @@ chdir (const char *dir) for (s = strchr (dir, '\0'); --s >= dir && isspace ((unsigned int) (*s & 0xff)); ) *s = '\0'; + if (!*s) + { + set_errno (ENOENT); + return -1; + } + + /* Convert path. Third argument ensures that we don't check for NULL/empty/invalid + again. */ + path_conv path (dir, PC_FULL | PC_SYM_FOLLOW, NULL); + if (path.error) + { + set_errno (path.error); + syscall_printf ("-1 = chdir (%s)", dir); + return -1; + } + + /* Look for trailing path component consisting entirely of dots. This is needed only in case of chdir since Windows simply ignores count of dots > 2 here instead of returning an error code. Counts of dots