summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-07-08 06:58:37 -0700
committerKaz Kylheku <kaz@kylheku.com>2020-10-11 09:06:19 -0700
commite165539393574641aafceae90a22aee64e9c87ce (patch)
treefaa799497b51acf4447a80fb524a40689511a4c7
parentd82e2f3c1412acda563eaf11ac7aeed0e58f5687 (diff)
downloadcygnal-e165539393574641aafceae90a22aee64e9c87ce.tar.gz
cygnal-e165539393574641aafceae90a22aee64e9c87ce.tar.bz2
cygnal-e165539393574641aafceae90a22aee64e9c87ce.zip
Small fixes in get_cmd_exe_path.
* winsup/cygwin/spawn.cc (init_cmd_exe_path): Restructure code to initialize rather than assign nchars. Include backslash in cmd.exe name; then it can be omitted from the size calculation and sprintf. Do not allocate an excess byte for the string. Thanks to user forsvarir of the code review stackechange. Also reformatted to the GNU style used inside Cygwin. (init_cmd_exe_path): Remove spurious whitespace.
-rw-r--r--winsup/cygwin/spawn.cc24
1 files changed, 13 insertions, 11 deletions
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 5c67f10a6..d534322d1 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -1430,23 +1430,25 @@ __posix_spawn_execvpe (const char *path, char * const *argv, char *const *envp,
static const char *cmd_exe_path = NULL;
-static void init_cmd_exe_path(void)
+static void
+init_cmd_exe_path(void)
{
char sysdir[NT_MAX_PATH];
- char cmdname[] = "cmd.exe";
- unsigned int nchars;
-
- if ((nchars = GetSystemDirectoryA(sysdir, sizeof sysdir)) < sizeof sysdir) {
- unsigned int total = nchars + 1 + sizeof cmdname + 1;
- char *path = static_cast<char *>(cmalloc_abort(HEAP_STR, total));
- snprintf(path, total, "%s\\%s", sysdir, cmdname);
- cmd_exe_path = path;
- }
+ char cmdname[] = "\\cmd.exe";
+ unsigned int nchars = GetSystemDirectoryA(sysdir, sizeof sysdir);
+
+ if (nchars < sizeof sysdir)
+ {
+ unsigned int total = nchars + sizeof cmdname;
+ char *path = static_cast<char *>(cmalloc_abort(HEAP_STR, total));
+ snprintf(path, total, "%s%s", sysdir, cmdname);
+ cmd_exe_path = path;
+ }
}
const char *get_cmd_exe_path()
{
static pthread_once_t cmd_exe_once = PTHREAD_ONCE_INIT;
- pthread_once (&cmd_exe_once, init_cmd_exe_path);
+ pthread_once(&cmd_exe_once, init_cmd_exe_path);
return cmd_exe_path;
}