From 08010249f2d9184d62f6da1cbcb225ca69c0b2ae Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <daniel@yesql.se>
Date: Fri, 18 Sep 2020 21:49:04 +0200
Subject: [PATCH] Remove memory allocation abstraction in pgcrypto

PX_OWN_ALLOC was intended as a way to disable the use of palloc, but
since the code already had quite a few palloc calls that didn't go
via px_alloc it's hardly an abstraction worth maintaining anymore.
---
 contrib/pgcrypto/imath.c            | 10 ++++-----
 contrib/pgcrypto/internal-sha2.c    | 28 +++++++++++--------------
 contrib/pgcrypto/internal.c         | 32 ++++++++++++-----------------
 contrib/pgcrypto/mbuf.c             | 30 +++++++++++++--------------
 contrib/pgcrypto/openssl.c          |  8 ++++----
 contrib/pgcrypto/pgp-cfb.c          |  5 ++---
 contrib/pgcrypto/pgp-compress.c     | 18 ++++++++--------
 contrib/pgcrypto/pgp-decrypt.c      | 11 +++++-----
 contrib/pgcrypto/pgp-encrypt.c      |  9 ++++----
 contrib/pgcrypto/pgp-mpi-internal.c |  6 +++---
 contrib/pgcrypto/pgp-mpi.c          |  4 ++--
 contrib/pgcrypto/pgp-pubenc.c       | 12 +++++------
 contrib/pgcrypto/pgp-pubkey.c       |  5 ++---
 contrib/pgcrypto/pgp.c              |  5 ++---
 contrib/pgcrypto/px-hmac.c          | 21 +++++++++----------
 contrib/pgcrypto/px.c               | 32 +++++++++++++----------------
 contrib/pgcrypto/px.h               | 13 ------------
 17 files changed, 106 insertions(+), 143 deletions(-)

diff --git a/contrib/pgcrypto/imath.c b/contrib/pgcrypto/imath.c
index da4cdede76..9deaa797c1 100644
--- a/contrib/pgcrypto/imath.c
+++ b/contrib/pgcrypto/imath.c
@@ -478,7 +478,7 @@ mp_int_init(mp_int z)
 mp_int
 mp_int_alloc(void)
 {
-	mp_int		out = px_alloc(sizeof(mpz_t));
+	mp_int		out = palloc(sizeof(mpz_t));
 
 	if (out != NULL)
 		mp_int_init(out);
@@ -604,7 +604,7 @@ mp_int_free(mp_int z)
 	assert(z != NULL);
 
 	mp_int_clear(z);
-	px_free(z);					/* note: NOT s_free() */
+	pfree(z);					/* note: NOT s_free() */
 }
 
 mp_result
@@ -2212,7 +2212,7 @@ static const mp_digit fill = (mp_digit) 0xdeadbeefabad1dea;
 static mp_digit *
 s_alloc(mp_size num)
 {
-	mp_digit   *out = px_alloc(num * sizeof(mp_digit));
+	mp_digit   *out = palloc(num * sizeof(mp_digit));
 
 	assert(out != NULL);
 
@@ -2235,7 +2235,7 @@ s_realloc(mp_digit *old, mp_size osize, mp_size nsize)
 		new[ix] = fill;
 	memcpy(new, old, osize * sizeof(mp_digit));
 #else
-	mp_digit   *new = px_realloc(old, nsize * sizeof(mp_digit));
+	mp_digit   *new = repalloc(old, nsize * sizeof(mp_digit));
 
 	assert(new != NULL);
 #endif
@@ -2246,7 +2246,7 @@ s_realloc(mp_digit *old, mp_size osize, mp_size nsize)
 static void
 s_free(void *ptr)
 {
-	px_free(ptr);
+	pfree(ptr);
 }
 
 static bool
diff --git a/contrib/pgcrypto/internal-sha2.c b/contrib/pgcrypto/internal-sha2.c
index e06f55445e..9fa940b5bb 100644
--- a/contrib/pgcrypto/internal-sha2.c
+++ b/contrib/pgcrypto/internal-sha2.c
@@ -85,8 +85,8 @@ int_sha224_free(PX_MD *h)
 	pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
 
 	px_memset(ctx, 0, sizeof(*ctx));
-	px_free(ctx);
-	px_free(h);
+	pfree(ctx);
+	pfree(h);
 }
 
 /* SHA256 */
@@ -133,8 +133,8 @@ int_sha256_free(PX_MD *h)
 	pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
 
 	px_memset(ctx, 0, sizeof(*ctx));
