Bug 24789

Summary: gdb thinks an int is not an int
Product: gdb Reporter: Hi-Angel <hi-angel>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: ssbssa, tromey
Priority: P2    
Version: 8.3   
Target Milestone: 10.1   
Host: Target:
Build: Last reconfirmed:

Description Hi-Angel 2019-07-09 07:44:28 UTC
# Steps to reproduce (in terms of terminal commands)

    $ cat -n test.cpp
         1  #include <cstdio>
         2  #include <utility>
         3
         4  using namespace std;
         5
         6  int main() {
         7      pair<int,int> mypair = {10, 10};
         8      auto [myint1,myint2] = mypair;
         9      char* ptr = nullptr;
        10      printf("%p\n", ptr + myint1);
        11  }
    $ g++ test.cpp -o a -g3 -O0 -std=c++17
    $ gdb ./a                             
    Reading symbols from ./a...
    gdb λ br 10
    Breakpoint 1 at 0x11a6: file test.cpp, line 10.
    gdb λ r
    Starting program: /tmp/a 

    Breakpoint 1, main () at test.cpp:10
    10          printf("%p\n", ptr + myint1);
    gdb λ p ptr + myint1

# Expected
    $1 = 0xa <error: Cannot access memory at address 0xa>

# Actual
    Argument to arithmetic operation not a number or boolean.

# Possible workarounds

Explicitly casting the int to int makes it work:

    gdb λ p ptr + (int)myint1
    $1 = 0xa <error: Cannot access memory at address 0xa>
Comment 1 Sourceware Commits 2020-04-01 17:16:38 UTC
The master branch has been updated by Hannes Domani <ssbssa@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=60e22c1eacb0df32aeeeb78c53cfd46c53a3770f

commit 60e22c1eacb0df32aeeeb78c53cfd46c53a3770f
Author: Hannes Domani <ssbssa@yahoo.de>
Date:   Tue Mar 31 14:49:06 2020 +0200

    Allow pointer arithmetic with integer references
    
    Considering these variables:
    int i = 3;
    int &iref = i;
    
    It's not possible to do any pointer arithmetic with iref:
    (gdb) p &i+iref
    Argument to arithmetic operation not a number or boolean.
    
    So this adds checks for references to integers in pointer arithmetic.
    
    gdb/ChangeLog:
    
    2020-04-01  Hannes Domani  <ssbssa@yahoo.de>
    
            PR gdb/24789
            * eval.c (is_integral_or_integral_reference): New function.
            (evaluate_subexp_standard): Allow integer references in
            pointer arithmetic.
    
    gdb/testsuite/ChangeLog:
    
    2020-04-01  Hannes Domani  <ssbssa@yahoo.de>
    
            PR gdb/24789
            * gdb.cp/misc.cc: Add integer reference variable.
            * gdb.cp/misc.exp: Add test.
Comment 2 Tom Tromey 2020-04-01 18:07:02 UTC
Fixed.