The return address of strtok is out of bounds in gdb

Zhang Zhen
2015-03-17T08:47:14Z

Hi,

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 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>
(gdb) n
13 printf("a0=%s\np1=%p\n", a0, p1);
(gdb) p p1
$3 = 0x7fffffffe260 "start"
(gdb)

The source code of strtok_test is:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 int main(int argc, const char **argv)
6 {
7 char a0[] = {"start test! "};
8
9 char *p1 = NULL;
10 char se[] = " ";
11
12 p1 = strtok(a0, se);
13 printf("a0=%s\np1=%p\n", a0, p1);
14
15 return 0;
16 }

Best regards!