]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix for loop variables used with lengthof
authorPeter Eisentraut <peter@eisentraut.org>
Sat, 11 Jul 2026 12:38:33 +0000 (14:38 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Sat, 11 Jul 2026 12:39:16 +0000 (14:39 +0200)
lengthof returns type size_t, but most for loops used int as a loop
variable.  Fix that.  This avoids possible warnings about
signed/unsigned mismatches under higher warning levels.  (The compiler
will likely optimize these loops beyond recognition, so this shouldn't
affect the generated code much.)

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/d639aede-209f-412b-927a-d38d4848b370%40eisentraut.org

33 files changed:
contrib/dblink/dblink.c
src/backend/access/heap/heapam.c
src/backend/access/transam/parallel.c
src/backend/bootstrap/bootstrap.c
src/backend/catalog/objectaddress.c
src/backend/commands/typecmds.c
src/backend/main/main.c
src/backend/postmaster/bgworker.c
src/backend/postmaster/datachecksum_state.c
src/backend/replication/logical/conflict.c
src/backend/utils/adt/amutils.c
src/backend/utils/adt/jsonb.c
src/backend/utils/adt/jsonpath_exec.c
src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c
src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c
src/bin/initdb/initdb.c
src/bin/pg_dump/pg_dump.c
src/bin/pgbench/pgbench.c
src/bin/psql/command.c
src/bin/psql/tab-complete.in.c
src/common/unicode_norm.c
src/interfaces/libpq/fe-auth.c
src/interfaces/libpq/fe-connect.c
src/pl/plpgsql/src/pl_scanner.c
src/port/pg_localeconv_r.c
src/port/win32error.c
src/port/win32ntdll.c
src/test/modules/oauth_validator/validator.c
src/test/modules/test_escape/test_escape.c
src/test/modules/test_integerset/test_integerset.c
src/test/modules/test_radixtree/test_radixtree.c
src/test/regress/regress.c
src/timezone/zic.c

index 3329f9ac0cc393425b844ca9fc060726ab2ae46e..9e42a642419b41695e09224737a30966733dc46e 100644 (file)
@@ -3151,9 +3151,8 @@ applyRemoteGucs(PGconn *conn)
        };
 
        int                     nestlevel = -1;
-       int                     i;
 
