From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31743 invoked by alias); 7 May 2014 06:56:29 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 31682 invoked by uid 89); 7 May 2014 06:56:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-wi0-f175.google.com Received: from mail-wi0-f175.google.com (HELO mail-wi0-f175.google.com) (209.85.212.175) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 07 May 2014 06:56:28 +0000 Received: by mail-wi0-f175.google.com with SMTP id f8so4745489wiw.14 for ; Tue, 06 May 2014 23:56:25 -0700 (PDT) X-Received: by 10.180.74.132 with SMTP id t4mr6138637wiv.30.1399445785445; Tue, 06 May 2014 23:56:25 -0700 (PDT) Received: from localhost.localdomain (cpc65057-bagu12-2-0-cust801.1-3.cable.virginm.net. [82.1.59.34]) by mx.google.com with ESMTPSA id gc19sm29624360wic.5.2014.05.06.23.56.24 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 06 May 2014 23:56:24 -0700 (PDT) From: Ray Donnelly To: gcc-patches@gcc.gnu.org Cc: ktietz70@gmail.com, Ray Donnelly Subject: [PATCH] Windows libibery: Don't quote args unnecessarily Date: Wed, 07 May 2014 06:56:00 -0000 Message-Id: <1399445760-4060-2-git-send-email-mingw.android@gmail.com> In-Reply-To: <1399445760-4060-1-git-send-email-mingw.android@gmail.com> References: <1397936424-2290-1-git-send-email-mingw.android@gmail.com> <1399445760-4060-1-git-send-email-mingw.android@gmail.com> X-SW-Source: 2014-05/txt/msg00365.txt.bz2 We only quote arguments that contain spaces, \t or " characters to prevent wasting 2 characters per argument of the CreateProcess() 32,768 limit. --- libiberty/pex-win32.c | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/libiberty/pex-win32.c b/libiberty/pex-win32.c index eae72c5..8b9d4f0 100644 --- a/libiberty/pex-win32.c +++ b/libiberty/pex-win32.c @@ -340,17 +340,25 @@ argv_to_cmdline (char *const *argv) char *p; size_t cmdline_len; int i, j, k; + int needs_quotes; cmdline_len = 0; for (i = 0; argv[i]; i++) { - /* We quote every last argument. This simplifies the problem; - we need only escape embedded double-quotes and immediately + /* We only quote arguments that contain spaces, \t or " characters to + prevent wasting 2 chars per argument of the CreateProcess 32k char + limit. We need only escape embedded double-quotes and immediately preceeding backslash characters. A sequence of backslach characters that is not follwed by a double quote character will not be escaped. */ + needs_quotes = 0; for (j = 0; argv[i][j]; j++) { + if (argv[i][j] == ' ' || argv[i][j] == '\t' || argv[i][j] == '"') + { + needs_quotes = 1; + } + if (argv[i][j] == '"') { /* Escape preceeding backslashes. */ @@ -362,16 +370,33 @@ argv_to_cmdline (char *const *argv) } /* Trailing backslashes also need to be escaped because they will be followed by the terminating quote. */ - for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--) - cmdline_len++; + if (needs_quotes) + { + for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--) + cmdline_len++; + } cmdline_len += j; - cmdline_len += 3; /* for leading and trailing quotes and space */ + /* for leading and trailing quotes and space */ + cmdline_len += needs_quotes * 2 + 1; } cmdline = XNEWVEC (char, cmdline_len); p = cmdline; for (i = 0; argv[i]; i++) { - *p++ = '"'; + needs_quotes = 0; + for (j = 0; argv[i][j]; j++) + { + if (argv[i][j] == ' ' || argv[i][j] == '\t' || argv[i][j] == '"') + { + needs_quotes = 1; + break; + } + } + + if (needs_quotes) + { + *p++ = '"'; + } for (j = 0; argv[i][j]; j++) { if (argv[i][j] == '"') @@ -382,9 +407,12 @@ argv_to_cmdline (char *const *argv) } *p++ = argv[i][j]; } - for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--) - *p++ = '\\'; - *p++ = '"'; + if (needs_quotes) + { + for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--) + *p++ = '\\'; + *p++ = '"'; + } *p++ = ' '; } p[-1] = '\0'; -- 1.9.2