1
0
Fork 0
CANopenNode/301
94xhn 0b1154407e Fix shift-by-64 UB for mappedLength==0 in PDO bitwise mapping
In 301/CO_PDO.c, when CO_CONFIG_PDO_BITWISE_MAPPING is enabled, a
mapped object with mappedLength == 0 bits is a legal mapping (see the
CO_PDO_t.OD_IO documentation: "mappedLengthBits can be less or equal
to the OD_IO.dataLength*8"), but nothing rejects it and two spots
compute a shift amount as "64 - mappedLength", which becomes a
shift-by-64 on a uint64_t. Shifting a 64-bit value by 64 is undefined
behaviour in C and is not portable across compilers/optimization
levels (verified: an -O0 build silently no-ops the shift so the
intended mask/skip has no effect, while an -O2 build of the same
source produces a different result for the same input).

- CO_RPDO_process(): shiftedData was computed unconditionally as
  "buf64 & (UINT64_MAX >> (64 - mappedLength))"; now short-circuits to
  0 when mappedLength == 0, matching the documented semantics that a
  0-bit mapping should leave the target OD variable untouched.
- CO_TPDOsend(): the same masking pattern, plus a second UB spot
  where the result is placed with "buf <<= (verifyLength -
  mappedLength)", which is also a shift-by-64 once a preceding mapped
  entry already fills the full PDO width. Both operations are now
  skipped when mappedLength == 0, so a 0-bit-mapped OD variable never
  contributes any of its raw bytes to the outgoing TPDO buffer.

Verified against current master (9b8beed): compiled 301/CO_PDO.c and
linked the example/ project standalone with
CO_CONFIG_PDO_BITWISE_MAPPING forced on, with no new warnings.
Independently re-derived the shift logic in an isolated harness and
confirmed the fixed code matches a portable reference mask for every
legal mappedLength (0-64) with zero mismatches, and that both the RPDO
"leftover payload leaks into a 0-bit mapped variable" scenario and the
TPDO "0-bit mapped variable leaks its raw bytes onto the bus" scenario
are eliminated at both -O0 and -O2.
2026-07-13 18:36:33 +08:00
..
CO_config.h Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_driver.h Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_Emergency.c Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_Emergency.h Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_fifo.c Format comments in the .c files. 2024-07-06 21:48:21 +02:00
CO_fifo.h Format comments in the .h files. 2024-07-09 00:17:15 +02:00
CO_HBconsumer.c Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_HBconsumer.h Format comments in the .h files. 2024-07-09 00:17:15 +02:00
CO_NMT_Heartbeat.c Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_NMT_Heartbeat.h Format comments in the .h files. 2024-07-09 00:17:15 +02:00
CO_Node_Guarding.c Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_Node_Guarding.h Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_ODinterface.c Add ability to override OD_readOriginal and OD_writeOriginal #634 2026-07-03 14:22:33 +02:00
CO_ODinterface.h Make OD_requestTPDO() more clear, remove OD_getFlagsPDO(). 2024-10-19 12:42:36 +02:00
CO_PDO.c Fix shift-by-64 UB for mappedLength==0 in PDO bitwise mapping 2026-07-13 18:36:33 +08:00
CO_PDO.h Better organize CO_EM_RPDO_TIME_OUT detection. #618, #626 2026-07-10 16:17:46 +02:00
CO_SDOclient.c Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_SDOclient.h Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_SDOserver.c Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_SDOserver.h Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_SYNC.c Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_SYNC.h Fix and update doxygen documentation. 2024-08-07 00:17:39 +02:00
CO_TIME.c Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
CO_TIME.h Fix CANopen terminology: “CAN messages” are actually “CAN frames” 2026-06-26 10:23:01 +02:00
crc16-ccitt.c Format the .c files using clang-format v15. 2024-07-06 19:57:06 +02:00
crc16-ccitt.h Format comments in the .h files. 2024-07-09 00:17:15 +02:00