diff mbox series

[3/4] v4l-utils: use 64-bit formats for time

Message ID 20240630224440.5912-3-rosenp@gmail.com
State New
Headers show
Series [1/4] v4l-utils: fix potential crashing with 32-bit musl | expand

Commit Message

Rosen Penev June 30, 2024, 10:44 p.m. UTC
musl since version 1.2.0 uses 64-bit time_t and suseconds_t on all
platforms, even 32-bit. Cast to avoid warnings and potentially future
proof for when glibc and others gain support.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 utils/cec-compliance/cec-compliance.cpp   |  3 ++-
 utils/cec-compliance/cec-test-adapter.cpp |  5 +++--
 utils/cec-ctl/cec-ctl.cpp                 | 19 ++++++++++---------
 utils/cec-follower/cec-processing.cpp     |  3 ++-
 utils/keytable/keytable.c                 |  1 +
 5 files changed, 18 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/utils/cec-compliance/cec-compliance.cpp b/utils/cec-compliance/cec-compliance.cpp
index 8075e1d6..df633a33 100644
--- a/utils/cec-compliance/cec-compliance.cpp
+++ b/utils/cec-compliance/cec-compliance.cpp
@@ -3,6 +3,7 @@ 
  * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  */
 
+#include <cinttypes>
 #include <sstream>
 
 #include <fcntl.h>
@@ -279,7 +280,7 @@  static std::string ts2s(__u64 ts)
 	t = res.tv_sec;
 	s = ctime(&t);
 	s = s.substr(0, s.length() - 6);
-	sprintf(buf, "%03lu", res.tv_usec / 1000);
+	sprintf(buf, "%03" PRIu64, (uint64_t)res.tv_usec / 1000);
 	return s + "." + buf;
 }
 
diff --git a/utils/cec-compliance/cec-test-adapter.cpp b/utils/cec-compliance/cec-test-adapter.cpp
index 08c856af..7a80d17b 100644
--- a/utils/cec-compliance/cec-test-adapter.cpp
+++ b/utils/cec-compliance/cec-test-adapter.cpp
@@ -4,6 +4,7 @@ 
  */
 
 #include <cerrno>
+#include <cinttypes>
 #include <ctime>
 #include <string>
 
@@ -1276,9 +1277,9 @@  static int testLostMsgs(struct node *node)
 			printf("\t\tReceived messages: %d of which %d were CEC_MSG_CEC_VERSION\n",
 			       pending_rx_msgs, pending_rx_cec_version_msgs);
 		if (pending_quick_msgs < pending_msgs)
-			printf("\t\tReceived %d messages immediately, and %d over %ld seconds\n",
+			printf("\t\tReceived %d messages immediately, and %d over %" PRIu64 " seconds\n",
 			       pending_quick_msgs, pending_msgs - pending_quick_msgs,
-			       time(nullptr) - start);
+			       (uint64_t)time(nullptr) - start);
 	}
 	print_sfts(sft[1][1], "SFTs for repeating messages (>= 7)");
 	print_sfts(sft[1][0], "SFTs for repeating remote messages (>= 7)");
diff --git a/utils/cec-ctl/cec-ctl.cpp b/utils/cec-ctl/cec-ctl.cpp
index 8848a49d..fb38320d 100644
--- a/utils/cec-ctl/cec-ctl.cpp
+++ b/utils/cec-ctl/cec-ctl.cpp
@@ -6,6 +6,7 @@ 
 #include <algorithm>
 #include <cctype>
 #include <cerrno>
+#include <cinttypes>
 #include <cstring>
 #include <ctime>
 #include <map>
@@ -414,7 +415,7 @@  std::string ts2s(__u64 ts)
 		strftime(buf, sizeof(buf), "%a %b %e %T.000000", &tm);
 	}
 	secs = last_secs + t - last_t;
