From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4735 invoked by alias); 6 May 2011 17:12:29 -0000 Received: (qmail 4723 invoked by uid 22791); 6 May 2011 17:12:27 -0000 X-SWARE-Spam-Status: No, hits=-6.8 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,TW_CP,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 06 May 2011 17:12:10 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p46HCAFV019178 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 6 May 2011 13:12:10 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p46HC96l017519; Fri, 6 May 2011 13:12:10 -0400 Received: from opsy.redhat.com (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id p46HC9l1012215; Fri, 6 May 2011 13:12:09 -0400 Received: by opsy.redhat.com (Postfix, from userid 500) id B66E537818E; Fri, 6 May 2011 11:12:08 -0600 (MDT) From: Tom Tromey To: gdb-patches@sourceware.org Subject: RFC: fix `gdb -write' case Date: Fri, 06 May 2011 17:12:00 -0000 Message-ID: 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: 2011-05/txt/msg00197.txt.bz2 I'd appreciate comments on this. In the absence of comments I will check it in. I plan to put it on the 7.3 branch as well. This comes from: https://bugzilla.redhat.com/show_bug.cgi?id=696148 The bug is that you cannot change a string in an executable with `gdb -write'. Jan tracked this down to the wide string change; the bug is that evaluate_subexp_c did not examine `expect_type', leading to an attempt to coerce the string to memory. Built and regtested by our internal buildbot. Tom 2011-05-06 Tom Tromey * c-lang.c (evaluate_subexp_c): Use expect_type if it is not NULL. diff --git a/gdb/c-lang.c b/gdb/c-lang.c index ad770bf..64c4d79 100644 --- a/gdb/c-lang.c +++ b/gdb/c-lang.c @@ -1061,9 +1061,39 @@ evaluate_subexp_c (struct type *expect_type, struct expression *exp, /* Write the terminating character. */ for (i = 0; i < TYPE_LENGTH (type); ++i) obstack_1grow (&output, 0); - result = value_cstring (obstack_base (&output), - obstack_object_size (&output), - type); + + if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_ARRAY) + { + LONGEST low_bound, high_bound; + struct type *element_type + = check_typedef (TYPE_TARGET_TYPE (expect_type)); + int element_size = TYPE_LENGTH (element_type); + + if (TYPE_CODE (element_type) != TYPE_CODE_INT + && (TYPE_CODE (element_type) != TYPE_CODE_CHAR)) + error (_("wrong expected type for string constant")); + if (TYPE_LENGTH (element_type) != TYPE_LENGTH (type)) + error (_("expected type of string constant has wrong " + "character width")); + + if (get_discrete_bounds (TYPE_INDEX_TYPE (expect_type), + &low_bound, &high_bound) < 0) + { + low_bound = 0; + high_bound = (TYPE_LENGTH (expect_type) / element_size) - 1; + } + if (obstack_object_size (&output) / TYPE_LENGTH (type) + > (high_bound - low_bound + 1)) + error (_("Too many array elements")); + + result = allocate_value (expect_type); + memcpy (value_contents_raw (result), obstack_base (&output), + obstack_object_size (&output)); + } + else + result = value_cstring (obstack_base (&output), + obstack_object_size (&output), + type); } do_cleanups (cleanup); return result;