This is the mail archive of the gdb-prs@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/10694] New: Passing "const std::string" by value to a function shows corrupted object


I've stumbled accross the following weird behaviour of gdb:

When passing a "const std::string" object to a function (or member function)
then when stepping into that function in gdb the object seems to be corrupted.
However the program runs perfectly fine. Still the supposed corruption is very
confusing during debugging sessions.

I've broken this down to the following example code:

------------------------------
#include <iostream>
#include <stdlib.h>
#include <string>

void func(const std::string s)
{
	std::cout << s << "\n";
}

int main()
{
	func("hello");

	return EXIT_SUCCESS;
}
----------------------------------

Compilation and debugging run is as follows:

-----------------------------
~ $ g++ gdbtest.cxx -g

~ $ gdb ./a.out 
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...
(gdb) b main
Breakpoint 1 at 0x804889e: file gdbtest.cxx, line 12.
(gdb) r
Starting program: ~/a.out 

Breakpoint 1, main () at gdbtest.cxx:12
12              func("hello");
(gdb) s
func (s=
        {static npos = 4294967295, _M_dataplus = {<std::allocator<char>> =
{<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p
= 0xbf92a4b4
"\024&#65533;&#65533;t&#548;\222&#65533;&#65533;222&#65533;&#65533;217&#1783;\200&#65533;&#65533;&#65533;\222&#65533;\f\226&#631;\200&#65533;&#65533;\211\004\b8&#65533;\222&#65533;\f\226&#631;\001"}}) at
gdbtest.cxx:7
7               std::cout << s << "\n";
(gdb) n
hello
8       }
(gdb) 
-----------------------

As can be seen the string shown upon entry in func() is corrupted. However
printing the string object onto stdout works perfectly fine as does everythign
else (i.e. the program works correctly).

I run in this behaviour on two different platforms. The above example was
performed on a Linux system compiling with

gcc --version
gcc (Gentoo 4.3.2-r3 p1.6, pie-10.1.5) 4.3.2

and debugging with

gdb --version
GNU gdb 6.8

The same occured on a QNX system with gcc version

gcc --version
gcc (GCC) 4.3.3

and debugger version

gdb --version
GNU gdb 6.8 qnx-nto (rev. 347)

Strangely I could not reproduce the effect using an instance of my own simple
class definition holding a char*. However the same error occured to me in a
bigger project using a string object that is not std::string but a call similar
to it.

In that case I could see upon entry to functions that when stepping into the
string object's constructor, the object still looks fine. But only upon actual
entry to the function the object was totally messed up. Actually the address of
the object was not the same as the previously seen this pointer in the
constructor. The address was shifted by quite an amount bytes.

So possibly this is an error with interpreting the function's stack frame in
that case or something.

I couldn't find any bug reports regarding this matter yet so I found this might
be the right place to turn to.

Best regards,

Matthias

-- 
           Summary: Passing "const std::string" by value to a function shows
                    corrupted object
           Product: gdb
           Version: 6.8
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at sourceware dot org
        ReportedBy: Matthias dot Gerstner at elektrobit dot com
                CC: gdb-prs at sourceware dot org


http://sourceware.org/bugzilla/show_bug.cgi?id=10694

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]