nRF24 / RF24Mesh

OSI Layer 7 Mesh Networking for RF24Network & nrf24L01+ & nrf52x devices
http://nrf24.github.io/RF24Mesh
GNU General Public License v2.0
422 stars 154 forks source link

Split increment and assignment in order to avoid UB #154

Closed igormp closed 5 years ago

igormp commented 5 years ago

According to the C11 standard, the previous code could generate an undefined behavior, and would also generate a warning if compiled using clang:

make
clang++ -Wall -fPIC  -c RF24Mesh.cpp
RF24Mesh.cpp:256:6: warning: unsequenced modification and access to 'reqCounter' [-Wunsequenced]
    (++reqCounter) = reqCounter%4;
     ^               ~~~~~~~~~~
RF24Mesh.cpp:257:6: warning: unsequenced modification and access to 'totalReqs' [-Wunsequenced]
    (++totalReqs) = totalReqs%10;
     ^              ~~~~~~~~~
2 warnings generated.

Splitting the increment and assign into multiple lines solves that, whilst improving readability and eliminating the warnings thrown by clang (which means that the code now compiles with no warnings at all under it!).