-	px_free(ctx);
-	px_free(h);
+	pfree(ctx);
+	pfree(h);
 }
 
 /* SHA384 */
@@ -181,8 +181,8 @@ int_sha384_free(PX_MD *h)
 	pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
 
 	px_memset(ctx, 0, sizeof(*ctx));
-	px_free(ctx);
-	px_free(h);
+	pfree(ctx);
+	pfree(h);
 }
 
 /* SHA512 */
@@ -229,8 +229,8 @@ int_sha512_free(PX_MD *h)
 	pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
 
 	px_memset(ctx, 0, sizeof(*ctx));
-	px_free(ctx);
-	px_free(h);
+	pfree(ctx);
+	pfree(h);
 }
 
 /* init functions */
@@ -240,8 +240,7 @@ init_sha224(PX_MD *md)
 {
 	pg_sha224_ctx *ctx;
 
-	ctx = px_alloc(sizeof(*ctx));
-	memset(ctx, 0, sizeof(*ctx));
+	ctx = palloc0(sizeof(*ctx));
 
 	md->p.ptr = ctx;
 
@@ -260,8 +259,7 @@ init_sha256(PX_MD *md)
 {
 	pg_sha256_ctx *ctx;
 
-	ctx = px_alloc(sizeof(*ctx));
-	memset(ctx, 0, sizeof(*ctx));
+	ctx = palloc0(sizeof(*ctx));
 
 	md->p.ptr = ctx;
 
@@ -280,8 +278,7 @@ init_sha384(PX_MD *md)
 {
 	pg_sha384_ctx *ctx;
 
-	ctx = px_alloc(sizeof(*ctx));
-	memset(ctx, 0, sizeof(*ctx));
+	ctx = palloc0(sizeof(*ctx));
 
 	md->p.ptr = ctx;
 
@@ -300,8 +297,7 @@ init_sha512(PX_MD *md)
 {
 	pg_sha512_ctx *ctx;
 
-	ctx = px_alloc(sizeof(*ctx));
-	memset(ctx, 0, sizeof(*ctx));
+	ctx = palloc0(sizeof(*ctx));
 
 	md->p.ptr = ctx;
 
diff --git a/contrib/pgcrypto/internal.c b/contrib/pgcrypto/internal.c
index a12d7b4178..06469d41c0 100644
--- a/contrib/pgcrypto/internal.c
+++ b/contrib/pgcrypto/internal.c
@@ -123,8 +123,8 @@ int_md5_free(PX_MD *h)
 	MD5_CTX    *ctx = (MD5_CTX *) h->p.ptr;
 
 	px_memset(ctx, 0, sizeof(*ctx));
-	px_free(ctx);
-	px_free(h);
+	pfree(ctx);
+	pfree(h);
 }
 
 /* SHA1 */
@@ -171,8 +171,8 @@ int_sha1_free(PX_MD *h)
 	SHA1_CTX   *ctx = (SHA1_CTX *) h->p.ptr;
 
 	px_memset(ctx, 0, sizeof(*ctx));
-	px_free(ctx);
-	px_free(h);
+	pfree(ctx);
+	pfree(h);
 }
 
 /* init functions */
@@ -182,8 +182,7 @@ init_md5(PX_MD *md)
 {
 	MD5_CTX    *ctx;
 
-	ctx = px_alloc(sizeof(*ctx));
-	memset(ctx, 0, sizeof(*ctx));
+	ctx = palloc0(sizeof(*ctx));
 
 	md->p.ptr = ctx;
 
@@ -202,8 +201,7 @@ init_sha1(PX_MD *md)
 {
 	SHA1_CTX   *ctx;
 
-	ctx = px_alloc(sizeof(*ctx));
-	memset(ctx, 0, sizeof(*ctx));
+	ctx = palloc0(sizeof(*ctx));
 
 	md->p.ptr = ctx;
 
@@ -246,9 +244,9 @@ intctx_free(PX_Cipher *c)
 	if (cx)
 	{
 		px_memset(cx, 0, sizeof *cx);
-		px_free(cx);
+		pfree(cx);
 	}
-	px_free(c);
+	pfree(c);
 }
 
 /*
@@ -373,8 +371,7 @@ rj_load(int mode)
 	PX_Cipher  *c;
 	struct int_ctx *cx;
 
-	c = px_alloc(sizeof *c);
-	memset(c, 0, sizeof *c);
+	c = palloc0(sizeof *c);
 
 	c->block_size = rj_block_size;
 	c->key_size = rj_key_size;
@@ -384,8 +381,7 @@ rj_load(int mode)
 	c->decrypt = rj_decrypt;
 	c->free = intctx_free;
 
-	cx = px_alloc(sizeof *cx);
-	memset(cx, 0, sizeof *cx);
+	cx = palloc0(sizeof *cx);
 	cx->mode = mode;
 
 	c->ptr = cx;
@@ -482,8 +478,7 @@ bf_load(int mode)
 	PX_Cipher  *c;
 	struct int_ctx *cx;
 
-	c = px_alloc(sizeof *c);
-	memset(c, 0, sizeof *c);
+	c = palloc0(sizeof *c);
 
 	c->block_size = bf_block_size;
 	c->key_size = bf_key_size;
@@ -493,8 +488,7 @@ bf_load(int mode)
 	c->decrypt = bf_decrypt;
 	c->free = intctx_free;
 
-	cx = px_alloc(sizeof *cx);
-	memset(cx, 0, sizeof *cx);
+	cx = palloc0(sizeof *cx);
 	cx->mode = mode;
 	c->ptr = cx;
 	return c;
@@ -564,7 +558,7 @@ px_find_digest(const char *name, PX_MD **res)
 	for (p = int_digest_list; p->name; p++)
 		if (pg_strcasecmp(p->name, name) == 0)
 		{
-			h = px_alloc(sizeof(*h));
+			h = palloc(sizeof(*h));
 			p->init(h);
 
 			*res = h;
diff --git a/contrib/pgcrypto/mbuf.c b/contrib/pgcrypto/mbuf.c
index 548ef62097..bc668a0e80 100644
--- a/contrib/pgcrypto/mbuf.c
+++ b/contrib/pgcrypto/mbuf.c
@@ -70,9 +70,9 @@ mbuf_free(MBuf *mbuf)
 	if (mbuf->own_data)
 	{
 		px_memset(mbuf->data, 0, mbuf->buf_end - mbuf->data);
-		px_free(mbuf->data);
+		pfree(mbuf->data);
 	}
-	px_free(mbuf);
+	pfree(mbuf);
 	return 0;
 }
 
@@ -88,7 +88,7 @@ prepare_room(MBuf *mbuf, int block_len)
 	newlen = (mbuf->buf_end - mbuf->data)
 		+ ((block_len + STEP + STEP - 1) & -STEP);
 
-	newbuf = px_realloc(mbuf->data, newlen);
+	newbuf = repalloc(mbuf->data, newlen);
 
 	mbuf->buf_end = newbuf + newlen;
 	mbuf->data_end = newbuf + (mbuf->data_end - mbuf->data);
@@ -121,8 +121,8 @@ mbuf_create(int len)
 	if (!len)
 		len = 8192;
 
-	mbuf = px_alloc(sizeof *mbuf);
-	mbuf->data = px_alloc(len);
+	mbuf = palloc(sizeof *mbuf);
+	mbuf->data = palloc(len);
 	mbuf->buf_end = mbuf->data + len;
 	mbuf->data_end = mbuf->data;
 	mbuf->read_pos = mbuf->data;
@@ -138,7 +138,7 @@ mbuf_create_from_data(uint8 *data, int len)
 {
 	MBuf	   *mbuf;
 
-	mbuf = px_alloc(sizeof *mbuf);
+	mbuf = palloc(sizeof *mbuf);
 	mbuf->data = (uint8 *) data;
 	mbuf->buf_end = mbuf->data + len;
 	mbuf->data_end = mbuf->data + len;
@@ -219,15 +219,14 @@ pullf_create(PullFilter **pf_p, const PullFilterOps *op, void *init_arg, PullFil
 		res = 0;
 	}
 
-	pf = px_alloc(sizeof(*pf));
-	memset(pf, 0, sizeof(*pf));
+	pf = palloc0(sizeof(*pf));
 	pf->buflen = res;
 	pf->op = op;
 	pf->priv = priv;
 	pf->src = src;
 	if (pf->buflen > 0)
 	{
-		pf->buf = px_alloc(pf->buflen);
+		pf->buf = palloc(pf->buflen);
 		pf->pos = 0;
 	}
 	else
@@ -248,11 +247,11 @@ pullf_free(PullFilter *pf)
 	if (pf->buf)
 	{
 		px_memset(pf->buf, 0, pf->buflen);
-		px_free(pf->buf);
+		pfree(pf->buf);
 	}
 
 	px_memset(pf, 0, sizeof(*pf));
-	px_free(pf);
+	pfree(pf);
 }
 
 /* may return less data than asked, 0 means eof */
@@ -386,15 +385,14 @@ pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFil
 		res = 0;
 	}
 
-	mp = px_alloc(sizeof(*mp));
-	memset(mp, 0, sizeof(*mp));
+	mp = palloc0(sizeof(*mp));
 	mp->block_size = res;
 	mp->op = op;
 	mp->priv = priv;
 	mp->next = next;
 	if (mp->block_size > 0)
 	{
-		mp->buf = px_alloc(mp->block_size);
+		mp->buf = palloc(mp->block_size);
 		mp->pos = 0;
 	}
 	else
@@ -415,11 +413,11 @@ pushf_free(PushFilter *mp)
 	if (mp->buf)
 	{
 		px_memset(mp->buf, 0, mp->block_size);
-		px_free(mp->buf);
+		pfree(mp->buf);
 	}
 
 	px_memset(mp, 0, sizeof(*mp));
-	px_free(mp);
+	pfree(mp);
 }
 
 void
diff --git a/contrib/pgcrypto/openssl.c b/contrib/pgcrypto/openssl.c
index 3057afb339..90951a8ae7 100644
--- a/contrib/pgcrypto/openssl.c
+++ b/contrib/pgcrypto/openssl.c
@@ -156,7 +156,7 @@ digest_free(PX_MD *h)
 	OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
 
 	free_openssl_digest(digest);
-	px_free(h);
+	pfree(h);
 }
 
 static int	px_openssl_initialized = 0;
