Uploaded image for project: 'C++ Standard Library'
  1. C++ Standard Library
  2. STDCXX-1062

std::vector<bool>::at() throws the wrong exception

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 4.2.1, 4.2.x, 4.3.x
    • 4.2.2, 4.2.x, 4.3.x, 5.0.0
    • 23. Containers
    • Solaris 10 and 11
      RedHat Linux, OpenSuSE Linux

      Defect is independent of platform and/or compiler

    • Patch Available
    • Incorrect Behavior

    Description

      The std::vector<bool>::at() specialization throws the wrong exception:

      test.cc
      #include <iostream>
      #include <vector>
      #include <stdexcept>
      
      static const size_t maxlen = 10;
      
      int main()
      {
          size_t i;
          bool b;
          int r;
          int ret = 0;
          int x = 0;
      
          std::vector<int> vi(maxlen, 1);
          std::vector<bool> vb(maxlen, false);
      
          try
          {
              r = vi.at (12);
          }
          catch (std::out_of_range& e)
          {
              std::cerr << "std::vector<int>::at(12): OK" << std::endl;
              ++x;
          }
          catch (std::exception& e)
          {
              std::cerr << "std::vector<int>::at(12): wrong exception: "
                      << e.what() << std::endl;
              ++ret;
              ++x;
          }
          catch ( ... )
          {
              std::cerr << "std::vector<int::>at(12): wrong exception!"
                      << std::endl;
              ++ret;
              ++x;
          }
      
          try
          {
              b = vb.at(12);
          }
          catch (std::out_of_range& e)
          {
              std::cerr << "std::vector<bool>::at(12): OK" << std::endl;
              ++x;
          }
          catch (std::exception& e)
          {
              std::cerr << "std::vector<bool>::at(12): wrong exception: "
                      << e.what() << std::endl;
              ++ret;
              ++x;
          }
          catch ( ... )
          {
              std::cerr << "std::vector<boolt>::at(12): wrong exception!"
                          << std::endl;
              ++ret;
              ++x;
          }
      
          if (x == 0)
          {
              std::cerr << "no exception was thrown" << std::endl;
              ++ret;
          }
      
          return ret;
      }
      

      1. Output from GCC 4.5.0 on OpenSuSE Linux 11.3:

      [steleman@darthvader][/src/steleman/programming/stdcxx-ss122/bugfixes-sunw/vector.bool][02/16/2012 15:37:04][1028]>> ./test-gcc
      std::vector<int>::at(12): OK
      std::vector<bool>::at(12): OK
      [steleman@darthvader][/src/steleman/programming/stdcxx-ss122/bugfixes-sunw/vector.bool][02/16/2012 15:37:16][1029]>> echo $status
      0
      

      2. Output from SunPro C++ 12.2 with libCstd (default):

      [steleman@darthvader][/src/steleman/programming/stdcxx-ss122/bugfixes-sunw/vector.bool][02/16/2012 15:37:25][1032]>> ./test-cstd
      std::vector<int>::at(12): OK
      std::vector<bool>::at(12): OK
      [steleman@darthvader][/src/steleman/programming/stdcxx-ss122/bugfixes-sunw/vector.bool][02/16/2012 15:37:29][1033]>> echo $status
      0
      

      3. Output from Intel C++ 12.10:

      [steleman@darthvader][/src/steleman/programming/stdcxx-ss122/bugfixes-sunw/vector.bool][02/16/2012 15:39:33][1053]>> ./test-icc 
      std::vector<int>::at(12): OK
      std::vector<bool>::at(12): OK
      [steleman@darthvader][/src/steleman/programming/stdcxx-ss122/bugfixes-sunw/vector.bool][02/16/2012 15:39:36][1054]>> echo $status
      0
      

      4. Output from SunPro C++ 12.2 with our patched stdcxx:

      [steleman@darthvader][/src/steleman/programming/stdcxx-ss122/bugfixes-sunw/vector.bool][02/16/2012 15:37:31][1034]>> ./test-stdcxx
      std::vector<int>::at(12): OK
      std::vector<bool>::at(12): OK
      [steleman@darthvader][/src/steleman/programming/stdcxx-ss122/bugfixes-sunw/vector.bool][02/16/2012 15:37:35][1035]>> echo $status
      0
      

      5. Output from Pathscale 4.0.12.1 (which didn't patch stdcxx):

      [steleman@darthvader][/src/steleman/programming/stdcxx-ss122/bugfixes-sunw/vector.bool][02/16/2012 15:37:37][1036]>> ./test-pathscale
      std::vector<int>::at(12): OK
      std::vector<bool>::at(12): wrong exception: /opt/pathscale/ekopath-4.0.12.1/include/4.0.12.1/stl/vector:1236: std::vector<bool, _Allocator>::reference std::vector<bool, _Allocator>::at(typename _Allocator::size_type) [with _Allocator = std::allocator<bool>]: length error: size 12 out of range [0, 10)
      [steleman@darthvader][/src/steleman/programming/stdcxx-ss122/bugfixes-sunw/vector.bool][02/16/2012 15:37:42][1037]>> echo $status
      1
      

      Attachments

        1. stdcxx-1062.patch
          1 kB
          Stefan Teleman

        Activity

          People

            farid Farid Zaripov
            steleman Stefan Teleman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: