Bug 1964

Summary: GetInteger overflow
Product: ns-3 Reporter: Tom Henderson <tomh>
Component: coreAssignee: Tom Henderson <tomh>
Status: RESOLVED FIXED    
Severity: normal CC: ns-bugs, pdbarnes
Priority: P5    
Version: pre-release   
Hardware: PC   
OS: Linux   

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:

number->GetInteger(SessionId::MIN_SESSION_NUMBER,SessionId::MAX_SESSION_NUMBER)

Where:

   *  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 ).

random-variable-stream.cc

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) );

Explicitly:

  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