From 750270e29f3c1c8e28ce70ee7dfd0aed581abbf7 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sun, 12 Mar 2023 12:45:35 -0700
Subject: [PROPOSED 2/3] One limit, not two, on tz abbr in localtime.c
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* localtime.c (TZ_ABBR_MAX_LEN): Remove.
All uses replaced by MY_TZNAME_MAX, so that there’s
just one limit.
(scrub_abbrs): Compare to GRANDPARENTED only if the length is
plausible.  In the usual case where MY_TZNAME_MAX is 255 and
GRANDPARENTED's length is less, the compiler can optimize
away the memcmp entirely.
---
 localtime.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/localtime.c b/localtime.c
index 3135ea33..1de6b55b 100644
--- a/localtime.c
+++ b/localtime.c
@@ -28,10 +28,6 @@ static int lock(void) { return 0; }
 static void unlock(void) { }
 #endif
 
-#ifndef TZ_ABBR_MAX_LEN
-# define TZ_ABBR_MAX_LEN 16
-#endif /* !defined TZ_ABBR_MAX_LEN */
-
 #ifndef TZ_ABBR_CHAR_SET
 # define TZ_ABBR_CHAR_SET \
 	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
@@ -361,10 +357,12 @@ scrub_abbrs(struct state *sp)
 	for (i = 0; i < sp->typecnt; ++i) {
 		register const struct ttinfo * const	ttisp = &sp->ttis[i];
 		char *cp = &sp->chars[ttisp->tt_desigidx];
+		size_t cplen = strlen(cp);
+		static char const gp[sizeof GRANDPARENTED - 1] = GRANDPARENTED;
 
-		if (strlen(cp) > TZ_ABBR_MAX_LEN &&
-			strcmp(cp, GRANDPARENTED) != 0)
-				*(cp + TZ_ABBR_MAX_LEN) = '\0';
+		if (MY_TZNAME_MAX < cplen
+		    && ! (cplen == sizeof gp && memcmp(cp, gp, sizeof gp) == 0))
+				*(cp + MY_TZNAME_MAX) = '\0';
 	}
 }
 
-- 
2.37.2