@@ -214,7 +214,7 @@ px_find_digest(const char *name, PX_MD **res)
 	open_digests = digest;
 
 	/* The PX_MD object is allocated in the current memory context. */
-	h = px_alloc(sizeof(*h));
+	h = palloc(sizeof(*h));
 	h->result_size = digest_result_size;
 	h->block_size = digest_block_size;
 	h->reset = digest_reset;
@@ -353,7 +353,7 @@ gen_ossl_free(PX_Cipher *c)
 	OSSLCipher *od = (OSSLCipher *) c->ptr;
 
 	free_openssl_cipher(od);
-	px_free(c);
+	pfree(c);
 }
 
 static int
@@ -790,7 +790,7 @@ px_find_cipher(const char *name, PX_Cipher **res)
 		od->evp_ciph = i->ciph->cipher_func();
 
 	/* The PX_Cipher is allocated in current memory context */
-	c = px_alloc(sizeof(*c));
+	c = palloc(sizeof(*c));
 	c->block_size = gen_ossl_block_size;
 	c->key_size = gen_ossl_key_size;
 	c->iv_size = gen_ossl_iv_size;
diff --git a/contrib/pgcrypto/pgp-cfb.c b/contrib/pgcrypto/pgp-cfb.c
index 8ae7c8608f..dafa562daa 100644
--- a/contrib/pgcrypto/pgp-cfb.c
+++ b/contrib/pgcrypto/pgp-cfb.c
@@ -67,8 +67,7 @@ pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len,
 		return res;
 	}
 
