Re: The return address of strtok is out of bounds in gdb

Yao Qi
2015-03-17T11:06:08Z

Zhang Zhen <zhenzhang.zhang@huawei.com> writes:

> I found a problem with gdb-7.9 on my x86_64 machine.
> The return address is out of bounds by calling call strtok in gdb.
> But if we enter 'n', the return address is correct.
> I want to know this is a bug ? If so, how to resolve it ?

It is not a bug, IMO.

>
> It is easily reproduced as follows:
>
> Fs-Server:/opt/zhangzhen/gdb-7.9 # ./gdb/gdb -q ../strtok_test
> Reading symbols from ../strtok_test...done.
> (gdb) b 12
> Breakpoint 1 at 0x4005c7: file strtok_test.c, line 12.
> (gdb) r
> Starting program: /opt/zhangzhen/strtok_test
>
> Breakpoint 1, main (argc=1, argv=0x7fffffffe358) at strtok_test.c:12
> 12 p1 = strtok(a0, se);
> (gdb) p p1
> $1 = 0x0
> (gdb) p p1 = strtok(a0, se)
> $2 = 0xffffffffffffe260 <error: Cannot access memory at address 0xffffffffffffe260>

You are doing an "inferior call"
https://sourceware.org/gdb/onlinedocs/gdb/Calling.html here. In order
to support inferior call, GDB needs to create a new frame, get the
function's signature (return value and arguments), prepare the
arguments in the right place (registers or stack) as well as return
address, and resume the programme, wait for the function call finished.

In your case, I suspect GDB prepares the incorrect arguments for
function strtok due to lack of debugging information, so you'll see
the error.

You can get your libc debug info installed, or wrap up strktok like
this in your program,

char *
my_strtok(char *str, const char *delim)
{
return strtok (str, delim);
}

and in gdb,

(gdb) p p1 = my_strtok(a0, se)

--
Yao (齐尧)