From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29230 invoked by alias); 20 Oct 2012 00:29:04 -0000 Received: (qmail 29220 invoked by uid 22791); 20 Oct 2012 00:29:03 -0000 X-SWARE-Spam-Status: No, hits=-2.1 required=5.0 tests=AWL,BAYES_00,RCVD_IN_HOSTKARMA_NO,RP_MATCHES_RCVD,TW_CP,TW_XS X-Spam-Check-By: sourceware.org Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 20 Oct 2012 00:28:58 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id E46612E080; Fri, 19 Oct 2012 20:28:57 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id ynEA577PiZtm; Fri, 19 Oct 2012 20:28:57 -0400 (EDT) Received: from joel.gnat.com (localhost.localdomain [127.0.0.1]) by rock.gnat.com (Postfix) with ESMTP id AD33F1C7FE2; Fri, 19 Oct 2012 20:28:57 -0400 (EDT) Received: by joel.gnat.com (Postfix, from userid 1000) id 4512EC800D; Fri, 19 Oct 2012 17:28:46 -0700 (PDT) From: Joel Brobecker To: gdb-patches@sourceware.org Cc: Joel Brobecker Subject: [RFA/commit/Windows] run program with space in path to exe. Date: Sat, 20 Oct 2012 00:29:00 -0000 Message-Id: <1350692925-14181-1-git-send-email-brobecker@adacore.com> Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-10/txt/msg00366.txt.bz2 The following works... % gdb c:\path to exe\foo.exe (gdb) start ... unless a file or directory called "c:\path" or "c:\path to" exists. This is what happens in the latter case: (gdb) start [...] Error creating process C:\path to exe\foo.exe (error 193). This is because we are calling CreateProcess (et al) without specifying the lpApplicationName, so Windows determines the name of the executable using the second argument, which is the entire command line. This command line is a space-separated list of tokens, so the space in the path to the executable which potentially creates an ambiguity. The ambiguity is automatically resolved unless we're in the situation above. The solution, as suggested by the MSDN documentation for CreateProcess is to quote the executable name. gdb/ChangeLog: * windows-nat.c (windows_create_inferior) [!__CYGWIN__]: New local variable args_len. Quote the name of the executable when computing the command line. Tested on x86-windows. Fixes the problem, with no regression. A few remarks on the patch: 1. As shown in the ChangeLog entry, this only deal with the MinGW case. I think we have the same issue with cygwin, except it is a little more complicated, because there are two cases: startup with shell, vs startup without shell, multiplied by wide characters vs non-wide characters. It'd be nice if someone who uses the cygwin version were to fix the problem there as well. I don't mind writing a patch, but it will be just barely tested, since I do not have a testing environment for cygwin. I'm also waiting to see if this patch is accepted. 2. It seemed much simpler to use xsnprintf rather than strcat to compute the command line, especially now that it has a few more elements in it. So I used that instead. 3. Theoretically, I have a feeling that we're breaking the case where the executable name contains a double quote in it. But, I think that's not something anyone would do, and I don't think I'd want to complexify the current code just to handle this theoretical case, particularly since it didn't work anyway! $ gdb -q 'foo"bar.exe' foo"bar.exe: No such file or directory. So, I don't think anyone is going to complain... Any objections this patch? Thanks, -- Joel --- gdb/windows-nat.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index 905d4bf..5eb8f67 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -2036,6 +2036,7 @@ windows_create_inferior (struct target_ops *ops, char *exec_file, char shell[__PMAX]; /* Path to shell */ char *toexec; char *args; + size_t args_len; HANDLE tty; char *w32env; char *temp; @@ -2188,10 +2189,13 @@ windows_create_inferior (struct target_ops *ops, char *exec_file, } #else toexec = exec_file; - args = alloca (strlen (toexec) + strlen (allargs) + 2); - strcpy (args, toexec); - strcat (args, " "); - strcat (args, allargs); + /* Build the command line, a space-separated list of tokens where + the first token is the name of the module to be executed. + To avoid ambiguities introduced by spaces in the module name, + we quote it. */ + args_len = strlen (toexec) + 2 /* quotes */ + strlen (allargs) + 2; + args = alloca (args_len); + xsnprintf (args, args_len, "\"%s\" %s", toexec, allargs); flags |= DEBUG_ONLY_THIS_PROCESS; -- 1.7.9.5