-       for (i = 0; i < lengthof(GUCsAffectingIO); i++)
+       for (size_t i = 0; i < lengthof(GUCsAffectingIO); i++)
        {
                const char *gucName = GUCsAffectingIO[i];
                const char *remoteVal = PQparameterStatus(conn, gucName);
index abfd8e8970a608ce16e52f87c9bfa256b900f769..de31a204c9f2a0aa616c501b52902ec8ea3cec50 100644 (file)
@@ -8458,7 +8458,7 @@ index_delete_sort(TM_IndexDeleteOp *delstate)
        StaticAssertDecl(sizeof(TM_IndexDelete) <= 8,
                                         "element size exceeds 8 bytes");
 
-       for (int g = 0; g < lengthof(gaps); g++)
+       for (size_t g = 0; g < lengthof(gaps); g++)
        {
                for (int hi = gaps[g], i = hi; i < ndeltids; i++)
                {
index 89e9d224eec7d20b85df4e1e520e8b6d8eb3d099..c0640e071b99e6923be553ad3d3a01902402eb5e 100644 (file)
@@ -1655,9 +1655,7 @@ LookupParallelWorkerFunction(const char *libraryname, const char *funcname)
         */
        if (strcmp(libraryname, "postgres") == 0)
        {
-               int                     i;
-
-               for (i = 0; i < lengthof(InternalParallelWorkers); i++)
+               for (size_t i = 0; i < lengthof(InternalParallelWorkers); i++)
                {
                        if (strcmp(InternalParallelWorkers[i].fn_name, funcname) == 0)
                                return InternalParallelWorkers[i].fn_addr;
index b0dcd9876c56fd89aabda8bfc02f7854e356e925..a678f345230d3d0e1fe845c9fdf3c7c8b81e1415 100644 (file)
@@ -1093,7 +1093,7 @@ boot_get_type_io_data(Oid typid,
 Oid
 boot_get_role_oid(const char *rolname)
 {
-       for (int i = 0; i < lengthof(RolInfo); i++)
+       for (size_t i = 0; i < lengthof(RolInfo); i++)
        {
                if (strcmp(RolInfo[i].rolname, rolname) == 0)
                        return RolInfo[i].oid;
index 0c305ed6de3edd0459da37e89644f4f4787d6c07..703754a812313530518221558c4719e2045e48e3 100644 (file)
@@ -2711,9 +2711,7 @@ get_object_namespace(const ObjectAddress *address)
 int
 read_objtype_from_string(const char *objtype)
 {
-       int                     i;
-
-       for (i = 0; i < lengthof(ObjectTypeMap); i++)
+       for (size_t i = 0; i < lengthof(ObjectTypeMap); i++)
        {
                if (strcmp(ObjectTypeMap[i].tm_name, objtype) == 0)
                        return ObjectTypeMap[i].tm_type;
@@ -2840,9 +2838,7 @@ get_object_namensp_unique(Oid class_id)
 bool
 is_objectclass_supported(Oid class_id)
 {
-       int                     index;
-
-       for (index = 0; index < lengthof(ObjectProperty); index++)
+       for (size_t index = 0; index < lengthof(ObjectProperty); index++)
        {
                if (ObjectProperty[index].class_oid == class_id)
                        return true;
@@ -2858,7 +2854,6 @@ static const ObjectPropertyType *
 get_object_property_data(Oid class_id)
 {
        static const ObjectPropertyType *prop_last = NULL;
-       int                     index;
 
        /*
         * A shortcut to speed up multiple consecutive lookups of a particular
@@ -2867,7 +2862,7 @@ get_object_property_data(Oid class_id)
        if (prop_last && prop_last->class_oid == class_id)
                return prop_last;
 
-       for (index = 0; index < lengthof(ObjectProperty); index++)
+       for (size_t index = 0; index < lengthof(ObjectProperty); index++)
        {
                if (ObjectProperty[index].class_oid == class_id)
                {
index e9c3215ccecb22798e6b2b27f271116271e74095..871da5af40e7b77f6eb4e36929330a5a75f51214 100644 (file)
@@ -1803,7 +1803,6 @@ makeRangeConstructors(const char *name, Oid namespace,
        Oid                     constructorArgTypes[3];
        ObjectAddress myself,
                                referenced;
-       int                     i;
 
        constructorArgTypes[0] = subtype;
        constructorArgTypes[1] = subtype;
@@ -1813,7 +1812,7 @@ makeRangeConstructors(const char *name, Oid namespace,
        referenced.objectId = rangeOid;
        referenced.objectSubId = 0;
 
-       for (i = 0; i < lengthof(prosrc); i++)
+       for (size_t i = 0; i < lengthof(prosrc); i++)
        {
                oidvector  *constructorArgTypesVector;
 
index 7b9b602f3c4b0b64ecd2b52de89041965b436316..8384b4f545e1618e7d61fe93d9b7270aa5e78eb9 100644 (file)
@@ -243,7 +243,7 @@ main(int argc, char *argv[])
 DispatchOption
 parse_dispatch_option(const char *name)
 {
-       for (int i = 0; i < lengthof(DispatchOptionNames); i++)
+       for (size_t i = 0; i < lengthof(DispatchOptionNames); i++)
        {
                /*
                 * Unlike the other dispatch options, "forkchild" takes an argument,
index 2e4acad4f005c8e790a6363017ea7ccd834d6b4d..f2cffce3ff6d243f736e50143e7f5677680903e9 100644 (file)
@@ -1365,9 +1365,7 @@ LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
         */
        if (strcmp(libraryname, "postgres") == 0)
        {
-               int                     i;
-
-               for (i = 0; i < lengthof(InternalBGWorkers); i++)
+               for (size_t i = 0; i < lengthof(InternalBGWorkers); i++)
                {
                        if (strcmp(InternalBGWorkers[i].fn_name, funcname) == 0)
                                return InternalBGWorkers[i].fn_addr;
index 7f29551202ff96d342e2a86cb65303df635968d3..73dc539836b01eb2b6db0094164e15caa966b327 100644 (file)
@@ -516,7 +516,7 @@ AbsorbDataChecksumsBarrier(ProcSignalBarrierType barrier)
         * a condition would be a grave programmer error as the states are a
         * discrete set.
         */
-       for (int i = 0; i < lengthof(checksum_barriers) && !found; i++)
+       for (size_t i = 0; i < lengthof(checksum_barriers) && !found; i++)
        {
                if (checksum_barriers[i].from == current && checksum_barriers[i].to == target_state)
                        found = true;
@@ -794,7 +794,7 @@ ResetDataChecksumsProgressCounters(void)
 
        int64           vals[lengthof(index)];
 
-       for (int i = 0; i < lengthof(index); i++)
+       for (size_t i = 0; i < lengthof(index); i++)
                vals[i] = -1;
 
        pgstat_progress_update_multi_param(lengthof(index), index, vals);
index 5ea8c455cd4abca1cd2b2179179fe58617b59b73..f0a6ed20c6095704c0cb67c326d96655765e8de4 100644 (file)
@@ -79,7 +79,7 @@ static const ConflictLogColumnDef ConflictLogSchema[] = {
        {.attname = "local_conflicts", .atttypid = JSONARRAYOID}
 };
 
-#define NUM_CONFLICT_ATTRS lengthof(ConflictLogSchema)
+#define NUM_CONFLICT_ATTRS ((AttrNumber) lengthof(ConflictLogSchema))
 
 static const char *const ConflictTypeNames[] = {
        [CT_INSERT_EXISTS] = "insert_exists",
index c81fb61a06a5e5fe99ee48b7f01d7f20bb66f047..9e758ebaa6f6026f602bd7b756ef57fe8c5e8043 100644 (file)
@@ -89,9 +89,7 @@ static const struct am_propname am_propnames[] =
 static IndexAMProperty
 lookup_prop_name(const char *name)
 {
-       int                     i;
-
-       for (i = 0; i < lengthof(am_propnames); i++)
+       for (size_t i = 0; i < lengthof(am_propnames); i++)
        {
                if (pg_strcasecmp(am_propnames[i].name, name) == 0)
                        return am_propnames[i].prop;
index 864c5ac1c85a55cb9a3b4152638b8dfc0c1d44d5..da0061ba1b8ca3128ff32bd445adebe3ed88bc13 100644 (file)
@@ -1803,9 +1803,8 @@ cannotCastJsonbValue(enum jbvType type, const char *sqltype, Node *escontext)
                {jbvObject, gettext_noop("cannot cast jsonb object to type %s")},
                {jbvBinary, gettext_noop("cannot cast jsonb array or object to type %s")}
        };
-       int                     i;
 
-       for (i = 0; i < lengthof(messages); i++)
+       for (size_t i = 0; i < lengthof(messages); i++)
                if (messages[i].type == type)
                        ereturn(escontext, (Datum) 0,
                                        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
index b69c87d8587e5e69ee9377384aa9ebdb43b454a5..74b6a793c9031352b4be3b6859471b2f2fed252d 100644 (file)
@@ -2527,7 +2527,6 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
 
                /* cache for format texts */
                static text *fmt_txt[lengthof(fmt_str)] = {0};
-               int                     i;
 
                /*
                 * Check for optional precision for methods other than .datetime() and
@@ -2554,7 +2553,7 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
                }
 
                /* loop until datetime format fits */
-               for (i = 0; i < lengthof(fmt_str); i++)
+               for (size_t i = 0; i < lengthof(fmt_str); i++)
                {
                        ErrorSaveContext escontext = {T_ErrorSaveContext};
 
index bcfa368406b9d732a0e633e37547176f9150db8c..e6166451a7b087041f20542697d8ab1090f69114 100644 (file)
@@ -107,11 +107,10 @@ iso8859_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
        bool            noError = PG_GETARG_BOOL(5);
-       int                     i;
 
        CHECK_ENCODING_CONVERSION_ARGS(-1, PG_UTF8);
 
-       for (i = 0; i < lengthof(maps); i++)
+       for (size_t i = 0; i < lengthof(maps); i++)
        {
                if (encoding == maps[i].encoding)
                {
@@ -143,11 +142,10 @@ utf8_to_iso8859(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
        bool            noError = PG_GETARG_BOOL(5);
-       int                     i;
 
        CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, -1);
 
-       for (i = 0; i < lengthof(maps); i++)
+       for (size_t i = 0; i < lengthof(maps); i++)
        {
                if (encoding == maps[i].encoding)
                {
index cf56fc16e8dc12e92bd71fca2f4275d8e6135388..e169a1d796f1f674cdadc43fc9b3cf92e0462eef 100644 (file)
@@ -88,11 +88,10 @@ win_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
        bool            noError = PG_GETARG_BOOL(5);
-       int                     i;
 
        CHECK_ENCODING_CONVERSION_ARGS(-1, PG_UTF8);
 
-       for (i = 0; i < lengthof(maps); i++)
+       for (size_t i = 0; i < lengthof(maps); i++)
        {
                if (encoding == maps[i].encoding)
                {
@@ -124,11 +123,10 @@ utf8_to_win(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
        bool            noError = PG_GETARG_BOOL(5);
-       int                     i;
 
        CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, -1);
 
-       for (i = 0; i < lengthof(maps); i++)
+       for (size_t i = 0; i < lengthof(maps); i++)
        {
                if (encoding == maps[i].encoding)
                {
index 9d95c5301daea0e1b239cfb0132ce6237b59a301..b3d496372ad7507014a4271c3bc7d09899d278d4 100644 (file)
@@ -3076,7 +3076,6 @@ initialize_data_directory(void)
 {
        PG_CMD_DECL;
        PQExpBufferData cmd;
-       int                     i;
 
        setup_signals();
 
@@ -3096,7 +3095,7 @@ initialize_data_directory(void)
        printf(_("creating subdirectories ... "));
        fflush(stdout);
 
-       for (i = 0; i < lengthof(subdirs); i++)
+       for (size_t i = 0; i < lengthof(subdirs); i++)
        {
                char       *path;
 
index 4d660d14b4ce10676c8ca30e69832bc2d61df1d8..4948e6d80c7ee493ecde277252f97ce973a522d0 100644 (file)
@@ -19156,7 +19156,7 @@ dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
 static inline SeqType
 parse_sequence_type(const char *name)
 {
-       for (int i = 0; i < lengthof(SeqTypeNames); i++)
+       for (size_t i = 0; i < lengthof(SeqTypeNames); i++)
        {
                if (strcmp(SeqTypeNames[i], name) == 0)
                        return (SeqType) i;
index 8ab35a8c83ecb8e49016c95272c37c4eed25eb71..90d22e12fdb7826d4357f7b4285b9437cad2f03f 100644 (file)
@@ -4938,14 +4938,13 @@ initCreateTables(PGconn *con)
                        1
                }
        };
-       int                     i;
        PQExpBufferData query;
 
        fprintf(stderr, "creating tables...\n");
 
        initPQExpBuffer(&query);
 
-       for (i = 0; i < lengthof(DDLs); i++)
+       for (size_t i = 0; i < lengthof(DDLs); i++)
        {
                const struct ddlinfo *ddl = &DDLs[i];
 
@@ -5246,13 +5245,12 @@ initCreatePKeys(PGconn *con)
                "alter table pgbench_tellers add primary key (tid)",
                "alter table pgbench_accounts add primary key (aid)"
        };
-       int                     i;
        PQExpBufferData query;
 
        fprintf(stderr, "creating primary keys...\n");
        initPQExpBuffer(&query);
 
-       for (i = 0; i < lengthof(DDLINDEXes); i++)
+       for (size_t i = 0; i < lengthof(DDLINDEXes); i++)
        {
                resetPQExpBuffer(&query);
                appendPQExpBufferStr(&query, DDLINDEXes[i]);
@@ -5286,10 +5284,9 @@ initCreateFKeys(PGconn *con)
                "alter table pgbench_history add constraint pgbench_history_tid_fkey foreign key (tid) references pgbench_tellers",
                "alter table pgbench_history add constraint pgbench_history_aid_fkey foreign key (aid) references pgbench_accounts"
        };
-       int                     i;
 
        fprintf(stderr, "creating foreign keys...\n");
-       for (i = 0; i < lengthof(DDLKEYs); i++)
+       for (size_t i = 0; i < lengthof(DDLKEYs); i++)
        {
                executeStatement(con, DDLKEYs[i]);
        }
@@ -6205,10 +6202,8 @@ process_builtin(const BuiltinScript *bi, int weight)
 static void
 listAvailableScripts(void)
 {
-       int                     i;
-
        fprintf(stderr, "Available builtin scripts:\n");
-       for (i = 0; i < lengthof(builtin_script); i++)
+       for (size_t i = 0; i < lengthof(builtin_script); i++)
                fprintf(stderr, "  %13s: %s\n", builtin_script[i].name, builtin_script[i].desc);
        fprintf(stderr, "\n");
 }
@@ -6217,12 +6212,11 @@ listAvailableScripts(void)
 static const BuiltinScript *
 findBuiltin(const char *name)
 {
-       int                     i,
-                               found = 0,
+       int                     found = 0,
                                len = strlen(name);
        const BuiltinScript *result = NULL;
 
-       for (i = 0; i < lengthof(builtin_script); i++)
+       for (size_t i = 0; i < lengthof(builtin_script); i++)
        {
                if (strncmp(builtin_script[i].name, name, len) == 0)
                {
index 7fdd4511a36228a5d93c591ca82e058f0f7d8f6b..ee85c05a00d6794ce2323896550389992f5d8175 100644 (file)
@@ -1000,9 +1000,7 @@ exec_command_crosstabview(PsqlScanState scan_state, bool active_branch)
 
        if (active_branch)
        {
-               int                     i;
-
-               for (i = 0; i < lengthof(pset.ctv_args); i++)
+               for (size_t i = 0; i < lengthof(pset.ctv_args); i++)
                        pset.ctv_args[i] = psql_scan_slash_option(scan_state,
                                                                                                          OT_NORMAL, NULL, true);
                pset.crosstab_flag = true;
@@ -5110,7 +5108,7 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
                {
                        int                     match_pos = -1;
 
-                       for (int i = 0; i < lengthof(formats); i++)
+                       for (size_t i = 0; i < lengthof(formats); i++)
                        {
                                if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
                                {
index 49ea584cd4f9d7f4402e35c310f48d68d2d8376c..1cacc8c3ea2cc3fd9c752391521f6d85b53c189c 100644 (file)
@@ -2018,7 +2018,7 @@ psql_completion(const char *text, int start, int end)
                 * as desirable interactions hidden in the order of the pattern
                 * checks.  TODO: think about a better way to manage that.
                 */
-               for (int tindx = 0; tindx < lengthof(tcpatterns); tindx++)
+               for (size_t tindx = 0; tindx < lengthof(tcpatterns); tindx++)
                {
                        const TCPattern *tcpat = tcpatterns + tindx;
                        bool            match = false;
index 0534ae34640ff383fa96843d6bf0901218146d46..6386ae636c594651885d6d8e2c44f82c13de76d3 100644 (file)
@@ -286,9 +286,7 @@ recompose_code(uint32 start, uint32 code, uint32 *result)
 
 #else
 
-               int                     i;
-
-               for (i = 0; i < lengthof(UnicodeDecompMain); i++)
+               for (size_t i = 0; i < lengthof(UnicodeDecompMain); i++)
                {
                        entry = &UnicodeDecompMain[i];
 
index f05aaea96510a9726f1c2b220cf30ddea576d4e4..e3bddf912032725b86119695fe10d26df0c04c7a 100644 (file)
@@ -557,7 +557,7 @@ pg_SASL_init(PGconn *conn, int payloadlen, bool *async)
        {
                bool            allowed = false;
 
-               for (int i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
+               for (size_t i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
                {
                        if (conn->sasl == conn->allowed_sasl_mechs[i])
                        {
index f2232d311ce4372123079b4064922416037909f0..17c2288e9bcded7952e3d558f8124f85220597c3 100644 (file)
@@ -1215,7 +1215,7 @@ fill_allowed_sasl_mechs(PGconn *conn)
        StaticAssertDecl(lengthof(conn->allowed_sasl_mechs) == SASL_MECHANISM_COUNT,
                                         "conn->allowed_sasl_mechs[] is not sufficiently large for holding all supported SASL mechanisms");
 
-       for (int i = 0; i < SASL_MECHANISM_COUNT; i++)
+       for (size_t i = 0; i < SASL_MECHANISM_COUNT; i++)
                conn->allowed_sasl_mechs[i] = supported_sasl_mechs[i];
 }
 
@@ -1225,7 +1225,7 @@ fill_allowed_sasl_mechs(PGconn *conn)
 static inline void
 clear_allowed_sasl_mechs(PGconn *conn)
 {
-       for (int i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
+       for (size_t i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
                conn->allowed_sasl_mechs[i] = NULL;
 }
 
@@ -1236,7 +1236,7 @@ clear_allowed_sasl_mechs(PGconn *conn)
 static inline int
 index_of_allowed_sasl_mech(PGconn *conn, const pg_fe_sasl_mech *mech)
 {
-       for (int i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
+       for (size_t i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
        {
                if (conn->allowed_sasl_mechs[i] == mech)
                        return i;
@@ -1256,8 +1256,6 @@ index_of_allowed_sasl_mech(PGconn *conn, const pg_fe_sasl_mech *mech)
 bool
 pqConnectOptions2(PGconn *conn)
 {
-       int                     i;
-
        /*
         * Allocate memory for details about each host to which we might possibly
         * try to connect.  For that, count the number of elements in the hostaddr
@@ -1281,6 +1279,7 @@ pqConnectOptions2(PGconn *conn)
         */
        if (conn->pghostaddr != NULL && conn->pghostaddr[0] != '\0')
        {
+               int                     i;
                char       *s = conn->pghostaddr;
                bool            more = true;
 
@@ -1302,6 +1301,7 @@ pqConnectOptions2(PGconn *conn)
 
        if (conn->pghost != NULL && conn->pghost[0] != '\0')
        {
+               int                     i;
                char       *s = conn->pghost;
                bool            more = true;
 
@@ -1326,7 +1326,7 @@ pqConnectOptions2(PGconn *conn)
         * Now, for each host slot, identify the type of address spec, and fill in
         * the default address if nothing was given.
         */
-       for (i = 0; i < conn->nconnhost; i++)
+       for (int i = 0; i < conn->nconnhost; i++)
        {
                pg_conn_host *ch = &conn->connhost[i];
 
@@ -1370,6 +1370,7 @@ pqConnectOptions2(PGconn *conn)
         */
        if (conn->pgport != NULL && conn->pgport[0] != '\0')
        {
+               int                     i;
                char       *s = conn->pgport;
                bool            more = true;
 
@@ -1453,7 +1454,7 @@ pqConnectOptions2(PGconn *conn)
 
                if (conn->pgpassfile != NULL && conn->pgpassfile[0] != '\0')
                {
-                       for (i = 0; i < conn->nconnhost; i++)
+                       for (int i = 0; i < conn->nconnhost; i++)
                        {
                                /*
                                 * Try to get a password for this host from file.  We use host
@@ -1639,6 +1640,8 @@ pqConnectOptions2(PGconn *conn)
 
                                if (negated)
                                {
+                                       int                     i;
+
                                        /* Remove the existing mechanism from the list. */
                                        i = index_of_allowed_sasl_mech(conn, mech);
                                        if (i < 0)
@@ -1648,6 +1651,8 @@ pqConnectOptions2(PGconn *conn)
                                }
                                else
                                {
+                                       int                     i;
+
                                        /*
                                         * Find a space to put the new mechanism (after making
                                         * sure it's not already there).
@@ -1721,7 +1726,7 @@ pqConnectOptions2(PGconn *conn)
                                | (1 << AUTH_REQ_SASL_CONT)
                                | (1 << AUTH_REQ_SASL_FIN);
 
-                       for (i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
+                       for (size_t i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
                        {
                                if (conn->allowed_sasl_mechs[i])
                                {
@@ -2113,7 +2118,7 @@ pqConnectOptions2(PGconn *conn)
                 * last integer last).  The swap step can be optimized by combining it
                 * with the insertion.
                 */
-               for (i = 1; i < conn->nconnhost; i++)
+               for (int i = 1; i < conn->nconnhost; i++)
                {
                        int                     j = pg_prng_uint64_range(&conn->prng_state, 0, i);
                        pg_conn_host temp = conn->connhost[j];
index 042dcb4c5cf53be257f0347b2e17c995375ad6f5..344fb7b870870d5f59a27074e76febcfc2c9c6bc 100644 (file)
@@ -417,9 +417,7 @@ plpgsql_push_back_token(int token, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t
 bool
 plpgsql_token_is_unreserved_keyword(int token)
 {
-       int                     i;
-
-       for (i = 0; i < lengthof(UnreservedPLKeywordTokens); i++)
+       for (size_t i = 0; i < lengthof(UnreservedPLKeywordTokens); i++)
        {
                if (UnreservedPLKeywordTokens[i] == token)
                        return true;
index 640cb97a62ef29e595b0ca4f768291606b5d8871..fcde46599579b32ef55dfcb2b932b725f1f5299d 100644 (file)
@@ -103,7 +103,7 @@ lconv_char_member(struct lconv *lconv, int i)
 void
 pg_localeconv_free(struct lconv *lconv)
 {
-       for (int i = 0; i < lengthof(table); ++i)
+       for (size_t i = 0; i < lengthof(table); ++i)
                if (table[i].is_string)
                        free(*lconv_string_member(lconv, i));
 }
@@ -117,7 +117,7 @@ pg_localeconv_from_langinfo(struct lconv *dst,
                                                        locale_t monetary_locale,
                                                        locale_t numeric_locale)
 {
-       for (int i = 0; i < lengthof(table); ++i)
+       for (size_t i = 0; i < lengthof(table); ++i)
        {
                locale_t        locale;
 
@@ -156,7 +156,7 @@ pg_localeconv_copy_members(struct lconv *dst,
                                                   struct lconv *src,
                                                   int category)
 {
-       for (int i = 0; i < lengthof(table); ++i)
+       for (size_t i = 0; i < lengthof(table); ++i)
        {
                if (table[i].category != category)
                        continue;
index 62233b29356717719e97424ca63e6dd1274ca885..11d854c73702878be71358a8833af3e8832fdbde 100644 (file)
@@ -176,15 +176,13 @@ static const struct
 void
 _dosmaperr(unsigned long e)
 {
-       int                     i;
-
        if (e == 0)
        {
                errno = 0;
                return;
        }
 
-       for (i = 0; i < lengthof(doserrors); i++)
+       for (size_t i = 0; i < lengthof(doserrors); i++)
        {
                if (doserrors[i].winerr == e)
                {
index 86e4719f3333aa2170ca78d3e0dc30f16c5c0830..e6c57946aebf027289bc1e6ba460a4cfe5f26999 100644 (file)
@@ -49,7 +49,7 @@ initialize_ntdll(void)
                return -1;
        }
 
-       for (int i = 0; i < lengthof(routines); ++i)
+       for (size_t i = 0; i < lengthof(routines); ++i)
        {
                pg_funcptr_t address;
 
index 85fb4c08bf20191b70a8327f3338759019799477..e8b4dc668ea730f7dfbd3afb79527ab1b6f20ffc 100644 (file)
@@ -147,7 +147,7 @@ validator_startup(ValidatorModuleState *state)
         * startup_cb).
         */
        RegisterOAuthHBAOptions(state, lengthof(hba_opts), hba_opts);
-       for (int i = 0; i < lengthof(hba_opts); i++)
+       for (size_t i = 0; i < lengthof(hba_opts); i++)
        {
                if (GetOAuthHBAOption(state, hba_opts[i]))
                        elog(ERROR,
index 816241f9c54e5eb27af6cc7119ab86849414bd17..4b556f738913c9559e5e1964af48a20c446c163b 100644 (file)
@@ -867,7 +867,7 @@ test_one_vector(pe_test_config *tc, const pe_test_vector *tv)
                exit(1);
        }
 
-       for (int escoff = 0; escoff < lengthof(pe_test_escape_funcs); escoff++)
+       for (size_t escoff = 0; escoff < lengthof(pe_test_escape_funcs); escoff++)
        {
                const pe_test_escape_func *ef = &pe_test_escape_funcs[escoff];
 
@@ -955,7 +955,7 @@ main(int argc, char *argv[])
        test_gb18030_page_multiple(&tc);
        test_gb18030_json(&tc);
 
-       for (int i = 0; i < lengthof(pe_test_vectors); i++)
+       for (size_t i = 0; i < lengthof(pe_test_vectors); i++)
        {
                test_one_vector(&tc, &pe_test_vectors[i]);
        }
index 9bc18a502f4a94557aa6301e631a9af9f03f3ab3..f24ee886eb0b202d867d161f6a2f637b0dd9e928 100644 (file)
@@ -116,7 +116,7 @@ test_integerset(PG_FUNCTION_ARGS)
        test_single_value_and_filler(PG_UINT64_MAX, 1000, 2000);
 
        /* Test different test patterns, with lots of entries */
-       for (int i = 0; i < lengthof(test_specs); i++)
+       for (size_t i = 0; i < lengthof(test_specs); i++)
        {
                test_pattern(&test_specs[i]);
        }
index c8dfada660bd49c3c2d02d2ada3f3d02a11db3ed..da2a47d3aad904d14aad587b175086afa3ba5e5c 100644 (file)
@@ -435,7 +435,7 @@ test_radixtree(PG_FUNCTION_ARGS)
 
        test_empty();
 
-       for (int i = 0; i < lengthof(rt_node_class_tests); i++)
+       for (size_t i = 0; i < lengthof(rt_node_class_tests); i++)
        {
                rt_node_class_test_elem *test_info = &(rt_node_class_tests[i]);
 
index d5aafdf370c51baa37a25bba55f8f71d26054441..6ee6689a46888a5569998dca42ccf3a7f231a437 100644 (file)
@@ -1409,7 +1409,7 @@ test_instr_time(PG_FUNCTION_ARGS)
         */
        max_err = (ticks_per_ns_scaled >> TICKS_TO_NS_SHIFT) + 1;
 
-       for (int i = 0; i < lengthof(test_ns); i++)
+       for (size_t i = 0; i < lengthof(test_ns); i++)
        {
                int64           result;
 
index 1f89e220730bf5e9c01a9c29a08843c3f34b723e..cc6550bdb1434d9418512c7e3db93fdf2a9032ae 100644 (file)
@@ -1105,7 +1105,7 @@ catch_signals(void)
 #endif
                SIGTERM
        };
-       int                     i;
+       size_t          i;
 
        for (i = 0; i < sizeof signals / sizeof signals[0]; i++)
        {