Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB <Address 0xblablabla out of bounds> error

Tags:

c

debugging

gdb

So, I'm learning how to program in C, and I'm having (or at elast, trying to) having some fun with GDB.

So I wrote this simple code:

#include <stdio.h> 

int main (int argc, char *argv[]){

int i;

int n = atoi(argv[2]); 

for (i=0; i<n ; i++){
    printf("%s \n",i+1,argv[1]); // prints the string provided in 
}                                // the arguments for n times
return 0;
}

and I was trying to usd GDB to obtain some infos on it. So I used it to try and get the arguments from the memory addresses, but this is what I get:

(gdb) break main
Breakpoint 1 at 0x4005d7: file repeat2.c, line 14.
(gdb) break 17
Breakpoint 2 at 0x40062c: file repeat2.c, line 17.
(gdb) run hello 5
Starting program: /root/Scrivania/Programmazione/repeat2 hello 5
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000

Breakpoint 1, main (argc=3, argv=0x7fffffffe948) at repeat2.c:14
14      int n = atoi(argv[2]);
(gdb) cont
Continuing.
1    ------>     hello 
2    ------>     hello 
3    ------>     hello 
4    ------>     hello 
5    ------>     hello 

Breakpoint 2, main (argc=3, argv=0x7fffffffe948) at repeat2.c:18
18  return 0;
(gdb) x/3xw 0x7fffffffe948     (I try to read what argv contains)
0x7fffffffe948: 0xffffebbc  0x00007fff  0xffffebe3
(gdb) x/s 0xffffebbc           (I try to read one of the argoments in the array)
0xffffebbc:  <Address 0xffffebbc out of bounds>

Why do I keep getting this error? I'm on a 64-bit, and I'm using Kali Linux

The program, if compiled, works, it's just that I can't understand why I can't read those values with GDB.

like image 734
DoubleCat Avatar asked Jul 21 '14 13:07

DoubleCat


2 Answers

@DrakaSAN found the bug in your program. As for your gdb question:

x/3xw prints out 3 4-byte words. argv is an array of char * pointers. Since you're on a 64-bit system, pointers are 8 bytes, so instead of w you want to use g (giant, 8 bytes) or a (address), which will select the correct size automatically:

(gdb) break 7
Breakpoint 1 at 0x40058c: file repeat2.c, line 7.
(gdb) run hello 5
Starting program: /tmp/repeat2 hello 5

Breakpoint 1, main (argc=3, argv=0x7fffffffdfe8) at repeat2.c:7
7   int n = atoi(argv[2]); 
(gdb) x/3xg 0x7fffffffdfe8
0x7fffffffdfe8: 0x00007fffffffe365  0x00007fffffffe372
0x7fffffffdff8: 0x00007fffffffe378
(gdb) x/3xa 0x7fffffffdfe8
0x7fffffffdfe8: 0x7fffffffe365  0x7fffffffe372
0x7fffffffdff8: 0x7fffffffe378
(gdb) x/s 0x7fffffffe365
0x7fffffffe365: "/tmp/repeat2"
(gdb) x/s 0x7fffffffe372
0x7fffffffe372: "hello"
(gdb) x/s 0x7fffffffe378
0x7fffffffe378: "5"

Thanks to @adpeace for suggesting the a modifier.

like image 148
Mark Plotnick Avatar answered Oct 20 '22 20:10

Mark Plotnick


Welcome to SO, +1 for having a well asked first question.

printf("%s \n",i+1,argv[1]);

You try to put a int (i) when printf expect a string (%s). I think what you wanted to do was:

for (i=0; i<n ; i++)
{
    printf("%s \n", argv[1]);
}

Althought, I m surprised your compiler didn t scream at you for this.

(As a note... Kali Linux isn t supposed to be used as a development OS, you may want to use a Debian or Ubuntu...)

like image 45
DrakaSAN Avatar answered Oct 20 '22 20:10

DrakaSAN