Skip to contentSkip to conversation

v4l-utils: fix formats under alpha/ppc64/mips64

PATCH In review Rosen Penev 2 participants 7 messages Jul 2, 2024 – Jul 19, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 v4l-utils: fix formats under alpha/ppc64/mips64

Unlike libc, kernel headers use long long for 64-bit types. __SANE_USERSPACE_TYPES__ needs to be defined for the exceptions.

Fixes -Wformat warnings about llu being used instead of lu.

---
 meson.build | 6 ++++++
 1 file changed, 6 insertions(+)
meson.build +6 -0
index 900377fa..15b16971 100644
@@ -69,6 +69,12 @@ elif host_machine.system() == 'freebsd'
     v4l2_utils_incdir_arr += 'contrib/freebsd/include'
 endif
 
+# These platforms default to __[us]64 as long. Add this define to get long long
+# as is done elsewhere.
+if host_machine.cpu_family() in ['alpha', 'mips64', 'ppc64']
+  add_project_arguments('-D__SANE_USERSPACE_TYPES__', language: ['c', 'cpp'])
+endif
+
 v4l2_utils_incdir = include_directories(v4l2_utils_incdir_arr)
 
 prog_bash = find_program('bash')
Signature · 1 line
2.45.2
#1 lore raw

PATCH 2/4 v4l-utils: fix potential crashing with 32-bit musl

Under musl, if a format string has an integer followed by %s, a mismatch between types can cause the second half of the integer to be interpreted by %s.

Eg: printf("%d %s", 64bittype, string);

will crash, especially on 32-bit big endian.

The reason these are cast to __u64 is because time_t and suseconds_t are 64-bit under musl, even on 32-bit platforms. __u64 helps avoid any truncation issues that may or may not arise.

---
 utils/cec-follower/cec-follower.cpp | 2 +-
 utils/libv4l2util/v4l2_driver.c     | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
utils/cec-follower/cec-follower.cpp +1 -1
index a7481aea..67e0d92b 100644
@@ -354,7 +354,7 @@ void print_timers(struct node *node)
 			printf("source: %s, ", source.c_str());
 			if (t.recording_seq)
 				printf("rec-seq: 0x%x, ", t.recording_seq);
-			printf("needs: %ld %s\n", t.duration, "MB."); /* 1MB per second. */
+			printf("needs: %llu %s\n", (__u64)t.duration, "MB."); /* 1MB per second. */
 		}
 		printf("Total media space available for recording: ");
 		if (node->state.media_space_available >= 0)
