From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 63ABF38582B0 for ; Tue, 9 Jan 2024 14:26:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63ABF38582B0 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 63ABF38582B0 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704810418; cv=none; b=DwnNrJz6dEixWAMTdEyMDAGRuMVTmSn+HzAkPplBX8wzBt7wYUZLaKEpwSo1IgLAUhOAkemuv2tknmYnoa+g8iotMT8GhnOc6dHVer04SDK9U4/IL6SnIbGtsYRcOD/jprvuOX+YsMayGf/DPvMNHH1pvjP/+x1cKggnvNwBzYU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704810418; c=relaxed/simple; bh=dJE7p7PX34zVVYrp8Q+50Ibi6E7eWyJLZRXx53S7DUE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=o8SqHJoIrGF8eGILpKUzP6uDRKhNl6n8AcoUp4i2O9+GRFSReYjh1s8SE0/inKCCUvebL0vFICRXy8P7XumS3f28Ob8cke8czlQO4pRcZkAe48Ms6EJUdKEixILvuQbtYyO68Qvzy8U/jZw44UNJF9UuVNmcNTthDl9KiPhVbqs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704810416; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T1p2z0cZ55nb7B40+fQX+qArCu9REZLI9TCzfUxWfeI=; b=KnjZHUO+bn9T/75pDS2rq3mpCWWFD0KWBZBeKG+iFXgo+GZ1S0pjvXGcQkRcxJbemei6Um DnBtJ3aSs1oSlO96wefx8IdMne2lBohUTvoD2I/xD36jujpvkauIRknjy2Kqtbi71k3NFc nYeStGTX3MXusa4/HGjWVdlVqrAqX4Y= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-689-0MjcbojfNtOWiThh0EWcdQ-1; Tue, 09 Jan 2024 09:26:54 -0500 X-MC-Unique: 0MjcbojfNtOWiThh0EWcdQ-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-33768a5aea7so1519773f8f.3 for ; Tue, 09 Jan 2024 06:26:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704810413; x=1705415213; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T1p2z0cZ55nb7B40+fQX+qArCu9REZLI9TCzfUxWfeI=; b=Hoe84fnMO+T7Ii7VcxXLdXe3Z+60hQX3iibcdGHbLXJ/H9mpNZn3vxN4WzB8NjVdUD ixQDfCzqT9LPFb8cGKLvKMTpMz8+/oyQjcsWyLhYHc0GVJFkbWqBNlPnemR4FGAb753I nvoVyJstJjg7gsz4I57azhhqZf924zCgH0Q5P3xYWSnbClEfK03Bx1HbUXSWwVH6wNId CY4p56waylKtbg8H2OZ8zyAZTkcZ4L4RpXM5NKW0iwT7TubAvV8kxWYFVEhBmBDGPBH4 ywRh8qecI+z55/SYpewsnZOWNQNW6xIxgqVxRtXM7BpjYa6nmB0oOvaYDFwLfFgXwuIp vFTg== X-Gm-Message-State: AOJu0Yzo8XRiG2j714RonU5FvkIzkmx9ldb+33wiAVllTfUmJdhqJPt9 y1XBUW5qRftpyNdvdEwq+2BBYF4wvY2f82u5Qiemd2p/9MvYsOmreZqtymdoNYN6pYzhUwu8QwR lW+U07H3ZcdH6tnrbJbQjkz/dAmAOWLjjuw5VniNQPZ0sviiVonDxBsdVikJDBQM+u5ZBsBDi0s 9gl1Wd6L7xduqHLA== X-Received: by 2002:adf:f343:0:b0:336:7632:cdbb with SMTP id e3-20020adff343000000b003367632cdbbmr663355wrp.22.1704810412841; Tue, 09 Jan 2024 06:26:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IFvNfpDGcuTySfMHC/LGsPTUEAYGj7t9NgY/s8Hkg4JJGVUlZkiuFwGapCxbNVXKtforguWEw== X-Received: by 2002:adf:f343:0:b0:336:7632:cdbb with SMTP id e3-20020adff343000000b003367632cdbbmr663348wrp.22.1704810412540; Tue, 09 Jan 2024 06:26:52 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id d7-20020adfe887000000b0033672971fabsm2528406wrm.115.2024.01.09.06.26.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 06:26:51 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Michael Weghorn Subject: [PATCH 07/16] gdbsupport: split escape_shell_characters in two Date: Tue, 9 Jan 2024 14:26:30 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Building on the previous commit, this commit performs further refactoring. The escape_shell_characters function is split into two. We have a worker core (the escape_characters function) which takes a set of "special" characters that need escaping, and applies that escaping. Then we have escape_shell_characters, which just calls escape_characters with the correct set of special characters. There should be no user visible changes after this commit. This commit is similar to some of the changes made in this series: https://inbox.sourceware.org/gdb-patches/20211022071933.3478427-1-m.weghorn@posteo.de/ Though I don't think there's one commit that is exactly the same as this one. But I've listed the author of the original series as a Co-Author, because I feel the work is similar enough. Co-Authored-By: Michael Weghorn --- gdbsupport/common-inferior.cc | 49 +++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/gdbsupport/common-inferior.cc b/gdbsupport/common-inferior.cc index f5620ec89aa..6717f7d5c08 100644 --- a/gdbsupport/common-inferior.cc +++ b/gdbsupport/common-inferior.cc @@ -44,23 +44,23 @@ construct_inferior_arguments (gdb::array_view argv, return result; } -/* See common-inferior.h. */ - -std::string -escape_shell_characters (const char *arg) +/* Escape characters in ARG and return an updated string. The string + SPECIAL contains the set of characters that must be escaped. SPECIAL + must not be nullptr, and it is assumed that SPECIAL contains the newline + '\n' character. It is assumed that ARG is not nullptr, but ARG can + be the empty string. */ + +static std::string +escape_characters (const char *arg, const char *special) { + gdb_assert (special != nullptr); + gdb_assert (arg != nullptr); + std::string result; #ifdef __MINGW32__ - /* This holds all the characters considered special to the - Windows shells. */ - static const char special[] = "\"!&*|[]{}<>?`~^=;, \t\n"; static const char quote = '"'; #else - /* This holds all the characters considered special to the - typical Unix shells. We include `^' because the SunOS - /bin/sh treats it as a synonym for `|'. */ - static const char special[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n"; static const char quote = '\''; #endif @@ -70,12 +70,16 @@ escape_shell_characters (const char *arg) result += quote; result += quote; } + /* The special character handling code here assumes that if SPECIAL is + not nullptr, then SPECIAL will contain '\n'. This is true for all our + current usages, but if this ever changes in the future the following + might need reworking. */ else { #ifdef __MINGW32__ bool quoted = false; - if (strpbrk (arg, special)) + if (strpbrk (argv[i], special)) { quoted = true; result += quote; @@ -96,7 +100,7 @@ escape_shell_characters (const char *arg) #ifdef __MINGW32__ if (*cp == quote) #else - if (strchr (special, *cp) != NULL) + if (strchr (special, *cp) != nullptr) #endif result += '\\'; result += *cp; @@ -110,3 +114,22 @@ escape_shell_characters (const char *arg) return result; } + +/* See common-inferior.h. */ + +std::string +escape_shell_characters (const char *arg) +{ +#ifdef __MINGW32__ + /* This holds all the characters considered special to the + Windows shells. */ + static const char special[] = "\"!&*|[]{}<>?`~^=;, \t\n"; +#else + /* This holds all the characters considered special to the + typical Unix shells. We include `^' because the SunOS + /bin/sh treats it as a synonym for `|'. */ + static const char special[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n"; +#endif + + return escape_characters (arg, special); +} -- 2.25.4