Discussion:
[Bug python/20622] New: Misleading error for pointer arithmetic with float operand from Python3
jwakely.gcc at gmail dot com
2016-09-19 16:54:40 UTC
Permalink
https://sourceware.org/bugzilla/show_bug.cgi?id=20622

Bug ID: 20622
Summary: Misleading error for pointer arithmetic with float
operand from Python3
Product: gdb
Version: 7.10.1
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: python
Assignee: unassigned at sourceware dot org
Reporter: jwakely.gcc at gmail dot com
Target Milestone: ---

tmp$ cat ptr.c
int main()
{
int i = 0;
int* p = &i;
return *p;
}

tmp$ gcc -g ptr.c
tmp$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x4004fa: file ptr.c, line 3.
Starting program: /tmp/a.out

Temporary breakpoint 1, main () at ptr.c:3
3 int i = 0;
(gdb) n
4 int* p = &i;
(gdb) n
5 return *p;
(gdb) python p = gdb.parse_and_eval('p') ; offset = 1/4 ; p = p + offset
Traceback (most recent call last):
File "<string>", line 1, in <module>
gdb.error: Argument to arithmetic operation not a number or boolean.
Error while executing Python code.

The error comes from
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=gdb/valarith.c;h=de6fcfd79abb2f3b6a1b341c36736ac949826cf3;hb=HEAD#l960

"Not a number" is highly confusing, because it certainly is a number. The
problem is that with Python 3 it's a float:

(gdb) python print(type(offset))
<class 'float'>
(gdb) python p + int(offset)
(gdb) # No error this time.

This breaks some libstdc++ printers. They used to work with Python2 where
type(1/4) is int, but using Python 3 the float type gives the "not a number"
error.

I don't know if GDB's scalar_binop function should accept Python floats, or the
conversion from Python values to GDB values should convert to TYPE_CODE_FLT, or
something else.
--
You are receiving this mail because:
You are on the CC list for the bug.
jwakely.gcc at gmail dot com
2016-09-20 13:46:36 UTC
Permalink
https://sourceware.org/bugzilla/show_bug.cgi?id=20622

--- Comment #1 from Jonathan Wakely <jwakely.gcc at gmail dot com> ---
Due to PR 20624 if the offset is calculated using a gdb.Value then the pointer
arithmetic does work (because gdb.Value division is always integer division):

(gdb) python n = 1 ; offset = n / 4; p = p + offset
Traceback (most recent call last):
File "<string>", line 1, in <module>
gdb.error: Argument to arithmetic operation not a number or boolean.
Error while executing Python code.
(gdb) python n = gdb.Value(1) ; offset = n / 4; p = p + offset
(gdb)

I now think the right fix for this bug is not to make the arithmetic work, but
just to change the error message to say "not an integer number or boolean."
--
You are receiving this mail because:
You are on the CC list for the bug.
jwakely.gcc at gmail dot com
2016-09-20 14:54:31 UTC
Permalink
https://sourceware.org/bugzilla/show_bug.cgi?id=20622

--- Comment #2 from Jonathan Wakely <jwakely.gcc at gmail dot com> ---
(In reply to Jonathan Wakely from comment #1)
Post by jwakely.gcc at gmail dot com
I now think the right fix for this bug is not to make the arithmetic work,
but just to change the error message to say "not an integer number or
boolean."
Since that error comes from value_binop which isn't specific to the Python API
we probably don't want to change it. I've posted a patch to
https://sourceware.org/ml/gdb-patches/2016-09/msg00222.html which does
additional checking on the argument types before calling value_binop and fails
sooner with a more specific error message.
--
You are receiving this mail because:
You are on the CC list for the bug.
tromey at sourceware dot org
2018-09-15 00:06:14 UTC
Permalink
https://sourceware.org/bugzilla/show_bug.cgi?id=20622

Tom Tromey <tromey at sourceware dot org> changed:

What |Removed |Added
----------------------------------------------------------------------------
CC| |tromey at sourceware dot org

--- Comment #3 from Tom Tromey <tromey at sourceware dot org> ---
What happened with this patch?
--
You are receiving this mail because:
You are on the CC list for the bug.
Loading...