Interesting void* bug

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

I'm trying to use the libRocket library for my projects but I stumbled
upon a bug I can't figure out why it is happening.

The problem is that it seems like a gcc problem. I extracted the code,
and tried to simplify it so that others are able to easily test by
themselves.

The output of this code for me is the following:

$ ./main
Okay, the data_block->data_ptr has the address: 0xd37014
The address of this new StringBase is: 0xd37014
The string_id address is: 0xd3701400000000
Now, after string_id=0 the data_block->data_ptr has the address: 0

What is bugging me is that string_id (void*) is a member of StringBase
and has nothing to do with data_block->data_ptr (void*), a member of
Variant, and still, it starts out with the same address data_ptr is
holding. Then I set string_id to 0, and by doing this,
data_block->data_ptr is set to 0 too.

It is much easier to understand this by running the code with gdb, my
session goes something like this (the important part):

// In Variant NewDataBlock method
data_block = new DataBlock();
  data_block->data_ptr = data; //data is a char[16], a buffer
new (data_block->data_ptr) String(); // String is a StringBase<char>

// In StringBase<char> constructor
string_id = 0
// This changes the value of data_block->data_ptr (!??)

If, for instance, I would set data_block->data_ptr = data, this would
change the value of string_id to data too, inside the instance of class
String (!?) (string_id is even protected)

Anyway, the code is very easy to run and test, and the bug is very
interesting. You can clone it from github:

git clone git://github.com/hiltonm/cpptest.git

Then run ./make.sh to build. It will create a libvariant.so and an
executable 'main'. Nothing needs to be installed.

Please, test it and let me know the output. I tested this with gcc
4.5 (and 4.3):

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.5.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr
--enable-languages=c,c++,fortran,objc,obj-c++,ada --enable-shared
--enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu
--enable-gnu-unique-object --enable-lto --enable-plugin
--disable-multilib --disable-libstdcxx-pch --with-system-zlib
--with-ppl --with-cloog --libdir=/usr/lib --libexecdir=/usr/lib
--mandir=/usr/share/man --infodir=/usr/share/info
Thread model: posix
gcc version 4.5.1 (GCC)

$ uname -a
Linux hilton.laptop 2.6.35-ARCH #1 SMP PREEMPT Wed Sep 29 08:45:18 CEST
2010 x86_64 AMD Athlon(tm) 64 X2 Dual-Core Processor TK-57 AuthenticAMD
GNU/Linux

Any help would be really appreciated. Also, note that I removed lots of
code and left only the necessary to test the bug, but there is some
leftovers.

Cheers,
Hilton


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux