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 767533858C74 for ; Tue, 9 Jan 2024 14:26:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 767533858C74 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 767533858C74 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=1704810408; cv=none; b=qCkxmdavcQtged+3Hj8nkK3/Ma+lh314rjlyeQZ4iR1KrYzMJpaNT1laCtnnthn587Y4jaVmqvzFRPHJf4IRK7KvrDO6ufwhaMGki94Vc+o00m2IE+oQxKO9PIZsaWETVSN8A/vy2+TnLcwLWsqgcDUcz4C/iiIZU563sf/VFZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704810408; c=relaxed/simple; bh=8VeJEcs+pOHv9lUHkcfDv6zWGnNtuHQ/JZHEsCeapr0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=xi6EGDoESEumNI4YlsVZNj1X8dkgjWfY+HxUOxVh/BB4c6LnCALa/FbD4XM76D5WebhF7Q1LwsgGHHVu3hh+sQtBF1nn9HZoW0u6vrmMe1WKvQGeXi+K7ZqdpLrtttftUrIe9NN3TVHDGun4a6NwH/T9W3DcEVyFf2/D6TiZPC4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704810406; 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=g38xUe9ECml6Nh56EQ3c6JMDrgfpsQ4fisuckU5t+Js=; b=L0P8GLnKhP+sqntpdI9b4hGU5UFLE5mqj5pnr1e3c0oSVgJd/eIhvjYz1cNSEE1QuvZy7G 7/E+9t2U7Xu6srIzrLCW2lslSCsrQ8anZmiIvt0kJkdPb8lsSuaNUKA4Z4ItaqhvvEaw3i bK8lSaT1p/mmk0zQX36hoWD8b+HuLuc= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-541-cpeaVr-qOWOSIsZNvAasjA-1; Tue, 09 Jan 2024 09:26:44 -0500 X-MC-Unique: cpeaVr-qOWOSIsZNvAasjA-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-40e49c3c124so11291095e9.1 for ; Tue, 09 Jan 2024 06:26:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704810403; x=1705415203; 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=g38xUe9ECml6Nh56EQ3c6JMDrgfpsQ4fisuckU5t+Js=; b=k+eDdMGm0WsvioKlL7tU9P0JPblJW3RWisD2Ac0yboPuYukQzBpJpDJaZwV1X53oKF XlciC0w1A/gcoOWzGT1EpXEI90xrm0fndvhL4j9/CRiePU0LSZ8KBcIf7A9I8efyPfw0 A8O/Ms9b4CytYLxjR6aejY2QkLZVgbaww2CGxuc9mePOBofKRiGes7BFgYgyMbSFC5QU aj9yD+llwUixu1y/s4BcMzb9sIkb0/V0Hxj12e7e7T3jcwwsd0c+fR4GZIYkW/I6muMK tJ5Cb0xW7SwgHRLM9XHLb5kjLa+dUpY4Ipwhl2n/sVpQyrO4HpQzaDvCoK8tNjiQUDx6 O6Iw== X-Gm-Message-State: AOJu0Yzdw7UUQmO8lb2QMxRUxxWwS9J/tJmz7BUWnyw0yKBWAA8LfGrv 8UQVSqHsVvX5WytOGbr3KtYx2DBlID81fXHMMPiCvdKa2vgmu0zkBsYvWgMmE3SKH04rfO7tU+X vzltiCrZNURFBcbd4Dy7iZoOIOdqctoAnhlwYD2QQsIjMaqe/mNEYdP7Y6CqXuE1d2s0Mr2VgKL 3qpgLjsiY/GtmYtw== X-Received: by 2002:a05:600c:3ba9:b0:40e:3663:88b6 with SMTP id n41-20020a05600c3ba900b0040e366388b6mr2818389wms.2.1704810403669; Tue, 09 Jan 2024 06:26:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IGgPer1nbz6ta/jEFgKykLuPa6NPGOCmq4kL+Eb5lcfMEnUPfCPn85IfzZ+KulG2sRkDtS7CA== X-Received: by 2002:a05:600c:3ba9:b0:40e:3663:88b6 with SMTP id n41-20020a05600c3ba900b0040e366388b6mr2818378wms.2.1704810403237; Tue, 09 Jan 2024 06:26:43 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id f13-20020a05600c154d00b0040e3635ca65sm15109838wmg.2.2024.01.09.06.26.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 06:26:42 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Michael Weghorn Subject: [PATCH 01/16] libiberty/buildargv: POSIX behaviour for backslash handling Date: Tue, 9 Jan 2024 14:26:24 +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: This is a libiberty patch. I've posted this to the gcc-patches list here: https://inbox.sourceware.org/gcc-patches/24a8d878590403540bc9b579ba58805985a4d2f7.1701881419.git.aburgess@redhat.com/ However, GCC is currently in stage 4 of its release cycle. Based on the timing of previous releases, I'm not expecting this patch to be merged before April. One option is clearly to just wait until GCC hits stage 1, and then try to get this patch merged. Another option would be to create a GDB only fork of buildargv which includes this patch. In April if/when I manage to get this patch merged I would remove out GDB local copy. Of course, there's a risk that this patch isn't accepted into GCC, in which case we might be stuck with a GDB only fork. Either way, I figure the first step is to address any issues that are raised with the rest of this series, this could well take until April anyway, in which case GCC might be back in stage 1. Thanks, Andrew --- GDB makes use of the libiberty function buildargv for splitting the inferior (program being debugged) argument string in some situations. I have recently been working to fix some edge cases issues in this area of GDB, and have tracked done some of the unexpected behaviour to the libiberty function buildargv, and how it handles backslash escapes. For reference, I've been mostly reading: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html The issues that I would like to fix are: 1. Backslashes within single quotes should not be treated as an escape, thus: '\a' should split to \a, retaining the backslash. 2. Backslashes within double quotes should only act as an escape if they are immediately before one of the characters $ (dollar), ` (backtick), " (double quote), ` (backslash), or \n (newline). In all other cases a backslash should not be treated as an escape character. Thus: "\a" should split to \a, but "\$" should split to $. 3. A backslash-newline sequence should be treated as a line continuation, both the backslash and the newline should be removed. I've updated libiberty and also added some tests. All the existing libiberty tests continue to pass, but I'm not sure if there is additional testing that should be done. --- libiberty/argv.c | 8 +++++-- libiberty/testsuite/test-expandargv.c | 34 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/libiberty/argv.c b/libiberty/argv.c index c2823d3e4ba..6bae4ca2ee9 100644 --- a/libiberty/argv.c +++ b/libiberty/argv.c @@ -224,9 +224,13 @@ char **buildargv (const char *input) if (bsquote) { bsquote = 0; - *arg++ = *input; + if (*input != '\n') + *arg++ = *input; } - else if (*input == '\\') + else if (*input == '\\' + && !squote + && (!dquote + || strchr ("$`\"\\\n", *(input + 1)) != NULL)) { bsquote = 1; } diff --git a/libiberty/testsuite/test-expandargv.c b/libiberty/testsuite/test-expandargv.c index 30f2337ef77..b8dcc6a269a 100644 --- a/libiberty/testsuite/test-expandargv.c +++ b/libiberty/testsuite/test-expandargv.c @@ -142,6 +142,40 @@ const char *test_data[] = { "b", 0, + /* Test 7 - No backslash removal within single quotes. */ + "'a\\$VAR' '\\\"'", /* Test 7 data */ + ARGV0, + "@test-expandargv-7.lst", + 0, + ARGV0, + "a\\$VAR", + "\\\"", + 0, + + /* Test 8 - Remove backslash / newline pairs. */ + "\"ab\\\ncd\" ef\\\ngh", /* Test 8 data */ + ARGV0, + "@test-expandargv-8.lst", + 0, + ARGV0, + "abcd", + "efgh", + 0, + + /* Test 9 - Backslash within double quotes. */ + "\"\\$VAR\" \"\\`\" \"\\\"\" \"\\\\\" \"\\n\" \"\\t\"", /* Test 9 data */ + ARGV0, + "@test-expandargv-9.lst", + 0, + ARGV0, + "$VAR", + "`", + "\"", + "\\", + "\\n", + "\\t", + 0, + 0 /* Test done marker, don't remove. */ }; -- 2.25.4