for loop variable fixes

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: for loop variable fixes
Date: 2026-06-23 10:29:14
Message-ID: d639aede-209f-412b-927a-d38d4848b370@eisentraut.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I've been working toward cleaning up the C integer type use from various
angles (signed/unsigned mismatches, int/size_t misuse, etc.). More
about that to come. Here is a small subset to get started.

I have noticed that many for loops have loop variables whose type
doesn't match the upper-bound condition. Meaning, if you have

for (i = 0; i < foo; i++)

then the type of i should ordinarily be the same as the type of foo. At
least if you declare i specifically for that purpose, you might as well
make it match. However, as you can see in the attached patches, lots of
randomness abounds.

As far as I can tell, none of of these are current bugs, but this is the
kind of thing that sets bad precedents and could lead to truncated
values or unintended overflows etc. in the future.

I have located these sites with gcc -Wsign-compare, so that means that
there could be other issues of this nature that are not caught by that
option because the signedness does match but maybe the underlying types
do not. I have started to investigate these too (e.g. gcc
-Wconversion), but this patch is already sizable enough, so I'm going
with this for now.

Another thing to note is that -Wsign-compare is included in gcc -Wall in
C++. So with the increasing interest in C++ for extensions, this kind
of hygiene would also be beneficial.

(But just for the avoidance of doubt: This does not claim to fix all
-Wsign-compare warnings.)

I split this patch into two mainly because the second patch is entirely
repetitive, but they both address the same topic.

Attachment Content-Type Size
0001-Fix-for-loop-variables.patch text/plain 85.2 KB
0002-Fix-for-loop-variables-used-with-lengthof.patch text/plain 26.5 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2026-06-23 10:39:23 Re: doc: should pg_createsubscriber be grouped as a client application?
Previous Message vignesh C 2026-06-23 10:19:10 Re: Include sequences in publications created by pg_createsubscriber