Opened 15 years ago

Closed 14 years ago

#1531 closed Bugs (invalid)

segfault in dynamic_bitset::reference assign operator

Reported by: ben.collins@… Owned by: jsiek
Milestone: Boost 1.36.0 Component: dynamic_bitset
Version: Boost Development Trunk Severity: Problem
Keywords: dynamic_bitset Cc: gennaro.prota@…

Description

I will of course take a look at this later (i.e., I've go to stop here and do the work I get paid to do), but for now, this will serve as a heads up to any one who's interested. Perhaps I'm not initializing bset correctly, but I couldn't easily tell that from the headers or the online documentation.

test code:

$ cat bitset.cpp
#include <iostream>
#include <string>
#include <boost/dynamic_bitset.hpp>

int main()
{
  boost::dynamic_bitset<> bset;

  bset[0] = true;
  bset[0] = false;

  return 0;
}

compile + gdb session:

$ g++ -I ~/local/include/boost-1_35/ -o bitset bitset.cpp
$ ./bitset
Segmentation fault
$ g++ -g -I ~/local/include/boost-1_35/ -o bitset bitset.cpp
$ gdb bitset
GNU gdb Red Hat Linux (6.5-15.fc6rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

(gdb) r
Starting program: /users/bcollins/test/C++/bitset

Program received signal SIGSEGV, Segmentation fault.
0x08048877 in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::do_set (this=0xbf856240)
    at /users/bcollins/local/include/boost-1_35/boost/dynamic_bitset/dynamic_bitset.hpp:115
115             void do_set() { m_block |= m_mask; }
(gdb) bt
#0  0x08048877 in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::do_set (this=0xbf856240)
    at /users/bcollins/local/include/boost-1_35/boost/dynamic_bitset/dynamic_bitset.hpp:115
#1  0x080488a3 in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::do_assign (this=0xbf856240, x=true)
    at /users/bcollins/local/include/boost-1_35/boost/dynamic_bitset/dynamic_bitset.hpp:118
#2  0x080488d1 in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::operator= (this=0xbf856240, x=true)
    at /users/bcollins/local/include/boost-1_35/boost/dynamic_bitset/dynamic_bitset.hpp:103
#3  0x080486c9 in main () at bitset.cpp:9
(gdb) f 0
#0  0x08048877 in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::do_set (this=0xbf856240)
    at /users/bcollins/local/include/boost-1_35/boost/dynamic_bitset/dynamic_bitset.hpp:115
115             void do_set() { m_block |= m_mask; }
(gdb) p m_block
$1 = (long unsigned int &) @0x0: Cannot access memory at address 0x0
(gdb)

Change History (2)

comment:1 by smckay@…, 15 years ago

A new dynamic_bitset with no size is empty. You're trying to assign to an empty vector.

I think it would work if you initialized bset with a size of 1.

comment:2 by Gennaro Prota, 14 years ago

Cc: gennaro.prota@… added
Resolution: invalid
Status: newclosed

This isn't a bug: as noted in the previous comment, the size of bset is zero, and evaluating bset[0] violates the precondition n < this->size(), which holds for both the operator[]() members, and thus invokes undefined behavior.

Note: See TracTickets for help on using tickets.