From patchwork Wed Sep 21 11:07:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 15836 Received: (qmail 85168 invoked by alias); 21 Sep 2016 11:07:15 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 85150 invoked by uid 89); 21 Sep 2016 11:07:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.0 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 21 Sep 2016 11:07:12 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 84DF089C30 for ; Wed, 21 Sep 2016 11:07:11 +0000 (UTC) Received: from localhost (ovpn-116-66.ams2.redhat.com [10.36.116.66]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u8LB7ADJ007333; Wed, 21 Sep 2016 07:07:11 -0400 Date: Wed, 21 Sep 2016 12:07:10 +0100 From: Jonathan Wakely To: Pedro Alves Cc: gdb-patches@sourceware.org Subject: Re: [PATCH] Diagnose invalid pointer arithmetic on gdb.Value Message-ID: <20160921110710.GN17376@redhat.com> References: <20160920144601.GA3459@redhat.com> <20160920161936.GA5736@redhat.com> <3e1ff6fd-4711-5fb5-251b-de7ea0db6891@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <3e1ff6fd-4711-5fb5-251b-de7ea0db6891@redhat.com> X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.7.0 (2016-08-17) On 20/09/16 17:42 +0100, Pedro Alves wrote: >On 09/20/2016 05:19 PM, Jonathan Wakely wrote: >> On 20/09/16 16:41 +0100, Pedro Alves wrote: >>> On 09/20/2016 03:46 PM, Jonathan Wakely wrote: >>>> Instead of passing invalid arguments to value_binop and getting a >>>> misleading error, raise a TypeError directly in valpy_binop_throw. >>> >>> Did you try changing value_binop instead? The error string seems >>> misleading even in C: >>> >>> (gdb) p ptr >>> $1 = 0x601040 "" >>> (gdb) p ptr + 1 >>> $2 = 0x601041 "" >>> (gdb) p ptr + 1.0 >>> Argument to arithmetic operation not a number or boolean. >>> (gdb) >> >> Ah, so it's not specific to the Python API, in which case changing >> value_binop (or scalar_binop more accurately) might make sense. >> The check in scalar_binop *does* check for numbers, so the error is >> accurate. The argument that triggers the errors is actually the >> pointer argument, not the float one: >> >> if ((TYPE_CODE (type1) != TYPE_CODE_FLT >> && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT >> && !is_integral_type (type1)) >> || (TYPE_CODE (type2) != TYPE_CODE_FLT >> && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT >> && !is_integral_type (type2))) >> error (_("Argument to arithmetic operation not a number or boolean.")); >> >> >> The problem is that pointer arithmetic with invalid operands ends up >> here, because value_ptr{add,sub,diff} don't get called for invalid >> arguments. >> >> So maybe a better fix is to first check if either argument is a >> pointer and give a more specific error, as attached (untested). >> > >Makes sense to me. Here's a patch to do that. None of the existing tests needed to change as far as I can see. gdb.base/pointers.exp and gdb.base/completion.exp have tests that try to match the old error, but they're commented out. I kept my additions to py-value.exp that do invalid pointer arithmetic, updating the expected text. Should I move that to gdb.base/pointers.exp instead? Or check it in both? commit 39448eaecd5f2e97c6f2c1566b7bae40fa63b2ff Author: Jonathan Wakely Date: Tue Sep 20 15:39:44 2016 +0100 gdb: Better error message for invalid pointer arithmetic The value_ptr{add,sub,diff} functions are only used when the arguments are suitable types, otherwise we end up in scalar_binop so add a more helpful error message there. gdb/ChangeLog: 2016-09-21 Jonathan Wakely PR python/20622 * valarith.c (scalar_binop): If either argument is a pointer give a more descriptive error message. gdb/testsuite/ChangeLog: 2016-09-21 Jonathan Wakely PR python/20622 * gdb.python/py-value.exp: Test invalid pointer arithmetic. diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp index 57a9ba1..70732cc 100644 --- a/gdb/testsuite/gdb.python/py-value.exp +++ b/gdb/testsuite/gdb.python/py-value.exp @@ -144,6 +144,24 @@ proc test_value_numeric_ops {} { -re "result = .*$gdb_prompt $" {fail "catch throw of GDB error"} -re "$gdb_prompt $" {fail "catch throw of GDB error"} } + + gdb_test_multiple "python print ('result = ' + str(a+0.5))" "catch error in python type conversion" { + -re "Invalid arguments to pointer arithmetic operation..*$gdb_prompt $" {pass "catch error in python type conversion"} + -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"} + -re "$gdb_prompt $" {fail "catch error in python type conversion"} + } + + gdb_test_multiple "python print ('result = ' + str(0.5+a))" "catch error in python type conversion" { + -re "Invalid arguments to pointer arithmetic operation..*$gdb_prompt $" {pass "catch error in python type conversion"} + -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"} + -re "$gdb_prompt $" {fail "catch error in python type conversion"} + } + + gdb_test_multiple "python print ('result = ' + str(a-0.5))" "catch error in python type conversion" { + -re "Invalid arguments to pointer arithmetic operation..*$gdb_prompt $" {pass "catch error in python type conversion"} + -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"} + -re "$gdb_prompt $" {fail "catch error in python type conversion"} + } } proc test_value_boolean {} { diff --git a/gdb/valarith.c b/gdb/valarith.c index de6fcfd..546d4b6 100644 --- a/gdb/valarith.c +++ b/gdb/valarith.c @@ -951,7 +951,9 @@ scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) type1 = check_typedef (value_type (arg1)); type2 = check_typedef (value_type (arg2)); - if ((TYPE_CODE (type1) != TYPE_CODE_FLT + if (TYPE_CODE (type1) == TYPE_CODE_PTR || TYPE_CODE (type2) == TYPE_CODE_PTR) + error (_("Invalid arguments to pointer arithmetic operation.")); + else if ((TYPE_CODE (type1) != TYPE_CODE_FLT && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT && !is_integral_type (type1)) || (TYPE_CODE (type2) != TYPE_CODE_FLT