From 2998b8d24aed74221fede4954155b31fc7601b62 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 8 Dec 2022 14:32:43 +0100 Subject: [PATCH 2/4] Move array-size-related static assertions Since the addition of StaticAssertDecl(), we can put static assertions at the file level. Some static assertions that check the size of an array against some constant can now be moved closer to where those things are defined, instead of being stuck in some unrelated function body. --- src/backend/catalog/dependency.c | 12 ++++++------ src/backend/libpq/hba.c | 12 ++++++------ src/backend/utils/cache/syscache.c | 6 +++--- src/common/encnames.c | 6 +++--- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/backend/catalog/dependency.c b/src/backend/catalog/dependency.c index 7f3e64b5ae..30394dccf5 100644 --- a/src/backend/catalog/dependency.c +++ b/src/backend/catalog/dependency.c @@ -191,6 +191,12 @@ static const Oid object_classes[] = { TransformRelationId /* OCLASS_TRANSFORM */ }; +/* + * Make sure object_classes is kept up to date with the ObjectClass enum. + */ +StaticAssertDecl(lengthof(object_classes) == LAST_OCLASS + 1, + "object_classes[] must cover all ObjectClasses"); + static void findDependentObjects(const ObjectAddress *object, int objflags, @@ -2550,12 +2556,6 @@ add_object_address(ObjectClass oclass, Oid objectId, int32 subId, { ObjectAddress *item; - /* - * Make sure object_classes is kept up to date with the ObjectClass enum. - */ - StaticAssertStmt(lengthof(object_classes) == LAST_OCLASS + 1, - "object_classes[] must cover all ObjectClasses"); - /* enlarge array if needed */ if (addrs->numrefs >= addrs->maxrefs) { diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c index 46e91441ac..870b907697 100644 --- a/src/backend/libpq/hba.c +++ b/src/backend/libpq/hba.c @@ -127,6 +127,12 @@ static const char *const UserAuthName[] = "peer" }; +/* + * Make sure UserAuthName[] tracks additions to the UserAuth enum + */ +StaticAssertDecl(lengthof(UserAuthName) == USER_AUTH_LAST + 1, + "UserAuthName[] must match the UserAuth enum"); + static List *tokenize_expand_file(List *tokens, const char *outer_filename, const char *inc_filename, int elevel, @@ -3117,11 +3123,5 @@ hba_getauthmethod(hbaPort *port) const char * hba_authname(UserAuth auth_method) { - /* - * Make sure UserAuthName[] tracks additions to the UserAuth enum - */ - StaticAssertStmt(lengthof(UserAuthName) == USER_AUTH_LAST + 1, - "UserAuthName[] must match the UserAuth enum"); - return UserAuthName[auth_method]; } diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c index eec644ec84..bb3dd6f4d2 100644 --- a/src/backend/utils/cache/syscache.c +++ b/src/backend/utils/cache/syscache.c @@ -1040,6 +1040,9 @@ static const struct cachedesc cacheinfo[] = { } }; +StaticAssertDecl(SysCacheSize == (int) lengthof(cacheinfo), + "SysCacheSize does not match syscache.c's array"); + static CatCache *SysCache[SysCacheSize]; static bool CacheInitialized = false; @@ -1068,9 +1071,6 @@ InitCatalogCache(void) { int cacheId; - StaticAssertStmt(SysCacheSize == (int) lengthof(cacheinfo), - "SysCacheSize does not match syscache.c's array"); - Assert(!CacheInitialized); SysCacheRelationOidSize = SysCacheSupportingRelOidSize = 0; diff --git a/src/common/encnames.c b/src/common/encnames.c index 596a23b64d..2329db572b 100644 --- a/src/common/encnames.c +++ b/src/common/encnames.c @@ -451,6 +451,9 @@ static const char *const pg_enc2icu_tbl[] = "KOI8-U", /* PG_KOI8U */ }; +StaticAssertDecl(lengthof(pg_enc2icu_tbl) == PG_ENCODING_BE_LAST + 1, + "pg_enc2icu_tbl incomplete"); + /* * Is this encoding supported by ICU? @@ -469,9 +472,6 @@ is_encoding_supported_by_icu(int encoding) const char * get_encoding_name_for_icu(int encoding) { - StaticAssertStmt(lengthof(pg_enc2icu_tbl) == PG_ENCODING_BE_LAST + 1, - "pg_enc2icu_tbl incomplete"); - if (!PG_VALID_BE_ENCODING(encoding)) return NULL; return pg_enc2icu_tbl[encoding]; -- 2.38.1