I'm reading through a USB Wi-Fi card's C driver code and have come across a part I'm not sure I fully understand. I suspect it's my understanding of the C language and operator precedence that's wrong and that the driver code is fine, but I wanted to check.
In /drivers/net/wireless/rtl818x/rtl8187/dev.c
is some code that reads a bunch of values into a 14 element channels
array. The relevant code from dev.c
is as follows:
channel = priv->channels;
for (i = 0; i < 3; i++) {
eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_1 + i,
&txpwr);
(*channel++).hw_value = txpwr & 0xFF;
(*channel++).hw_value = txpwr >> 8;
}
for (i = 0; i < 2; i++) {
eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_4 + i,
&txpwr);
(*channel++).hw_value = txpwr & 0xFF;
(*channel++).hw_value = txpwr >> 8;
}
....
if (!priv->is_rtl8187b) {
for (i = 0; i < 2; i++) {
eeprom_93cx6_read(&eeprom,
RTL8187_EEPROM_TXPWR_CHAN_6 + i,
&txpwr);
(*channel++).hw_value = txpwr & 0xFF;
(*channel++).hw_value = txpwr >> 8;
}
} else {
eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_6,
&txpwr);
(*channel++).hw_value = txpwr & 0xFF;
eeprom_93cx6_read(&eeprom, 0x0A, &txpwr);
(*channel++).hw_value = txpwr & 0xFF;
eeprom_93cx6_read(&eeprom, 0x1C, &txpwr);
(*channel++).hw_value = txpwr & 0xFF;
(*channel++).hw_value = txpwr >> 8;
}
My concern with this code is that I would have thought the very first call to (*channel++).hw_value = ...
would have incremented the channel pointer before dereferencing it, thereby starting at element [1]
of channels and missing element [0]
. Also, regardless of which of the if/else branches get executed, I count 14 calls to (*channel++)...
, so I would have thought the final call to (*channel++)
would actually be pointing at (non-existent) channel[15]
and overwriting the memory of whatever variable happens to follow channels
in the stack. Can anyone point out where I might have gone wrong in my interpretation?