utils/libv4l2util/v4l2_driver.c +3 -3
index 6b6366fa..51e97b61 100644
@@ -174,13 +174,13 @@ static void prt_buf_info(char *name,struct v4l2_buffer *p)
 {
 	struct v4l2_timecode *tc=&p->timecode;
 
-	printf ("%s: %02ld:%02d:%02d.%08ld index=%d, type=%s, "
+	printf ("%s: %02llu:%02d:%02d.%08llu index=%d, type=%s, "
 		"bytesused=%d, flags=0x%08x, "
 		"field=%s, sequence=%d, memory=%s, offset=0x%08x, length=%d\n",
-		name, (p->timestamp.tv_sec/3600),
+		name, (__u64)(p->timestamp.tv_sec/3600),
 		(int)(p->timestamp.tv_sec/60)%60,
 		(int)(p->timestamp.tv_sec%60),
-		p->timestamp.tv_usec,
+		(__u64)p->timestamp.tv_usec,
 		p->index,
 		prt_names(p->type,v4l2_type_names),
 		p->bytesused,p->flags,
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH 2/4] v4l-utils: fix potential crashing with 32-bit musl

Hi Rosen,

Quoted 23 lines · Rosen Penev

Under musl, if a format string has an integer followed by %s, a mismatch between types can cause the second half of the integer to be interpreted by %s.

Eg: printf("%d %s", 64bittype, string);

will crash, especially on 32-bit big endian.

The reason these are cast to __u64 is because time_t and suseconds_t are 64-bit under musl, even on 32-bit platforms. __u64 helps avoid any truncation issues that may or may not arise.

---
 utils/cec-follower/cec-follower.cpp | 2 +-
 utils/libv4l2util/v4l2_driver.c     | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
utils/cec-follower/cec-follower.cpp +0 -0
index a7481aea..67e0d92b 100644

utils/cec-follower/cec-follower.cpp void print_timers(struct node *node) · line 360

Quoted 8 lines
utils/cec-follower/cec-follower.cpp +1 -1
@@ -354,7 +354,7 @@ void print_timers(struct node *node)
 			printf("source: %s, ", source.c_str());
 			if (t.recording_seq)
 				printf("rec-seq: 0x%x, ", t.recording_seq);
-			printf("needs: %ld %s\n", t.duration, "MB."); /* 1MB per second. */
+			printf("needs: %llu %s\n", (__u64)t.duration, "MB."); /* 1MB per second. */
 		}
 		printf("Total media space available for recording: ");
 		if (node->state.media_space_available >= 0)

I will drop this change, but I will keep the next change.

I'll explain more in my review of 4/4.

Regards,

Hans

Quoted 21 lines
utils/libv4l2util/v4l2_driver.c +3 -3
index 6b6366fa..51e97b61 100644
@@ -174,13 +174,13 @@ static void prt_buf_info(char *name,struct v4l2_buffer *p)
 {
 	struct v4l2_timecode *tc=&p->timecode;
 
-	printf ("%s: %02ld:%02d:%02d.%08ld index=%d, type=%s, "
+	printf ("%s: %02llu:%02d:%02d.%08llu index=%d, type=%s, "
 		"bytesused=%d, flags=0x%08x, "
 		"field=%s, sequence=%d, memory=%s, offset=0x%08x, length=%d\n",
-		name, (p->timestamp.tv_sec/3600),
+		name, (__u64)(p->timestamp.tv_sec/3600),
 		(int)(p->timestamp.tv_sec/60)%60,
 		(int)(p->timestamp.tv_sec%60),
-		p->timestamp.tv_usec,
+		(__u64)p->timestamp.tv_usec,
 		p->index,
 		prt_names(p->type,v4l2_type_names),
 		p->bytesused,p->flags,
#3 lore raw

Re: [PATCH 2/4] v4l-utils: fix potential crashing with 32-bit musl

Quoted 37 lines · Hans Verkuil

Hi Rosen,

Quoted 23 lines · Rosen Penev

Under musl, if a format string has an integer followed by %s, a mismatch between types can cause the second half of the integer to be interpreted by %s.

Eg: printf("%d %s", 64bittype, string);

will crash, especially on 32-bit big endian.

The reason these are cast to __u64 is because time_t and suseconds_t are 64-bit under musl, even on 32-bit platforms. __u64 helps avoid any truncation issues that may or may not arise.

---
 utils/cec-follower/cec-follower.cpp | 2 +-
 utils/libv4l2util/v4l2_driver.c     | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
utils/cec-follower/cec-follower.cpp +0 -0
index a7481aea..67e0d92b 100644

utils/cec-follower/cec-follower.cpp void print_timers(struct node *node) · line 360

Quoted 8 lines
utils/cec-follower/cec-follower.cpp +1 -1
@@ -354,7 +354,7 @@ void print_timers(struct node *node)
 			printf("source: %s, ", source.c_str());
 			if (t.recording_seq)
 				printf("rec-seq: 0x%x, ", t.recording_seq);
-			printf("needs: %ld %s\n", t.duration, "MB."); /* 1MB per second. */
+			printf("needs: %llu %s\n", (__u64)t.duration, "MB."); /* 1MB per second. */
 		}
 		printf("Total media space available for recording: ");
 		if (node->state.media_space_available >= 0)

I will drop this change, but I will keep the next change.

Quoted 1 line

I'll explain more in my review of 4/4.

Actually, I decided to do this change differently by changing the duration type to int instead of using time_t. There is no reason for using time_t for this, since the duration is limited to 100*3600 seconds anyway. So that solves this particular issue without the need for a cast.

I decided to accept 4/4 after all. I am a bit afraid that the same problem will reoccur if new printf statements are added showing timestamps, since this casting is quite fragile. But I don't really see a better approach.

Regards,

Hans

Quoted 28 lines

Regards,

Hans

Quoted 21 lines
utils/libv4l2util/v4l2_driver.c +3 -3
index 6b6366fa..51e97b61 100644
@@ -174,13 +174,13 @@ static void prt_buf_info(char *name,struct v4l2_buffer *p)
 {
 	struct v4l2_timecode *tc=&p->timecode;
 
-	printf ("%s: %02ld:%02d:%02d.%08ld index=%d, type=%s, "
+	printf ("%s: %02llu:%02d:%02d.%08llu index=%d, type=%s, "
 		"bytesused=%d, flags=0x%08x, "
 		"field=%s, sequence=%d, memory=%s, offset=0x%08x, length=%d\n",
-		name, (p->timestamp.tv_sec/3600),
+		name, (__u64)(p->timestamp.tv_sec/3600),
 		(int)(p->timestamp.tv_sec/60)%60,
 		(int)(p->timestamp.tv_sec%60),
-		p->timestamp.tv_usec,
+		(__u64)p->timestamp.tv_usec,
 		p->index,
 		prt_names(p->type,v4l2_type_names),
 		p->bytesused,p->flags,
#4 lore raw

PATCH 3/4 v4l-utils: use 64-bit off_t format

In reply to Rosen Penev

musl uses 64-bit off_t by default on all platforms. Cast and fix format to work everywhere.

---
 utils/v4l2-tracer/retrace.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utils/v4l2-tracer/retrace.cpp +1 -1
index 60d64d8b..010936c0 100644
@@ -72,7 +72,7 @@ void retrace_mmap(json_object *mmap_obj, bool is_mmap64)
 	                           (long) buf_address_retrace_pointer);
 
 	if (is_verbose() || (errno != 0)) {
-		fprintf(stderr, "fd: %d, offset: %ld, ", fd_retrace, off);
+		fprintf(stderr, "fd: %d, offset: %lld, ", fd_retrace, (long long)off);
 		if (is_mmap64)
 			perror("mmap64");
 		else
Signature · 1 line
2.45.2
#5 lore raw

PATCH 4/4 v4l-utils: use 64-bit formats for time

In reply to Rosen Penev

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.

---
 utils/cec-compliance/cec-compliance.cpp   |  2 +-
 utils/cec-compliance/cec-test-adapter.cpp |  4 ++--
 utils/cec-ctl/cec-ctl.cpp                 | 18 +++++++++---------
 utils/cec-follower/cec-processing.cpp     |  2 +-
 4 files changed, 13 insertions(+), 13 deletions(-)
utils/cec-compliance/cec-compliance.cpp +1 -1
index 8075e1d6..bddd57cb 100644
@@ -279,7 +279,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, "%03llu", (__u64)res.tv_usec / 1000);
 	return s + "." + buf;
 }
 
utils/cec-compliance/cec-test-adapter.cpp +2 -2
index 08c856af..f96baaf3 100644
@@ -1276,9 +1276,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 %llu seconds\n",
 			       pending_quick_msgs, pending_msgs - pending_quick_msgs,
-			       time(nullptr) - start);
+			       (__u64)time(nullptr) - start);
 	}
 	print_sfts(sft[1][1], "SFTs for repeating messages (>= 7)");
 	print_sfts(sft[1][0], "SFTs for repeating remote messages (>= 7)");
utils/cec-ctl/cec-ctl.cpp +9 -9
index 8848a49d..f7ba7409 100644
@@ -414,7 +414,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.%06llu", secs / 60, secs % 60, (__u64)res.tv_usec);
 	return buf;
 }
 