-	ctx = px_alloc(sizeof(*ctx));
-	memset(ctx, 0, sizeof(*ctx));
+	ctx = palloc0(sizeof(*ctx));
 	ctx->ciph = ciph;
 	ctx->block_size = px_cipher_block_size(ciph);
 	ctx->resync = resync;
@@ -85,7 +84,7 @@ pgp_cfb_free(PGP_CFB *ctx)
 {
 	px_cipher_free(ctx->ciph);
 	px_memset(ctx, 0, sizeof(*ctx));
-	px_free(ctx);
+	pfree(ctx);
 }
 
 /*
diff --git a/contrib/pgcrypto/pgp-compress.c b/contrib/pgcrypto/pgp-compress.c
index 3636a662b0..7e8ddba187 100644
--- a/contrib/pgcrypto/pgp-compress.c
+++ b/contrib/pgcrypto/pgp-compress.c
@@ -57,13 +57,13 @@ struct ZipStat
 static void *
 z_alloc(void *priv, unsigned n_items, unsigned item_len)
 {
-	return px_alloc(n_items * item_len);
+	return palloc(n_items * item_len);
 }
 
 static void
 z_free(void *priv, void *addr)
 {
-	px_free(addr);
+	pfree(addr);
 }
 
 static int
@@ -80,8 +80,7 @@ compress_init(PushFilter *next, void *init_arg, void **priv_p)
 	/*
 	 * init
 	 */
-	st = px_alloc(sizeof(*st));
-	memset(st, 0, sizeof(*st));
+	st = palloc0(sizeof(*st));
 	st->buf_len = ZIP_OUT_BUF;
 	st->stream.zalloc = z_alloc;
 	st->stream.zfree = z_free;
