Bug 1964 - GetInteger overflow
GetInteger overflow
Product: ns-3
Classification: Unclassified
Component: core
PC Linux
: P5 normal
Assigned To: Tom Henderson
Depends on:
  Show dependency treegraph
Reported: 2014-08-26 13:50 EDT by Tom Henderson
Modified: 2014-09-12 16:06 EDT (History)
2 users (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Tom Henderson 2014-08-26 13:50:21 EDT
Ruben Martinez reported the below to me; I'm opening a bug so I don't forget it.  Below text is from Ruben:

I'm using a  UniformRandomVariable::GetInteger to generate LTP Session Ids, like this:



   *  Session numbers chosen by LTP senders shall be in the range [1, 2^32-1]
  static const uint32_t MIN_SESSION_NUMBER = 1;
  static const uint32_t MAX_SESSION_NUMBER = 4294967295U;

I noticed that when called with those parameters GetInteger always returns a 0.

After checking the source code for RandomVariable, I found that the max parameter is increased by 1 (and thus it becomes 2^32 = 0 ).


183 UniformRandomVariable::GetInteger (uint32_t min, uint32_t max)
184 {
185  NS_LOG_FUNCTION (this << min << max);
186  NS_ASSERT (min <= max);
187  return static_cast<uint32_t> ( GetValue (min, max + 1) );
188 }

I had to substract 1 to MAX_SESSION_NUMBER in the call to GetInteger to get a return value within the range.

Doesn't look like this behaviour was intended.
Comment 1 Peter Barnes 2014-09-09 18:13:04 EDT
Call is to 

  double GetValue (double, double);

So force the arguments to double before the addition:

  return static_cast<uint32_t> ( GetValue (min, max + 1.0) );


  return static_cast<uint32_t> ( GetValue ((double)(min), (double)(max) + 1.0) );
Comment 2 Tom Henderson 2014-09-12 16:06:01 EDT
fixed in 10935:8c52d989750b