On 2026-07-04 10:38, Tom Lane wrote:
I'm not really following your aversion to a cast.  Surely none
of these constants will ever come close to exceeding INT_MAX.

Although a cast would be safe in this case, verifying that it's safe requires too much thinking. I want a knowledgeable reader to be able to look at just that line of code and trivially say "yeah, that's benign" while muttering a few choice words about clueless compilers.

How about replacing "+" with "~~"? That is, something like the first attached draft patch. It pacifies gcc -Wenum-compare. Does it also pacify MSVC? If so, I can install it.


the unsigned warning isn't only
on old MSVC versions.  I see it also on the latest that we
have in the Postgres buildfarm, 19.50.35726

Ah, OK, I misunderstood. Does the second attached draft patch pacify MSVC? If so, I can install it too.


I took a quick look at the postgres copy of zic.c and have a couple of thoughts.

First, most of the changes are indenting or comment reformatting and whatnot, which makes it hard to see what's going on. Do you know offhand whether there are any functional changes that tzcode should buy back? If so, are those changes public domain?

Second, I don't envy you the job of merging tzcode's zic.c changes into postgres. If there are no functional changes, how about the idea of postgres instead compiling a file pgzic.c that looks something like this:

   #include "postgres_fe.h"
   #include "pg_getopt.h"
   /* Any other Postgres-specific stuff goes here.  */
   #include "zic.c" /* A byte-for-byte copy of tzcode zic.c.  */

... and using that for the Postgres zic? I could make some changes to tzcode zic.c to make this happen, much as I already did (and want to continue to do) for FreeBSD.
From 23fbada0c1481d7e135a7885920c6ac5a548a2fa Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sat, 4 Jul 2026 11:02:15 -0700
Subject: [DRAFT 1/2] Pacify MSVC when comparing enums

Problem reported by Tom Lane for MSVC 19.50.35726 in:
https://lists.iana.org/hyperkitty/list/[email protected]/thread/PJBVERYHQZEXIREYIHC5OJGZMNVQC2SD/
* zic.c (MAX_FIELDS): Pacify with ~~ instead of with +.
---
 zic.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/zic.c b/zic.c
index af2bb80a..e049303d 100644
--- a/zic.c
+++ b/zic.c
@@ -412,10 +412,10 @@ enum {
 };
 
 /* The maximum number of fields on any of the above lines.
-   (The "+"s pacify gcc -Wenum-compare.)  */
+   (The "~~"s pacify gcc -Wenum-compare and MSVC C5287.)  */
 enum {
-  MAX_FIELDS = max(max(+RULE_FIELDS, +LINK_FIELDS),
-		   max(+LEAP_FIELDS, +EXPIRES_FIELDS))
+  MAX_FIELDS = max(max(~~RULE_FIELDS, ~~LINK_FIELDS),
+		   max(~~LEAP_FIELDS, ~~EXPIRES_FIELDS))
 };
 
 /*
-- 
2.53.0

From 948e846cccca1572a8b5a5e469574508e2d8973e Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sat, 4 Jul 2026 11:20:04 -0700
Subject: [DRAFT 2/2] Pacify MSVC when negating unsigned

Problem reported by Tom Lane for MSVC 19.50.35726 in:
https://lists.iana.org/hyperkitty/list/[email protected]/thread/PJBVERYHQZEXIREYIHC5OJGZMNVQC2SD/
* zic.c (random_dirent): Pacify with 0-X instead of -X.
---
 zic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/zic.c b/zic.c
index e049303d..31b72d2a 100644
--- a/zic.c
+++ b/zic.c
@@ -1571,8 +1571,8 @@ random_dirent(char const **name, char **namealloc)
      uintmax_t value that is this value or greater, yields a biased
      remainder when divided by BASE**6.  UNFAIR_MIN equals the
      mathematical value of ((UINTMAX_MAX + 1) - (UINTMAX_MAX + 1) % BASE**6)
-     computed without overflow.  */
-  uint_fast64_t unfair_min = - ((UINTMAX_MAX % base__6 + 1) % base__6);
+     computed without overflow.  The 0 pacifies MSVC C4146.  */
+  uint_fast64_t unfair_min = 0 - ((UINTMAX_MAX % base__6 + 1) % base__6);
 
   if (!dst) {
     char *cp = dst = xmalloc(size_sum(dirlen, prefixlen + suffixlen + 1));
-- 
2.53.0

Reply via email to