@@ -93,7 +92,7 @@ compress_init(PushFilter *next, void *init_arg, void **priv_p)
 		res = deflateInit(&st->stream, ctx->compress_level);
 	if (res != Z_OK)
 	{
-		px_free(st);
+		pfree(st);
 		return PXE_PGP_COMPRESSION_ERROR;
 	}
 	*priv_p = st;
@@ -174,7 +173,7 @@ compress_free(void *priv)
 
 	deflateEnd(&st->stream);
 	px_memset(st, 0, sizeof(*st));
-	px_free(st);
+	pfree(st);
 }
 
 static const PushFilterOps
@@ -212,8 +211,7 @@ decompress_init(void **priv_p, void *arg, PullFilter *src)
 		&& ctx->compress_algo != PGP_COMPR_ZIP)
 		return PXE_PGP_UNSUPPORTED_COMPR;
 
-	dec = px_alloc(sizeof(*dec));
-	memset(dec, 0, sizeof(*dec));
+	dec = palloc0(sizeof(*dec));
 	dec->buf_len = ZIP_OUT_BUF;
 	*priv_p = dec;
 
@@ -226,7 +224,7 @@ decompress_init(void **priv_p, void *arg, PullFilter *src)
 		res = inflateInit(&dec->stream);
 	if (res != Z_OK)
 	{
-		px_free(dec);
+		pfree(dec);
 		px_debug("decompress_init: inflateInit error");
 		return PXE_PGP_COMPRESSION_ERROR;
 	}
@@ -318,7 +316,7 @@ decompress_free(void *priv)
 
 	inflateEnd(&dec->stream);
 	px_memset(dec, 0, sizeof(*dec));
-	px_free(dec);
+	pfree(dec);
 }
 
 static const PullFilterOps
diff --git a/contrib/pgcrypto/pgp-decrypt.c b/contrib/pgcrypto/pgp-decrypt.c
index 3ecbf9c0c2..d12dcad194 100644
--- a/contrib/pgcrypto/pgp-decrypt.c
+++ b/contrib/pgcrypto/pgp-decrypt.c
@@ -211,7 +211,7 @@ pktreader_free(void *priv)
 	struct PktData *pkt = priv;
 
 	px_memset(pkt, 0, sizeof(*pkt));
-	px_free(pkt);
+	pfree(pkt);
 }
 
 static struct PullFilterOps pktreader_filter = {
@@ -224,13 +224,13 @@ pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len,
 					  int pkttype, PGP_Context *ctx)
 {
 	int			res;
-	struct PktData *pkt = px_alloc(sizeof(*pkt));
+	struct PktData *pkt = palloc(sizeof(*pkt));
 
 	pkt->type = pkttype;
 	pkt->len = len;
 	res = pullf_create(pf_p, &pktreader_filter, pkt, src);
 	if (res < 0)
-		px_free(pkt);
+		pfree(pkt);
 	return res;
 }
 
@@ -447,8 +447,7 @@ mdcbuf_init(void **priv_p, void *arg, PullFilter *src)
 	PGP_Context *ctx = arg;
 	struct MDCBufData *st;
 
-	st = px_alloc(sizeof(*st));
-	memset(st, 0, sizeof(*st));
+	st = palloc0(sizeof(*st));
 	st->buflen = sizeof(st->buf);
 	st->ctx = ctx;
 	*priv_p = st;
@@ -576,7 +575,7 @@ mdcbuf_free(void *priv)
 	px_md_free(st->ctx->mdc_ctx);
 	st->ctx->mdc_ctx = NULL;
 	px_memset(st, 0, sizeof(*st));
