On 2026-10-03 17:12, Christos Zoulas wrote:
I kept the original size_t buflen (because snprintf takes a size_t argument),
and then just cast the LHS of the if statement to (size_t)

Although that would work and would pacify the compiler, the cast is tricky and 
things would be nearly as tricky if it were a conversion instead of a cast 
(because casts are too powerful). The business about -1 wrapping around to 
SIZE_MAX is not always immediately obvious: I can still remember my surprise 
when I discovered that the C standard does not guarantee that the C expression 
(SIZE_MAX == -1) is true.

It's OK to use size_t for the buffer size, though, so I installed the attached 
additional patch. This makes the code closer to what was in 2026d, while still 
pacifying the compiler and avoiding a cast.
From 83f40cf091e90137edf09a23367dd040d156c97a Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sat, 3 Oct 2026 19:21:10 -0700
Subject: [PROPOSED] Revert to size_t for asctime_r buffer size

* asctime.c (asctime_r): Go back to using size_t for buffer size.
Problem reported by Christos Zoulas in:
https://lists.iana.org/hyperkitty/list/[email protected]/thread/S37SYED3CNWCHWUOO6USZGCMYXIHF4FL/
---
 asctime.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/asctime.c b/asctime.c
index 86b8e256..f9ca75d0 100644
--- a/asctime.c
+++ b/asctime.c
@@ -77,8 +77,8 @@ asctime_r(struct tm const *restrict timeptr, char *restrict buf)
 	int year, mday, hour, min, sec;
 	long long_TM_YEAR_BASE = TM_YEAR_BASE;
 	int buflen;
-	int bufsize = (buf == buf_asctime
-		       ? sizeof buf_asctime : STD_ASCTIME_BUF_SIZE);
+	size_t bufsize = (buf == buf_asctime
+			  ? sizeof buf_asctime : STD_ASCTIME_BUF_SIZE);
 
 	if (timeptr == NULL) {
 		strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
-- 
2.53.0

Reply via email to