Unwind #if spaghetti in hmac_openssl.c a bit.

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
This commit is contained in:
Tom Lane 2024-04-02 10:41:37 -04:00
parent cafe105655
commit 38698dd38e
1 changed files with 18 additions and 14 deletions

View File

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