From 6bdb574707f7690d5ffe698f84f8b7cad47f623f Mon Sep 17 00:00:00 2001 From: Oliver Hartkopp Date: Tue, 13 Oct 2026 18:40:59 +0000 Subject: [PATCH] asc2log: use unsigned types for DLC and data length values The DLC and data length values were read into signed integers with %x and %d. Negative values (e.g. '-1' or 'ffffffff') passed the range checks and led to len values of 255 or 65535. Via the union cfu these classic CAN frames were then printed as CAN XL frames. Use unsigned int for dlc/dlen and %u for the data length so that negative values are caught by the existing range checks. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Oliver Hartkopp --- asc2log.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/asc2log.c b/asc2log.c index 40a1c13..2da2e38 100644 --- a/asc2log.c +++ b/asc2log.c @@ -192,7 +192,7 @@ static void eval_can(char* buf, struct timeval *date_tvp, char timestamps, struct canfd_frame cf = { 0 }; struct can_frame *ccf = (struct can_frame *)&cf; /* for len8_dlc */ char rtr; - int dlc = 0; + unsigned int dlc = 0; int len = 0; int data[8]; char idstr[21]; @@ -300,7 +300,7 @@ static void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps, struct canfd_frame cf = { 0 }; unsigned char brs, esi; unsigned int flags; - int dlc, dlen = 0; + unsigned int dlc, dlen = 0; char idstr[21]; char dir[5]; /* 'Rx'/'Tx'/'TxRq' plus terminating zero */ char *ptr; @@ -319,12 +319,12 @@ static void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps, */ /* check for valid line without symbolic name */ - if (sscanf(buf, "%llu.%llu %*s %d %4s %20s %hhx %hhx %x %d %n", + if (sscanf(buf, "%llu.%llu %*s %d %4s %20s %hhx %hhx %x %u %n", &sec, &usec, &interface, dir, idstr, &brs, &esi, &dlc, &dlen, &n) != 9) { /* check for valid line with a symbolic name */ - if (sscanf(buf, "%llu.%llu %*s %d %4s %20s %*s %hhx %hhx %x %d %n", + if (sscanf(buf, "%llu.%llu %*s %d %4s %20s %*s %hhx %hhx %x %u %n", &sec, &usec, &interface, dir, idstr, &brs, &esi, &dlc, &dlen, &n) != 9) { @@ -412,7 +412,7 @@ static void eval_canxl_cc(char* buf, struct timeval *date_tvp, char timestamps, static struct timeval read_tv; /* frame timestamp from ASC file */ struct can_frame cf = { 0 }; unsigned int flags; - int dlc, dlen = 0; + unsigned int dlc, dlen = 0; char idstr[21]; char dir[5]; /* 'Rx'/'Tx'/'TxRq' plus terminating zero */ char *ptr; @@ -430,7 +430,7 @@ static void eval_canxl_cc(char* buf, struct timeval *date_tvp, char timestamps, if (sscanf(buf, "%llu.%llu %*s %d %4s " /* time, CANXL, channel, direction */ "%*s %*s %*s %20s " /* frame format, msg dur, bit count, ID */ - "%x %d %n", /* DLC, Datalen */ + "%x %u %n", /* DLC, Datalen */ &sec, &usec, &interface, dir, idstr, &dlc, &dlen, &n) != 7) { @@ -438,7 +438,7 @@ static void eval_canxl_cc(char* buf, struct timeval *date_tvp, char timestamps, if (sscanf(buf, "%llu.%llu %*s %d %4s " /* time, CANXL, channel, direction */ "%*s %*s %*s %20s " /* frame format, msg dur, bit count, ID */ - "%*s %x %d %n", /* sym name, DLC, Datalen */ + "%*s %x %u %n", /* sym name, DLC, Datalen */ &sec, &usec, &interface, dir, idstr, &dlc, &dlen, &n) != 7) { @@ -509,7 +509,7 @@ static void eval_canxl_fd(char* buf, struct timeval *date_tvp, char timestamps, static struct timeval read_tv; /* frame timestamp from ASC file */ struct canfd_frame cf = { 0 }; unsigned int flags; - int dlc, dlen = 0; + unsigned int dlc, dlen = 0; char idstr[21]; char dir[5]; /* 'Rx'/'Tx'/'TxRq' plus terminating zero */ char *ptr; @@ -527,7 +527,7 @@ static void eval_canxl_fd(char* buf, struct timeval *date_tvp, char timestamps, if (sscanf(buf, "%llu.%llu %*s %d %4s " /* time, CANXL, channel, direction */ "%*s %*s %*s %20s " /* frame format, msg dur, bit count, ID */ - "%x %d %n", /* DLC, Datalen */ + "%x %u %n", /* DLC, Datalen */ &sec, &usec, &interface, dir, idstr, &dlc, &dlen, &n) != 7) { @@ -535,7 +535,7 @@ static void eval_canxl_fd(char* buf, struct timeval *date_tvp, char timestamps, if (sscanf(buf, "%llu.%llu %*s %d %4s " /* time, CANXL, channel, direction */ "%*s %*s %*s %20s " /* frame format, msg dur, bit count, ID */ - "%*s %x %d %n", /* sym name, DLC, Datalen */ + "%*s %x %u %n", /* sym name, DLC, Datalen */ &sec, &usec, &interface, dir, idstr, &dlc, &dlen, &n) != 7) { @@ -603,7 +603,7 @@ static void eval_canxl_xl(char* buf, struct timeval *date_tvp, char timestamps, struct canxl_frame cf = { 0 }; unsigned char sdt, vcid, secbit; unsigned int af, flags; - int dlc, dlen = 0; + unsigned int dlc, dlen = 0; char idstr[21]; char dir[5]; /* 'Rx'/'Tx'/'TxRq' plus terminating zero */ char *ptr; @@ -622,7 +622,7 @@ static void eval_canxl_xl(char* buf, struct timeval *date_tvp, char timestamps, if (sscanf(buf, "%llu.%llu %*s %d %4s " /* time, CANXL, channel, direction */ "%*s %*s %*s %20s " /* frame format, msg dur, bit count, ID */ - "%hhx %hhx %x %d " /* SDT, SEC, DLC, Datalen */ + "%hhx %hhx %x %u " /* SDT, SEC, DLC, Datalen */ "%*s %*s %hhx %x %n", /* stuff bit count, crc, VCID, AF */ &sec, &usec, &interface, dir, idstr, @@ -632,7 +632,7 @@ static void eval_canxl_xl(char* buf, struct timeval *date_tvp, char timestamps, if (sscanf(buf, "%llu.%llu %*s %d %4s " /* time, CANXL, channel, direction */ "%*s %*s %*s %20s " /* frame format, msg dur, bit count, ID */ - "%*s %hhx %hhx %x %d " /* sym name, SDT, SEC, DLC, Datalen */ + "%*s %hhx %hhx %x %u " /* sym name, SDT, SEC, DLC, Datalen */ "%*s %*s %hhx %x %n", /* stuff bit count, crc, VCID, AF */ &sec, &usec, &interface, dir, idstr,