From a4905ed7c82b7858741f068744547d73b6d13fd0 Mon Sep 17 00:00:00 2001 From: Oliver Hartkopp Date: Tue, 18 Aug 2020 12:51:35 +0200 Subject: [PATCH] fix timeval to ASCII unsigned value conversion As reported by Oleksij Rempel here https://github.com/linux-can/can-utils/issues/233#issuecomment-674818935 the representation of timeval timestamps are signed values which leads to negative values on 32 bit machines addressing the year 2038 unix sec counter overflow. Fix the issue on 32 bit systems by converting the timeval values to unsigned ASCII value representations. Fixes: https://github.com/linux-can/can-utils/issues/234 Reported-by: Oleksij Rempel Signed-off-by: Oliver Hartkopp --- asc2log.c | 18 +++++++++--------- candump.c | 10 +++++----- canlogserver.c | 4 ++-- canplayer.c | 4 ++-- isotpdump.c | 6 +++--- isotpperf.c | 2 +- isotpsniffer.c | 6 +++--- log2asc.c | 6 +++--- 8 files changed, 28 insertions(+), 28 deletions(-) diff --git a/asc2log.c b/asc2log.c index de619c0..f53218b 100644 --- a/asc2log.c +++ b/asc2log.c @@ -72,7 +72,7 @@ void print_usage(char *prg) void prframe(FILE *file, struct timeval *tv, int dev, struct canfd_frame *cf, unsigned int max_dlen) { - fprintf(file, "(%ld.%06ld) ", tv->tv_sec, tv->tv_usec); + fprintf(file, "(%lu.%06lu) ", tv->tv_sec, tv->tv_usec); if (dev > 0) fprintf(file, "can%d ", dev-1); @@ -143,7 +143,7 @@ void eval_can(char* buf, struct timeval *date_tvp, char timestamps, char base, i if (base == 'h') { /* check for CAN frames with hexadecimal values */ - items = sscanf(buf, "%ld.%ld %d %s %*s %c %d %x %x %x %x %x %x %x %x", + items = sscanf(buf, "%lu.%lu %d %s %*s %c %d %x %x %x %x %x %x %x %x", &read_tv.tv_sec, &read_tv.tv_usec, &interface, tmp1, &rtr, &dlc, &data[0], &data[1], &data[2], &data[3], @@ -158,7 +158,7 @@ void eval_can(char* buf, struct timeval *date_tvp, char timestamps, char base, i } else { /* check for CAN frames with decimal values */ - items = sscanf(buf, "%ld.%ld %d %s %*s %c %d %d %d %d %d %d %d %d %d", + items = sscanf(buf, "%lu.%lu %d %s %*s %c %d %d %d %d %d %d %d %d %d", &read_tv.tv_sec, &read_tv.tv_usec, &interface, tmp1, &rtr, &dlc, &data[0], &data[1], &data[2], &data[3], @@ -191,7 +191,7 @@ void eval_can(char* buf, struct timeval *date_tvp, char timestamps, char base, i } /* check for ErrorFrames */ - if (sscanf(buf, "%ld.%ld %d %s", + if (sscanf(buf, "%lu.%lu %d %s", &read_tv.tv_sec, &read_tv.tv_usec, &interface, tmp1) == 4) { @@ -232,12 +232,12 @@ void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps, int dplace memset(&cf, 0, sizeof(cf)); /* check for valid line without symbolic name */ - if (sscanf(buf, "%ld.%ld %*s %d %*s %s %hhx %hhx %x %d ", + if (sscanf(buf, "%lu.%lu %*s %d %*s %s %hhx %hhx %x %d ", &read_tv.tv_sec, &read_tv.tv_usec, &interface, tmp1, &brs, &esi, &dlc, &dlen) != 8) { /* check for valid line with a symbolic name */ - if (sscanf(buf, "%ld.%ld %*s %d %*s %s %*s %hhx %hhx %x %d ", + if (sscanf(buf, "%lu.%lu %*s %d %*s %s %*s %hhx %hhx %x %d ", &read_tv.tv_sec, &read_tv.tv_usec, &interface, tmp1, &brs, &esi, &dlc, &dlen) != 8) { @@ -460,12 +460,12 @@ int main(int argc, char **argv) gettimeofday(&date_tv, NULL); } if (verbose) - printf("date %ld => %s", date_tv.tv_sec, ctime(&date_tv.tv_sec)); + printf("date %lu => %s", date_tv.tv_sec, ctime(&date_tv.tv_sec)); continue; } /* check for decimal places length in valid CAN frames */ - if (sscanf(buf, "%ld.%s %s ", &tmp_tv.tv_sec, tmp2, tmp1) == 3){ + if (sscanf(buf, "%lu.%s %s ", &tmp_tv.tv_sec, tmp2, tmp1) == 3){ dplace = strlen(tmp2); if (verbose) printf("decimal place %d, e.g. '%s'\n", dplace, tmp2); @@ -481,7 +481,7 @@ int main(int argc, char **argv) /* so try to get CAN frames and ErrorFrames and convert them */ /* check classic CAN format or the CANFD tag which can take both types */ - if (sscanf(buf, "%ld.%ld %s ", &tmp_tv.tv_sec, &tmp_tv.tv_usec, tmp1) == 3){ + if (sscanf(buf, "%lu.%lu %s ", &tmp_tv.tv_sec, &tmp_tv.tv_usec, tmp1) == 3){ if (!strncmp(tmp1, "CANFD", 5)) eval_canfd(buf, &date_tv, timestamps, dplace, outfile); else diff --git a/candump.c b/candump.c index 846c706..0758261 100644 --- a/candump.c +++ b/candump.c @@ -706,7 +706,7 @@ int main(int argc, char **argv) /* log CAN frame with absolute timestamp & device */ sprint_canframe(buf, &frame, 0, maxdlen); - fprintf(logfile, "(%010ld.%06ld) %*s %s\n", + fprintf(logfile, "(%010lu.%06lu) %*s %s\n", tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx], buf); } @@ -716,7 +716,7 @@ int main(int argc, char **argv) /* print CAN frame in log file style to stdout */ sprint_canframe(buf, &frame, 0, maxdlen); - printf("(%010ld.%06ld) %*s %s\n", + printf("(%010lu.%06lu) %*s %s\n", tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx], buf); goto out_fflush; /* no other output to stdout */ @@ -735,7 +735,7 @@ int main(int argc, char **argv) switch (timestamp) { case 'a': /* absolute with timestamp */ - printf("(%010ld.%06ld) ", tv.tv_sec, tv.tv_usec); + printf("(%010lu.%06lu) ", tv.tv_sec, tv.tv_usec); break; case 'A': /* absolute with date */ @@ -745,7 +745,7 @@ int main(int argc, char **argv) tm = *localtime(&tv.tv_sec); strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm); - printf("(%s.%06ld) ", timestring, tv.tv_usec); + printf("(%s.%06lu) ", timestring, tv.tv_usec); } break; @@ -762,7 +762,7 @@ int main(int argc, char **argv) diff.tv_sec--, diff.tv_usec += 1000000; if (diff.tv_sec < 0) diff.tv_sec = diff.tv_usec = 0; - printf("(%03ld.%06ld) ", diff.tv_sec, diff.tv_usec); + printf("(%03lu.%06lu) ", diff.tv_sec, diff.tv_usec); if (timestamp == 'd') last_tv = tv; /* update for delta calculation */ diff --git a/canlogserver.c b/canlogserver.c index 0f7f255..4bcf991 100644 --- a/canlogserver.c +++ b/canlogserver.c @@ -410,7 +410,7 @@ int main(int argc, char **argv) idx = idx2dindex(addr.can_ifindex, s[i]); - sprintf(temp, "(%ld.%06ld) %*s ", + sprintf(temp, "(%lu.%06lu) %*s ", tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx]); sprint_canframe(temp+strlen(temp), &frame, 0, maxdlen); strcat(temp, "\n"); @@ -422,7 +422,7 @@ int main(int argc, char **argv) #if 0 /* print CAN frame in log file style to stdout */ - printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec); + printf("(%lu.%06lu) ", tv.tv_sec, tv.tv_usec); printf("%*s ", max_devname_len, devname[idx]); fprint_canframe(stdout, &frame, "\n", 0, maxdlen); #endif diff --git a/canplayer.c b/canplayer.c index 1482dc8..9f2f438 100644 --- a/canplayer.c +++ b/canplayer.c @@ -395,7 +395,7 @@ int main(int argc, char **argv) eof = 0; - if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec, + if (sscanf(buf, "(%lu.%lu) %s %s", &log_tv.tv_sec, &log_tv.tv_usec, device, ascframe) != 4) { fprintf(stderr, "incorrect line format in logfile\n"); return 1; @@ -474,7 +474,7 @@ int main(int argc, char **argv) break; } - if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec, + if (sscanf(buf, "(%lu.%lu) %s %s", &log_tv.tv_sec, &log_tv.tv_usec, device, ascframe) != 4) { fprintf(stderr, "incorrect line format in logfile\n"); return 1; diff --git a/isotpdump.c b/isotpdump.c index e9d9620..fa2129b 100644 --- a/isotpdump.c +++ b/isotpdump.c @@ -356,7 +356,7 @@ int main(int argc, char **argv) switch (timestamp) { case 'a': /* absolute with timestamp */ - printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec); + printf("(%lu.%06lu) ", tv.tv_sec, tv.tv_usec); break; case 'A': /* absolute with date */ @@ -366,7 +366,7 @@ int main(int argc, char **argv) tm = *localtime(&tv.tv_sec); strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm); - printf("(%s.%06ld) ", timestring, tv.tv_usec); + printf("(%s.%06lu) ", timestring, tv.tv_usec); } break; @@ -383,7 +383,7 @@ int main(int argc, char **argv) diff.tv_sec--, diff.tv_usec += 1000000; if (diff.tv_sec < 0) diff.tv_sec = diff.tv_usec = 0; - printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec); + printf("(%lu.%06lu) ", diff.tv_sec, diff.tv_usec); if (timestamp == 'd') last_tv = tv; /* update for delta calculation */ diff --git a/isotpperf.c b/isotpperf.c index fe74802..f0ad808 100644 --- a/isotpperf.c +++ b/isotpperf.c @@ -397,7 +397,7 @@ int main(int argc, char **argv) /* check devisor to be not zero */ if (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000){ - printf("%ld.%06lds ", diff_tv.tv_sec, diff_tv.tv_usec); + printf("%lu.%06lus ", diff_tv.tv_sec, diff_tv.tv_usec); printf("=> %lu byte/s", (fflen * 1000) / (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000)); } else diff --git a/isotpsniffer.c b/isotpsniffer.c index bbae2f4..3a022f1 100644 --- a/isotpsniffer.c +++ b/isotpsniffer.c @@ -101,7 +101,7 @@ void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp, switch (timestamp) { case 'a': /* absolute with timestamp */ - printf("(%ld.%06ld) ", tv->tv_sec, tv->tv_usec); + printf("(%lu.%06lu) ", tv->tv_sec, tv->tv_usec); break; case 'A': /* absolute with date */ @@ -111,7 +111,7 @@ void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp, tm = *localtime(&tv->tv_sec); strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm); - printf("(%s.%06ld) ", timestring, tv->tv_usec); + printf("(%s.%06lu) ", timestring, tv->tv_usec); } break; @@ -128,7 +128,7 @@ void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp, diff.tv_sec--, diff.tv_usec += 1000000; if (diff.tv_sec < 0) diff.tv_sec = diff.tv_usec = 0; - printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec); + printf("(%lu.%06lu) ", diff.tv_sec, diff.tv_usec); if (timestamp == 'd') *last_tv = *tv; /* update for delta calculation */ diff --git a/log2asc.c b/log2asc.c index 3ce800b..d02213b 100644 --- a/log2asc.c +++ b/log2asc.c @@ -223,7 +223,7 @@ int main(int argc, char **argv) if (buf[0] != '(') continue; - if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec, + if (sscanf(buf, "(%lu.%lu) %s %s", &tv.tv_sec, &tv.tv_usec, device, ascframe) != 4) { fprintf(stderr, "incorrect line format in logfile\n"); return 1; @@ -262,9 +262,9 @@ int main(int argc, char **argv) tv.tv_sec = tv.tv_usec = 0; if (d4) - fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100); + fprintf(outfile, "%4lu.%04lu ", tv.tv_sec, tv.tv_usec/100); else - fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec); + fprintf(outfile, "%4lu.%06lu ", tv.tv_sec, tv.tv_usec); if ((mtu == CAN_MTU) && (fdfmt == 0)) can_asc(&cf, devno, nortrdlc, outfile);