Bug 59116 - std::nth_element segfaults by invalidating one of the vector elements
Summary: std::nth_element segfaults by invalidating one of the vector elements
Status: RESOLVED DUPLICATE of bug 58800
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.8.1
: P3 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2013-11-13 15:46 UTC by chandraprakashblr
Modified: 2013-11-13 15:59 UTC (History)
0 users

See Also:
Known to work:
Known to fail:
Last reconfirmed:

sample program (291 bytes, text/x-c++src)
2013-11-13 15:48 UTC, chandraprakashblr

Note You need to log in before you can comment on or make changes to this bug.
Description chandraprakashblr 2013-11-13 15:46:46 UTC
std::nth_element segfaults by invalidating one of the vector elements.

Note : Replacing nth_element by partial_sort works fine.

Trying nth_element less than 6 also doesn't result in segfault.

Sample program : 

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
int main()
  for (size_t i(0); i < 1000; ++i) {
    std::vector<int> vec;
    uint32_t nth_element = 7;

    for (size_t i(0); i < 10; ++i) {
      std::cout << " n = " << vec.back() << std::endl;
    assert((vec.size() >= nth_element));
    std::nth_element(vec.begin(), vec.begin() + nth_element, vec.end());

// commandline:  g++ nth_element.cc -std=c++11  -Wall -Wextra 

// stack trace

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401fdf in __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > std::__unguarded_partition<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int>(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&) ()

// system info

OS Ubuntu 13.10 (64 bit)

gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.8.1-10ubuntu8' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu8)
Comment 1 chandraprakashblr 2013-11-13 15:48:32 UTC
Created attachment 31210 [details]
sample program
Comment 2 Marc Glisse 2013-11-13 15:59:39 UTC
Already fixed.

*** This bug has been marked as a duplicate of bug 58800 ***