-	sprintf(buf + 14, "%02u:%02u.%06lu", secs / 60, secs % 60, res.tv_usec);
+	sprintf(buf + 14, "%02u:%02u.%06d", secs / 60, secs % 60, (int)res.tv_usec);
 	return buf;
 }
 
@@ -942,10 +943,10 @@  static void monitor(const struct node &node, __u32 monitor_time, const char *sto
 		}
 		fprintf(fstore, "# cec-ctl --store-pin\n");
 		fprintf(fstore, "# version %d\n", CEC_CTL_VERSION);
-		fprintf(fstore, "# start_monotonic %lu.%09lu\n",
-			start_monotonic.tv_sec, start_monotonic.tv_nsec);
-		fprintf(fstore, "# start_timeofday %lu.%06lu\n",
-			start_timeofday.tv_sec, start_timeofday.tv_usec);
+		fprintf(fstore, "# start_monotonic %" PRIu64 ".%09" PRIu64 "\n",
+			(uint64_t)start_monotonic.tv_sec, (uint64_t)start_monotonic.tv_nsec);
+		fprintf(fstore, "# start_timeofday %" PRIu64 ".%06" PRIu64 "\n",
+			(uint64_t)start_timeofday.tv_sec, (uint64_t)start_timeofday.tv_usec);
 		fprintf(fstore, "# log_addr_mask 0x%04x\n", node.log_addr_mask);
 		fprintf(fstore, "# phys_addr %x.%x.%x.%x\n",
 			cec_phys_addr_exp(node.phys_addr));
@@ -984,10 +985,10 @@  static void monitor(const struct node &node, __u32 monitor_time, const char *sto
 			 */
 			clock_gettime(CLOCK_MONOTONIC, &start_monotonic);
 			gettimeofday(&start_timeofday, nullptr);
-			fprintf(fstore, "# start_monotonic %lu.%09lu\n",
-				start_monotonic.tv_sec, start_monotonic.tv_nsec);
-			fprintf(fstore, "# start_timeofday %lu.%06lu\n",
-				start_timeofday.tv_sec, start_timeofday.tv_usec);
+			fprintf(fstore, "# start_monotonic %" PRIu64 ".%09" PRIu64 "\n",
+				(uint64_t)start_monotonic.tv_sec, (uint64_t)start_monotonic.tv_nsec);
+			fprintf(fstore, "# start_timeofday %" PRIu64 ".%06" PRIu64 "\n",
+				(uint64_t)start_timeofday.tv_sec, (uint64_t)start_timeofday.tv_usec);
 			fflush(fstore);
 			start_minute = now;
 		}
diff --git a/utils/cec-follower/cec-processing.cpp b/utils/cec-follower/cec-processing.cpp
index 14ee211b..20c6165c 100644
--- a/utils/cec-follower/cec-processing.cpp
+++ b/utils/cec-follower/cec-processing.cpp
@@ -4,6 +4,7 @@ 
  */
 
 #include <cerrno>
+#include <cinttypes>
 #include <ctime>
 #include <string>
 
@@ -72,7 +73,7 @@  static std::string ts2s(__u64 ts, bool wallclock)
 	t = res.tv_sec;
 	s = ctime(&t);
 	s = s.substr(0, s.length() - 6);
-	sprintf(buf, "%03lu", res.tv_usec / 1000);
+	sprintf(buf, "%03" PRIu64, (uint64_t)res.tv_usec / 1000);
 	return s + "." + buf;
 }
 
diff --git a/utils/keytable/keytable.c b/utils/keytable/keytable.c
index b6474d5c..538f4ef3 100644
--- a/utils/keytable/keytable.c
+++ b/utils/keytable/keytable.c
@@ -212,6 +212,7 @@  static enum sysfs_protocols parse_sysfs_protocol(const char *name, bool all_allo
 	return SYSFS_INVALID;
 }
 
+__attribute__((format(printf, 3, 0)))
 static void write_sysfs_protocols(enum sysfs_protocols protocols, FILE *fp, const char *fmt)
 {
 	const struct protocol_map_entry *pme;