From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18769 invoked by alias); 21 Aug 2008 01:56:52 -0000 Received: (qmail 18609 invoked by uid 22791); 21 Aug 2008 01:56:51 -0000 X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (66.187.233.31) by sourceware.org (qpsmtpd/0.31) with ESMTP; Thu, 21 Aug 2008 01:56:17 +0000 Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id m7L1uF0S009858 for ; Wed, 20 Aug 2008 21:56:15 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx1.corp.redhat.com (8.13.1/8.13.1) with ESMTP id m7L1uE8O000985 for ; Wed, 20 Aug 2008 21:56:14 -0400 Received: from opsy.redhat.com (vpn-10-77.bos.redhat.com [10.16.10.77]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id m7L1uDPd023013; Wed, 20 Aug 2008 21:56:13 -0400 Received: by opsy.redhat.com (Postfix, from userid 500) id 2CD3E3784AC; Wed, 20 Aug 2008 19:56:21 -0600 (MDT) To: gdb-patches@sourceware.org Subject: RFA: fix PR 2506 From: Tom Tromey Reply-To: tromey@redhat.com X-Attribution: Tom Date: Thu, 21 Aug 2008 01:56:00 -0000 Message-ID: User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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: 2008-08/txt/msg00553.txt.bz2 PR 2506 is about string concatenation. Something like "x" "y" is a valid C expression. However, gdb does not parse it. The simplest fix was to add a new production that handles string concatenation. I looked at doing this in the lexer, but that is tricky due to macro expansion. The memory allocation is handled the way it is in order to ensure that strings are always freed. (If we allocated a new string in the lexer it would leak if the string occurred in a syntactically invalid place.) Built & regtested on x86-64 (compile farm). New test case included. Ok? Tom 2008-08-20 Tom Tromey PR gdb/2506: * c-exp.y (string_exp): New production. (exp): Use it. 2008-08-20 Tom Tromey * gdb.base/exprs.exp (test_expr): Add test for string concatenation. diff --git a/gdb/c-exp.y b/gdb/c-exp.y index bd04dc2..be79f15 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -182,7 +182,7 @@ static int parse_number (char *, int, int, YYSTYPE *); %token NAME /* BLOCKNAME defined below to give it higher precedence. */ %token COMPLETE %token TYPENAME -%type name +%type name string_exp %type name_not_typename %type typename @@ -560,7 +560,36 @@ exp : SIZEOF '(' type ')' %prec UNARY write_exp_elt_opcode (OP_LONG); } ; -exp : STRING +string_exp: + STRING + { + /* We copy the string here, and not in the + lexer, to guarantee that we do not leak a + string. Note that we follow the + NUL-termination convention of the + lexer. */ + $$.length = $1.length; + $$.ptr = xmalloc ($1.length + 1); + memcpy ($$.ptr, $1.ptr, $1.length + 1); + } + + | string_exp STRING + { + /* Note that we NUL-terminate here, but just + for convenience. */ + struct stoken t; + t.length = $1.length + $2.length; + t.ptr = xmalloc (t.length + 1); + memcpy (t.ptr, $1.ptr, $1.length); + memcpy (t.ptr + $1.length, $2.ptr, $2.length); + t.ptr[t.length] = '\0'; + xfree ($1.ptr); + xfree ($2.ptr); + $$ = t; + } + ; + +exp : string_exp { /* C strings are converted into array constants with an explicit null byte added at the end. Thus the array upper bound is the string length. @@ -581,7 +610,9 @@ exp : STRING write_exp_elt_opcode (OP_ARRAY); write_exp_elt_longcst ((LONGEST) 0); write_exp_elt_longcst ((LONGEST) ($1.length)); - write_exp_elt_opcode (OP_ARRAY); } + write_exp_elt_opcode (OP_ARRAY); + xfree ($1.ptr); + } ; /* C++. */ diff --git a/gdb/testsuite/gdb.base/exprs.exp b/gdb/testsuite/gdb.base/exprs.exp index e25de77..ad60aad 100644 --- a/gdb/testsuite/gdb.base/exprs.exp +++ b/gdb/testsuite/gdb.base/exprs.exp @@ -248,3 +248,6 @@ gdb_test "print (void*) ((long long) (unsigned long) -1 + 1)" \ if [expr ! $ok] { setup_xfail "*-*-*" } gdb_test "print (void*) (~((long long)(unsigned long) -1) - 1)" \ "warning: value truncated.*" "truncate (void*) 0xffffffff00000000 - 1" + +# String concatentation. +test_expr "print \"x\" \"y\"" "\\$\[0-9\]* = \"xy\""