Skip to contentSkip to conversation

Bad #define, nonportable C, missing {}

BUG Nathan Myers 1 participant 1 message Nov 27, 2001

lore
Share & tools
lore
#0 lore raw

vda wrote in http://marc.theaimsgroup.com/?l=linux-kernel&m=100687040003540&w=2:

Quoted 11 lines · Alan Cox
Quoted 9 lines
Quoted 6 lines
Quoted 1 line

MODINC(x,y) (x = (x % y) + 1)

drivers/message/i2o/i2o_config.c:#define MODINC(x,y) (x = x++ % y)

Alan, can you clarify what this macro is doing?
What about making it less confusing?

Nothing to do with me 8). I didnt write that bit of the i2o code. I agree its both confusing and buggy. Send a fix ?

Quoted 6 lines

This is a test to be sure my replacement is equivalent:
--------------------
#include <stdio.h>
#define MODINC(x,y) (x = x++ % y)
#define MODULO_INC(x,y) ((x) = ((x)%(y))+1)
...

If they really are equivalent, you have certainly found a bug.

Examining the code in i2o_config.c, it is expected that the q_in argument ranges from 0 to I2O_EVT_Q_LEN-1, but the result of MODINC can never be 0, and may equal I2O_EVT_Q_LEN, overindexing the array member event_q and clobbering all the remaining members (including q_in).

The correct fix appears to be

  #define MODULO_INC(x,y) ((x) = ((x)+1)%(y))

Nathan Myers
ncm at cantrip dot org