tests: fix potential divide by zero condition 01/14501/3
authorCheyenne Wills <cwills@sinenomine.net>
Fri, 22 Jan 2021 19:48:21 +0000 (12:48 -0700)
committerBenjamin Kaduk <kaduk@mit.edu>
Sat, 23 Jan 2021 00:36:36 +0000 (19:36 -0500)
Running clang's static analysis revealed a possible divide by zero
condition.

There is a random chance of the divide by zero.

- it has to be in the first pass of the main loop testing events
  (counter = 0)
- 90% chance path :   if (counter < (NUMEVENTS -1) &&
                          random() % 10 == 0)      -- needs to be false
- 25% chance path:    if (random() % 4 == 0)       -- needs to be true

if the above conditions are met, the statement
    int victim = random() % counter
is a divide by zero.

Add a check to ensure the counter is greater than zero.  Add a comment
to document that only events prior to the current event are randomly
selected.

Change-Id: I4b4e73fa324842bb504bcc952079af15aea8a6a3
Reviewed-on: https://gerrit.openafs.org/14501
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Andrew Deason <adeason@sinenomine.net>
Reviewed-by: Michael Meffie <mmeffie@sinenomine.net>
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>

tests/rx/event-t.c

index 008cadb..7c4fdc5 100644 (file)
@@ -166,9 +166,11 @@ main(void)
                 = rxevent_Post(&eventTime, &now, eventSub, &events[counter],
                                NULL, 0);
        }
-
-       /* A 25% chance that we will cancel a random event */
-       if (random() % 4 == 0) {
+       /*
+        * A 25% chance that we will cancel some event.
+        * Randomly pick any event that was scheduled before the current event.
+        */
+       if (counter > 0 && (random() % 4 == 0)) {
            int victim = random() % counter;
 
            if (rxevent_Cancel(&events[victim].event))