-	px_free(st);
+	pfree(st);
 }
 
 static struct PullFilterOps mdcbuf_filter = {
diff --git a/contrib/pgcrypto/pgp-encrypt.c b/contrib/pgcrypto/pgp-encrypt.c
index 46518942ac..f7467c9b1c 100644
--- a/contrib/pgcrypto/pgp-encrypt.c
+++ b/contrib/pgcrypto/pgp-encrypt.c
@@ -178,8 +178,7 @@ encrypt_init(PushFilter *next, void *init_arg, void **priv_p)
 	if (res < 0)
 		return res;
 
-	st = px_alloc(sizeof(*st));
-	memset(st, 0, sizeof(*st));
+	st = palloc0(sizeof(*st));
 	st->ciph = ciph;
 
 	*priv_p = st;
@@ -219,7 +218,7 @@ encrypt_free(void *priv)
 	if (st->ciph)
 		pgp_cfb_free(st->ciph);
 	px_memset(st, 0, sizeof(*st));
-	px_free(st);
+	pfree(st);
 }
 
 static const PushFilterOps encrypt_filter = {
@@ -241,7 +240,7 @@ pkt_stream_init(PushFilter *next, void *init_arg, void **priv_p)
 {
 	struct PktStreamStat *st;
 
-	st = px_alloc(sizeof(*st));
+	st = palloc(sizeof(*st));
 	st->final_done = 0;
 	st->pkt_block = 1 << STREAM_BLOCK_SHIFT;
 	*priv_p = st;
@@ -301,7 +300,7 @@ pkt_stream_free(void *priv)
 	struct PktStreamStat *st = priv;
 
 	px_memset(st, 0, sizeof(*st));
-	px_free(st);
+	pfree(st);
 }
 
 static const PushFilterOps pkt_stream_filter = {
diff --git a/contrib/pgcrypto/pgp-mpi-internal.c b/contrib/pgcrypto/pgp-mpi-internal.c
index 0cea514180..5b94e65452 100644
--- a/contrib/pgcrypto/pgp-mpi-internal.c
+++ b/contrib/pgcrypto/pgp-mpi-internal.c
@@ -60,10 +60,10 @@ mp_px_rand(uint32 bits, mpz_t *res)
 	int			last_bits = bits & 7;
 	uint8	   *buf;
 
-	buf = px_alloc(bytes);
+	buf = palloc(bytes);
 	if (!pg_strong_random(buf, bytes))
 	{
-		px_free(buf);
+		pfree(buf);
 		return PXE_NO_RANDOM;
 	}
 
@@ -78,7 +78,7 @@ mp_px_rand(uint32 bits, mpz_t *res)
 
 	mp_int_read_unsigned(res, buf, bytes);
 
-	px_free(buf);
+	pfree(buf);
 
 	return 0;
 }
diff --git a/contrib/pgcrypto/pgp-mpi.c b/contrib/pgcrypto/pgp-mpi.c
index 36a6d361ab..03be27973b 100644
--- a/contrib/pgcrypto/pgp-mpi.c
+++ b/contrib/pgcrypto/pgp-mpi.c
@@ -44,7 +44,7 @@ pgp_mpi_alloc(int bits, PGP_MPI **mpi)
 		px_debug("pgp_mpi_alloc: unreasonable request: bits=%d", bits);
 		return PXE_PGP_CORRUPT_DATA;
 	}
-	n = px_alloc(sizeof(*n) + len);
+	n = palloc(sizeof(*n) + len);
 	n->bits = bits;
 	n->bytes = len;
 	n->data = (uint8 *) (n) + sizeof(*n);
@@ -72,7 +72,7 @@ pgp_mpi_free(PGP_MPI *mpi)
 	if (mpi == NULL)
 		return 0;
 	px_memset(mpi, 0, sizeof(*mpi) + mpi->bytes);
-	px_free(mpi);
+	pfree(mpi);
 	return 0;
 }
 
diff --git a/contrib/pgcrypto/pgp-pubenc.c b/contrib/pgcrypto/pgp-pubenc.c
index 9fdcf7c31c..c254a37275 100644
--- a/contrib/pgcrypto/pgp-pubenc.c
+++ b/contrib/pgcrypto/pgp-pubenc.c
@@ -46,12 +46,12 @@ pad_eme_pkcs1_v15(uint8 *data, int data_len, int res_len, uint8 **res_p)
 	if (pad_len < 8)
 		return PXE_BUG;
 
-	buf = px_alloc(res_len);
+	buf = palloc(res_len);
 	buf[0] = 0x02;
 
 	if (!pg_strong_random(buf + 1, pad_len))
 	{
-		px_free(buf);
+		pfree(buf);
 		return PXE_NO_RANDOM;
 	}
 
@@ -64,7 +64,7 @@ pad_eme_pkcs1_v15(uint8 *data, int data_len, int res_len, uint8 **res_p)
 			if (!pg_strong_random(p, 1))
 			{
 				px_memset(buf, 0, res_len);
-				px_free(buf);
+				pfree(buf);
 				return PXE_NO_RANDOM;
 			}
 		}
@@ -97,7 +97,7 @@ create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p, int full_bytes)
 	/*
 	 * create "secret message"
 	 */
-	secmsg = px_alloc(klen + 3);
+	secmsg = palloc(klen + 3);
 	secmsg[0] = ctx->cipher_algo;
 	memcpy(secmsg + 1, ctx->sess_key, klen);
 	secmsg[klen + 1] = (cksum >> 8) & 0xFF;
