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

Bug#185243: libstdc++3-dev: rope segfaults under heavy load



Good point, though changing the function to return rval in the other 
case seems to change the result not at all. I suspect that (due to luck) 
rval is on the stack in any case.

Here is the modified code, with some additional checks:

#include <ext/rope>
#include <iostream>
#include <assert.h>

using namespace std;
using namespace __gnu_cxx;

static unsigned int rand(unsigned int max) {
  unsigned int rval = (unsigned int)(((double)max)*rand()/(RAND_MAX));
  if (rval == max)
    return max-1;
  return rval;
}

int main() {
  crope r;
  char buf[10240];
  
  while (1) {
    if (rand(2) && r.size() < 1024*1024) {
      unsigned int s = rand(10240);
      assert(s <= 10240);
      r.append(buf, rand(10240));
    } else if (r.size() > 10240) {
      unsigned int s = rand(10240);
      assert(s <= r.size());
      r.erase(0, rand(10240));
    }
  }
  return 0;
}

On Wed, Mar 19, 2003 at 12:52:07AM +0000, Philip Martin wrote:
> > unsigned int rand(unsigned int max) {
> >   unsigned int rval = (unsigned int)(((double)max)*rand()/(RAND_MAX));
> >   if (rval == max)
> >     return max-1;
> > }
> 
> This function returns no value if rval != max so the behaviour is
> undefined.
> 
> -- 
> Philip Martin



Reply to: