From patchwork Wed Sep 27 17:27:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 76790 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B6A3F387541E for ; Wed, 27 Sep 2023 17:28:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B6A3F387541E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1695835718; bh=DpUvlrQbepz882xS6zMkjNs73A0ENg9b34IBsupWyyc=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=dLoHyel88o5pxLLBjJ/cr1jLrIiv5w/qlWSXsVGZa2aw4E9UEaqS1vQJSmlridTb2 jQy0hJEG6PQW/2xSg4kZkTpBicnaGR8DyibocTH5eWwN6Ua+UQDQd8uNqNe5xnNmRh KNtaYA8rGesJQig3TMIDg17kvJpj1uip18sPBXDU= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id C8AD7385E03D for ; Wed, 27 Sep 2023 17:28:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C8AD7385E03D Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-176-l7yLvrxwMUKW7GxXkHIOQA-1; Wed, 27 Sep 2023 13:28:06 -0400 X-MC-Unique: l7yLvrxwMUKW7GxXkHIOQA-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7740c35073bso2324634685a.3 for ; Wed, 27 Sep 2023 10:28:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695835685; x=1696440485; 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=DpUvlrQbepz882xS6zMkjNs73A0ENg9b34IBsupWyyc=; b=k5CzuKjgnARhAbjlMw9GDWYUuRqlltRb5xFXhT6Btl1Wi1KaM197RTLokDOdd+m9ht kN4Nya/HEHseaE0M7Md4iH1o24gZkWIz3nN3iTAemCd65Z0oHHYFJOgU7eVYhkBw8Ao5 AQkhHDUtzTGKyGFq2WuTjXQy/9gOKB5tW90ru5Q7wQEGlRUDKevpWdsc4zvNRg/qZFpj kNDqppKhXIl7Yl/dLpEFCJkzlmgYRQMaF/z6fo04qnCOXV7I/vYFzPbyzmUdqZ0hF40l NztyauvISelqq1m5Tjs8lc+/KYyw2ldN5UY0y2e7YThRzInil239ASDUUcO29NFgdR1B kllg== X-Gm-Message-State: AOJu0YyijtdUuDgSI3ZsOeBjSLgRlSzafNsrxRWCa2o2GKFdw3q8jRty bFwicst5NdRrbhstjBf37vxTl25fqxmk6cPLSzGq3TdirRIbgXbeyorgQDwlm2imOAwg/yC5+zi 0DeH9Fk8djbrSyGSUGSkab7d332azR0uQrOonwdzrrP0u1w5gIcTB7TRp6VJpd6jeY+p7LdVbdc A5V0O7Yw== X-Received: by 2002:a05:620a:254b:b0:76f:135d:ce08 with SMTP id s11-20020a05620a254b00b0076f135dce08mr2954064qko.57.1695835685470; Wed, 27 Sep 2023 10:28:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGIxGFywhkj2DTQYN7h7FvZy0WrXtRBPgSogiOr7LkA5EB0J7Qm8p3WDDRQkA4OMibPuffdLw== X-Received: by 2002:a05:620a:254b:b0:76f:135d:ce08 with SMTP id s11-20020a05620a254b00b0076f135dce08mr2954043qko.57.1695835685080; Wed, 27 Sep 2023 10:28:05 -0700 (PDT) Received: from localhost ([31.111.84.209]) by smtp.gmail.com with ESMTPSA id 17-20020a05620a06d100b00767d2870e39sm3589098qky.41.2023.09.27.10.28.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 10:28:04 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Andrew Burgess , Andreas Schwab Subject: [PATCHv2 2/5] gdbserver: fix handling of trailing empty argument Date: Wed, 27 Sep 2023 18:27:55 +0100 Message-Id: <3485c21f838f6df48b865663aa637d21d6656661.1695835626.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: <2b98ca58e47638b4760d86bd6e1fa9a9a79fa2ad.1695817255.git.aburgess@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.9 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_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" When I posted the previous patch for review Andreas Schwab pointed out that passing a trailing empty argument also doesn't work. The fix for this is in the same area of code as the previous patch, but is sufficiently different that I felt it deserved a patch of its own. I noticed that passing arguments containing single quotes to gdbserver didn't work correctly: gdb -ex 'set sysroot' --args /tmp/show-args Reading symbols from /tmp/show-args... (gdb) target extended-remote | gdbserver --once --multi - /tmp/show-args Remote debugging using | gdbserver --once --multi - /tmp/show-args stdin/stdout redirected Process /tmp/show-args created; pid = 176054 Remote debugging using stdio Reading symbols from /lib64/ld-linux-x86-64.so.2... (No debugging symbols found in /lib64/ld-linux-x86-64.so.2) 0x00007ffff7fd3110 in _start () from /lib64/ld-linux-x86-64.so.2 (gdb) set args abc "" (gdb) run The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /tmp/show-args \' stdin/stdout redirected Process /tmp/show-args created; pid = 176088 2 args are: /tmp/show-args abc Done. [Inferior 1 (process 176088) exited normally] (gdb) target native Done. Use the "run" command to start a process. (gdb) run Starting program: /tmp/show-args \' 2 args are: /tmp/show-args abc Done. [Inferior 1 (process 176095) exited normally] (gdb) q The 'shows-args' program used here just prints the arguments passed to the inferior. Notice that when starting the inferior using the extended-remote target there is only a single argument 'abc', while when using the native target there is a second argument, the blank line, representing the empty argument. The problem here is that the vRun packet coming from GDB looks like this (I've removing the trailing checksum): $vRun;PROGRAM_NAME;616263; If we compare this to a packet with only a single argument and no trailing empty argument: $vRun;PROGRAM_NAME;616263 Notice the lack of the trailing ';' character here. The problem is that gdbserver processes this string in a loop. At each point we maintain a pointer to the character just after a ';', and then we process everything up to either the next ';' character, or to the end of the string. We break out of this loop when the character we start with (in that loop iteration) is the null-character. This means in the trailing empty argument case, we abort the loop before doing anything with the empty argument. In this commit I've updated the loop, we now break out using a 'break' statement at the end of the loop if the (sub-)string we just processed was empty, with this change we now notice the trailing empty argument. I've updated the test case to cover this issue. --- gdb/testsuite/gdb.base/inferior-args.exp | 9 ++++++--- gdbserver/server.cc | 8 +++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/gdb/testsuite/gdb.base/inferior-args.exp b/gdb/testsuite/gdb.base/inferior-args.exp index 3d3cd39a706..2c920ab14ec 100644 --- a/gdb/testsuite/gdb.base/inferior-args.exp +++ b/gdb/testsuite/gdb.base/inferior-args.exp @@ -28,8 +28,10 @@ if {[build_executable "failed to prepare" $testfile $srcfile \ proc do_test { method } { global binfile hex - # The second arg is an empty string on purpose. - set inferior_args { "first arg" "" "third-arg" "'" "\"" " " } + # The second arg is an empty string on purpose. The last argument + # must be the empty argument -- we once had a bug where that + # wouldn't work! + set inferior_args { "first arg" "" "third-arg" "'" "\"" " " "" } clean_restart $binfile @@ -109,7 +111,7 @@ proc do_test { method } { } # Now that we are stopped at main, inspect argc/argv. - gdb_test "print argc" " = 7" + gdb_test "print argc" " = 8" gdb_test "print argv\[0\]" " = $hex \".*\"" gdb_test "print argv\[1\]" " = $hex \"first arg\"" gdb_test "print argv\[2\]" " = $hex \"\"" @@ -117,6 +119,7 @@ proc do_test { method } { gdb_test "print argv\[4\]" " = $hex \"'\"" gdb_test "print argv\[5\]" " = $hex \"\\\\\"\"" gdb_test "print argv\[6\]" " = $hex \" \"" + gdb_test "print argv\[7\]" " = $hex \"\"" } foreach_with_prefix method { "start" "starti" "run" "set args" } { diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 496b9bebb7d..d78eb5a7d94 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -2969,7 +2969,9 @@ handle_v_run (char *own_buf) char *new_program_name = NULL; int i; - for (i = 0, p = own_buf + strlen ("vRun;"); *p; p = next_p, ++i) + for (i = 0, p = own_buf + strlen ("vRun;"); + /* Exit condition is at the end of the loop. */; + p = next_p + 1, ++i) { next_p = strchr (p, ';'); if (next_p == NULL) @@ -3032,8 +3034,8 @@ handle_v_run (char *own_buf) new_argv.push_back (full_arg); xfree (arg); } - if (*next_p) - next_p++; + if (*next_p == '\0') + break; } if (new_program_name == NULL)