@@ -942,10 +942,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 %llu.%09llu\n",
+			(__u64)start_monotonic.tv_sec, (__u64)start_monotonic.tv_nsec);
+		fprintf(fstore, "# start_timeofday %llu.%06llu\n",
+			(__u64)start_timeofday.tv_sec, (__u64)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 +984,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 %llu.%09llu\n",
+				(__u64)start_monotonic.tv_sec, (__u64)start_monotonic.tv_nsec);
+			fprintf(fstore, "# start_timeofday %llu.%06llu\n",
+				(__u64)start_timeofday.tv_sec, (__u64)start_timeofday.tv_usec);
 			fflush(fstore);
 			start_minute = now;
 		}
utils/cec-follower/cec-processing.cpp +1 -1
index 14ee211b..cd20df59 100644
@@ -72,7 +72,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, "%03llu", (__u64)res.tv_usec / 1000);
 	return s + "." + buf;
 }
 
Signature · 1 line
2.45.2
#6 lore raw

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

Quoted 6 lines · Rosen Penev

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.

ping

Quoted 95 lines
---
 utils/cec-compliance/cec-compliance.cpp   |  2 +-
 utils/cec-compliance/cec-test-adapter.cpp |  4 ++--
 utils/cec-ctl/cec-ctl.cpp                 | 18 +++++++++---------
 utils/cec-follower/cec-processing.cpp     |  2 +-
 4 files changed, 13 insertions(+), 13 deletions(-)
utils/cec-compliance/cec-compliance.cpp +1 -1
index 8075e1d6..bddd57cb 100644
@@ -279,7 +279,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, "%03llu", (__u64)res.tv_usec / 1000);
        return s + "." + buf;
 }
 
utils/cec-compliance/cec-test-adapter.cpp +2 -2
index 08c856af..f96baaf3 100644
@@ -1276,9 +1276,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 %llu seconds\n",
                               pending_quick_msgs, pending_msgs - pending_quick_msgs,
-                              time(nullptr) - start);
+                              (__u64)time(nullptr) - start);
        }
        print_sfts(sft[1][1], "SFTs for repeating messages (>= 7)");
        print_sfts(sft[1][0], "SFTs for repeating remote messages (>= 7)");
utils/cec-ctl/cec-ctl.cpp +9 -9
index 8848a49d..f7ba7409 100644
@@ -414,7 +414,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.%06llu", secs / 60, secs % 60, (__u64)res.tv_usec);
        return buf;
 }
 
@@ -942,10 +942,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 %llu.%09llu\n",
+                       (__u64)start_monotonic.tv_sec, (__u64)start_monotonic.tv_nsec);
+               fprintf(fstore, "# start_timeofday %llu.%06llu\n",
+                       (__u64)start_timeofday.tv_sec, (__u64)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 +984,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 %llu.%09llu\n",
+                               (__u64)start_monotonic.tv_sec, (__u64)start_monotonic.tv_nsec);
+                       fprintf(fstore, "# start_timeofday %llu.%06llu\n",
+                               (__u64)start_timeofday.tv_sec, (__u64)start_timeofday.tv_usec);
                        fflush(fstore);
                        start_minute = now;
                }
utils/cec-follower/cec-processing.cpp +1 -2
index 14ee211b..cd20df59 100644
@@ -72,7 +72,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, "%03llu", (__u64)res.tv_usec / 1000);
        return s + "." + buf;
 }
 
--

2.45.2