Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in libstdc++ regarding std::list assignment?

Tags:

c++

list

std

gcc

c++11

I've recently came across a very interesting inconsistency while using libxml++ (a C++ wrapper for libxml2).

The library returns node lists using default STL list container (std::list<xmlpp::Node*>). Since it is installed from default repositories, it seems to be built in C++03 mode (but I'm working with C++11).

The caveat here is that C++11 changed the way std::list::size() works.
In C++03 it was O(n), calling std::distance(begin(), end()) each time - and now it returns the pre-computed value.

Here is the code:

  /**  Returns the number of elements in the %list.  */
  size_type
  size() const _GLIBCXX_NOEXCEPT
  {
#ifdef __GXX_EXPERIMENTAL_CXX0X__
    return this->_M_impl._M_size;
#else
    return std::distance(begin(), end());
#endif
  }

Things begin to happen when I receive such a list from the library and call size() on it. There I read values like 140734320138496, which clearly indicate an uninitialized counter: in original list there was no counter altogether.
Manually calling std::distance (list.begin(), list.end()) does work, of course.

The question is - can this be considered a bug in GCC/libstdc++ or I should never link executables built in different GCC modes?

like image 406
intelfx Avatar asked Dec 12 '25 15:12

intelfx


1 Answers

I think that's a violation of the One Definition Rule. Your code is compiled with a different definition of std::list than what your library is using. (This is undefined behavior, not a GCC bug.)

You should recompile your library (or recompile your code).

like image 149
Mat Avatar answered Dec 15 '25 09:12

Mat