Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: Yao Qi <qiyaoltc@gmail.com>
To: Zhang Zhen <zhenzhang.zhang@huawei.com>
Cc: <gdb@sourceware.org>,  <xuhanbing@huawei.com>
Subject: Re: The return address of strtok is out of bounds in gdb
Date: Tue, 17 Mar 2015 11:06:00 -0000	[thread overview]
Message-ID: <86k2yfg9yi.fsf@gmail.com> (raw)
In-Reply-To: <5507E977.2030003@huawei.com> (Zhang Zhen's message of "Tue, 17	Mar 2015 16:44:39 +0800")

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 (齐尧)


  reply	other threads:[~2015-03-17 11:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-17  8:47 Zhang Zhen
2015-03-17 11:06 ` Yao Qi [this message]
2015-03-17 11:26   ` Andreas Schwab
2015-03-18  3:09     ` Zhang Zhen
2015-03-19 15:15       ` Jan Kratochvil

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86k2yfg9yi.fsf@gmail.com \
    --to=qiyaoltc@gmail.com \
    --cc=gdb@sourceware.org \
    --cc=xuhanbing@huawei.com \
    --cc=zhenzhang.zhang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox