Unwind #if spaghetti in hmac_openssl.c a bit.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 2 Apr 2024 14:41:37 +0000 (10:41 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 2 Apr 2024 14:41:44 +0000 (10:41 -0400)
Make this code a little less confusing by defining a separate macro
that controls whether we'll use ResourceOwner facilities to track
the existence of a pg_hmac_ctx context.

The proximate reason to touch this is that since b8bff07da, we got
"unused function" warnings if building with older OpenSSL, because
the #if guards around the ResourceOwner wrapper function definitions
were different from those around the calls of those functions.
Pulling the ResourceOwner machinations outside of the #ifdef HAVE_xxx
guards fixes that and makes the code clearer too.

Discussion: https://postgr.es/m/1394271.1712016101@sss.pgh.pa.us

src/common/hmac_openssl.c

index d10f7e5af73d5dfbc8276a38ed022efb0997b517..84fcf340d8e5768d0f0a1e6b303c523b686bb7ba 100644 (file)
@@ -41,6 +41,7 @@
  */
 #ifndef FRONTEND
 #ifdef HAVE_HMAC_CTX_NEW
+#define USE_RESOWNER_FOR_HMAC
 #define ALLOC(size) MemoryContextAlloc(TopMemoryContext, size)
 #else
 #define ALLOC(size) palloc(size)
@@ -67,13 +68,13 @@ struct pg_hmac_ctx
        pg_hmac_errno error;
        const char *errreason;
 
-#ifndef FRONTEND
+#ifdef USE_RESOWNER_FOR_HMAC
        ResourceOwner resowner;
 #endif
 };
 
 /* ResourceOwner callbacks to hold HMAC contexts */
-#ifndef FRONTEND
+#ifdef USE_RESOWNER_FOR_HMAC
 static void ResOwnerReleaseHMAC(Datum res);
 
 static const ResourceOwnerDesc hmac_resowner_desc =
@@ -138,10 +139,12 @@ pg_hmac_create(pg_cryptohash_type type)
         * previous runs.
         */
        ERR_clear_error();
-#ifdef HAVE_HMAC_CTX_NEW
-#ifndef FRONTEND
+
+#ifdef USE_RESOWNER_FOR_HMAC
        ResourceOwnerEnlarge(CurrentResourceOwner);
 #endif
+
+#ifdef HAVE_HMAC_CTX_NEW
        ctx->hmacctx = HMAC_CTX_new();
 #else
        ctx->hmacctx = ALLOC(sizeof(HMAC_CTX));
@@ -159,14 +162,14 @@ pg_hmac_create(pg_cryptohash_type type)
                return NULL;
        }
 
-#ifdef HAVE_HMAC_CTX_NEW
-#ifndef FRONTEND
+#ifndef HAVE_HMAC_CTX_NEW
+       memset(ctx->hmacctx, 0, sizeof(HMAC_CTX));
+#endif
+
+#ifdef USE_RESOWNER_FOR_HMAC
        ctx->resowner = CurrentResourceOwner;
        ResourceOwnerRememberHMAC(CurrentResourceOwner, ctx);
 #endif
-#else
-       memset(ctx->hmacctx, 0, sizeof(HMAC_CTX));
-#endif                                                 /* HAVE_HMAC_CTX_NEW */
 
        return ctx;
 }
@@ -327,15 +330,16 @@ pg_hmac_free(pg_hmac_ctx *ctx)
 
 #ifdef HAVE_HMAC_CTX_FREE
        HMAC_CTX_free(ctx->hmacctx);
-#ifndef FRONTEND
-       if (ctx->resowner)
-               ResourceOwnerForgetHMAC(ctx->resowner, ctx);
-#endif
 #else
        explicit_bzero(ctx->hmacctx, sizeof(HMAC_CTX));
        FREE(ctx->hmacctx);
 #endif
 
+#ifdef USE_RESOWNER_FOR_HMAC
+       if (ctx->resowner)
+               ResourceOwnerForgetHMAC(ctx->resowner, ctx);
+#endif
+
        explicit_bzero(ctx, sizeof(pg_hmac_ctx));
        FREE(ctx);
 }
@@ -375,7 +379,7 @@ pg_hmac_error(pg_hmac_ctx *ctx)
 
 /* ResourceOwner callbacks */
 
-#ifndef FRONTEND
+#ifdef USE_RESOWNER_FOR_HMAC
 static void
 ResOwnerReleaseHMAC(Datum res)
 {