Changing parsePacket() to allow zero-length packets; impact assessment

shawn

Well-known member
I'm updating QNEthernet's handling of `EthernetUDP::parsePacket()` to return -1 if there's no packet available instead of zero. This allows for zero-length packets. However, I've seen examples that do things like this:

Code:
int packetSize = udp.parsePacket();
if (packetSize) {  // <-- This is not correct
  // ...do something...
}

Negative values are implicitly converted to a Boolean value of true. This means that the change might break code, even though the spec specifically says that `parsePacket()` is supposed to return the packet size. (See: https://www.arduino.cc/reference/en/libraries/ethernet/ethernetudp.parsepacket/) To me this means that zero is an acceptable return value for zero-length packets, and -1 is an acceptable return value for "no packet available".

I'm wondering how much code is out there that uses the `if (packetSize)` form?
 
Back
Top