@@ -118,10 +118,10 @@ create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p, int full_bytes)
 	if (padded)
 	{
 		px_memset(padded, 0, full_bytes);
-		px_free(padded);
+		pfree(padded);
 	}
 	px_memset(secmsg, 0, klen + 3);
-	px_free(secmsg);
+	pfree(secmsg);
 
 	if (res >= 0)
 		*msg_p = m;
diff --git a/contrib/pgcrypto/pgp-pubkey.c b/contrib/pgcrypto/pgp-pubkey.c
index d447e5fd4f..9a6561caf9 100644
--- a/contrib/pgcrypto/pgp-pubkey.c
+++ b/contrib/pgcrypto/pgp-pubkey.c
@@ -39,8 +39,7 @@ pgp_key_alloc(PGP_PubKey **pk_p)
 {
 	PGP_PubKey *pk;
 
-	pk = px_alloc(sizeof(*pk));
-	memset(pk, 0, sizeof(*pk));
+	pk = palloc0(sizeof(*pk));
 	*pk_p = pk;
 	return 0;
 }
@@ -78,7 +77,7 @@ pgp_key_free(PGP_PubKey *pk)
 			break;
 	}
 	px_memset(pk, 0, sizeof(*pk));
-	px_free(pk);
+	pfree(pk);
 }
 
 static int
diff --git a/contrib/pgcrypto/pgp.c b/contrib/pgcrypto/pgp.c
index 9b245fee61..3e9c2fef9b 100644
--- a/contrib/pgcrypto/pgp.c
+++ b/contrib/pgcrypto/pgp.c
@@ -200,8 +200,7 @@ pgp_init(PGP_Context **ctx_p)
 {
 	PGP_Context *ctx;
 
-	ctx = px_alloc(sizeof *ctx);
-	memset(ctx, 0, sizeof *ctx);
+	ctx = palloc0(sizeof *ctx);
 
 	ctx->cipher_algo = def_cipher_algo;
 	ctx->s2k_cipher_algo = def_s2k_cipher_algo;
@@ -226,7 +225,7 @@ pgp_free(PGP_Context *ctx)
 	if (ctx->pub_key)
 		pgp_key_free(ctx->pub_key);
 	px_memset(ctx, 0, sizeof *ctx);
-	px_free(ctx);
+	pfree(ctx);
 	return 0;
 }
 
diff --git a/contrib/pgcrypto/px-hmac.c b/contrib/pgcrypto/px-hmac.c
index 06e5148f1b..99174d2655 100644
--- a/contrib/pgcrypto/px-hmac.c
+++ b/contrib/pgcrypto/px-hmac.c
@@ -57,8 +57,7 @@ hmac_init(PX_HMAC *h, const uint8 *key, unsigned klen)
 	PX_MD	   *md = h->md;
 
 	bs = px_md_block_size(md);
-	keybuf = px_alloc(bs);
-	memset(keybuf, 0, bs);
+	keybuf = palloc0(bs);
 
 	if (klen > bs)
 	{
@@ -76,7 +75,7 @@ hmac_init(PX_HMAC *h, const uint8 *key, unsigned klen)
 	}
 
 	px_memset(keybuf, 0, bs);
-	px_free(keybuf);
+	pfree(keybuf);
 
 	px_md_update(md, h->p.ipad, bs);
 }
@@ -108,7 +107,7 @@ hmac_finish(PX_HMAC *h, uint8 *dst)
 	bs = px_md_block_size(md);
 	hlen = px_md_result_size(md);
 
-	buf = px_alloc(hlen);
+	buf = palloc(hlen);
 
 	px_md_finish(md, buf);
 
@@ -118,7 +117,7 @@ hmac_finish(PX_HMAC *h, uint8 *dst)
 	px_md_finish(md, dst);
 
 	px_memset(buf, 0, hlen);
-	px_free(buf);
+	pfree(buf);
 }
 
 static void
@@ -131,9 +130,9 @@ hmac_free(PX_HMAC *h)
 
 	px_memset(h->p.ipad, 0, bs);
 	px_memset(h->p.opad, 0, bs);
-	px_free(h->p.ipad);
-	px_free(h->p.opad);
-	px_free(h);
+	pfree(h->p.ipad);
+	pfree(h->p.opad);
+	pfree(h);
 }
 
 
@@ -158,9 +157,9 @@ px_find_hmac(const char *name, PX_HMAC **res)
 		return PXE_HASH_UNUSABLE_FOR_HMAC;
 	}
 
-	h = px_alloc(sizeof(*h));
-	h->p.ipad = px_alloc(bs);
-	h->p.opad = px_alloc(bs);
+	h = palloc(sizeof(*h));
+	h->p.ipad = palloc(bs);
+	h->p.opad = palloc(bs);
 	h->md = md;
 
 	h->result_size = hmac_result_size;
