Undefined behaviour when setting sFlowCpInterval to zero

From: andy kitchingman <andy.kitchingman@alliedtelesis.co.nz>
Date: 02/11/10
Message-Id: <4B7542840200005D000177DA@gwia.alliedtelesyn.co.nz>

Hi

In the poller function:

void
sfl_poller_set_sFlowCpInterval (SFLPoller * poller, u_int32_t sFlowCpInterval)
{
  poller->sFlowCpInterval = sFlowCpInterval;
  /* Set the countersCountdown to be a randomly selected value between 1 and
     sFlowCpInterval. That way the counter polling would be desynchronised
     (on a 200-port switch, polling all the counters in one second could be
harmful). */
  poller->countersCountdown = 1 + (random () % sFlowCpInterval); <--- MAY NOT
DO WHAT YOU THINK IF sFlowCpInterval == 0!!
}

The subexpression "random () % sFlowCpInterval" results in undefined behaviour
according to the C and C++ standards, if sFlowCpInterval is zero.

Depending on what platform you compile this on, you get different results.

For example, on the i686, I got a floating point error at runtime.

On a PowerPC, the expression evaluated to 1 + random().

Zero is a valid value for sFlowCpInterval according to the sFlow specification
V5, so shouldn't poller->countersCountdown be set to zero in this case?

Regards

Andy

NOTICE: This message contains privileged and confidential
information intended only for the use of the addressee
named above. If you are not the intended recipient of
this message you are hereby notified that you must not
disseminate, copy or take any action in reliance on it.
If you have received this message in error please
notify Allied Telesis Labs Ltd immediately.
Any views expressed in this message are those of the
individual sender, except where the sender has the
authority to issue and specifically states them to
be the views of Allied Telesis Labs.
Received on Thu Feb 11 15:00:04 2010

This archive was generated by hypermail 2.1.8 : 02/17/10 PST