diff --git a/contrib/pgcrypto/px.c b/contrib/pgcrypto/px.c
index 0f02fb56c4..6a4681dae9 100644
--- a/contrib/pgcrypto/px.c
+++ b/contrib/pgcrypto/px.c
@@ -196,8 +196,7 @@ combo_init(PX_Combo *cx, const uint8 *key, unsigned klen,
 	ivs = px_cipher_iv_size(c);
 	if (ivs > 0)
 	{
-		ivbuf = px_alloc(ivs);
-		memset(ivbuf, 0, ivs);
+		ivbuf = palloc0(ivs);
 		if (ivlen > ivs)
 			memcpy(ivbuf, iv, ivs);
 		else
@@ -206,15 +205,15 @@ combo_init(PX_Combo *cx, const uint8 *key, unsigned klen,
 
 	if (klen > ks)
 		klen = ks;
-	keybuf = px_alloc(ks);
+	keybuf = palloc0(ks);
 	memset(keybuf, 0, ks);
 	memcpy(keybuf, key, klen);
 
 	err = px_cipher_init(c, keybuf, klen, ivbuf);
 
 	if (ivbuf)
-		px_free(ivbuf);
-	px_free(keybuf);
+		pfree(ivbuf);
+	pfree(keybuf);
 
 	return err;
 }
@@ -238,7 +237,7 @@ combo_encrypt(PX_Combo *cx, const uint8 *data, unsigned dlen,
 	/* encrypt */
 	if (bs > 1)
 	{
-		bbuf = px_alloc(bs * 4);
+		bbuf = palloc(bs * 4);
 		bpos = dlen % bs;
 		*rlen = dlen - bpos;
 		memcpy(bbuf, data + *rlen, bpos);
@@ -283,7 +282,7 @@ combo_encrypt(PX_Combo *cx, const uint8 *data, unsigned dlen,
 	}
 out:
 	if (bbuf)
-		px_free(bbuf);
+		pfree(bbuf);
 
 	return err;
 }
@@ -351,7 +350,7 @@ combo_free(PX_Combo *cx)
 	if (cx->cipher)
 		px_cipher_free(cx->cipher);
 	px_memset(cx, 0, sizeof(*cx));
-	px_free(cx);
+	pfree(cx);
 }
 
 /* PARSER */
@@ -408,17 +407,14 @@ px_find_combo(const char *name, PX_Combo **res)
 
 	PX_Combo   *cx;
 
-	cx = px_alloc(sizeof(*cx));
-	memset(cx, 0, sizeof(*cx));
-
-	buf = px_alloc(strlen(name) + 1);
-	strcpy(buf, name);
+	cx = palloc0(sizeof(*cx));
+	buf = pstrdup(name);
 
 	err = parse_cipher_name(buf, &s_cipher, &s_pad);
 	if (err)
 	{
-		px_free(buf);
-		px_free(cx);
+		pfree(buf);
+		pfree(cx);
 		return err;
 	}
 
@@ -445,7 +441,7 @@ px_find_combo(const char *name, PX_Combo **res)
 	cx->decrypt_len = combo_decrypt_len;
 	cx->free = combo_free;
 
-	px_free(buf);
+	pfree(buf);
 
 	*res = cx;
 
@@ -454,7 +450,7 @@ px_find_combo(const char *name, PX_Combo **res)
 err1:
 	if (cx->cipher)
 		px_cipher_free(cx->cipher);
-	px_free(cx);
-	px_free(buf);
+	pfree(cx);
+	pfree(buf);
 	return PXE_NO_CIPHER;
 }
diff --git a/contrib/pgcrypto/px.h b/contrib/pgcrypto/px.h
index 0d4722a04a..5487923edb 100644
--- a/contrib/pgcrypto/px.h
+++ b/contrib/pgcrypto/px.h
@@ -37,19 +37,6 @@
 /* keep debug messages? */
 #define PX_DEBUG
 
-/* a way to disable palloc
- * - useful if compiled into standalone
- */
-#ifndef PX_OWN_ALLOC
-#define px_alloc(s) palloc(s)
-#define px_realloc(p, s) repalloc(p, s)
-#define px_free(p)	pfree(p)
-#else
-void	   *px_alloc(size_t s);
-void	   *px_realloc(void *p, size_t s);
-void		px_free(void *p);
-#endif
-
 /* max salt returned */
 #define PX_MAX_SALT_LEN		128
 
-- 
2.21.1 (Apple Git-122.3)

