Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ca7f8e2

Browse files
committed
Remove custom memory allocation layer in pgcrypto
PX_OWN_ALLOC was intended as a way to disable the use of palloc(), and over the time new palloc() or equivalent calls have been added like in 32984d8, making this extra layer losing its original purpose. This simplifies on the way some code paths to use palloc0() rather than palloc() followed by memset(0). Author: Daniel Gustafsson Discussion: https://postgr.es/m/A5BFAA1A-B2E8-4CBC-895E-7B1B9475A527@yesql.se
1 parent a45bc8a commit ca7f8e2

17 files changed

+106
-143
lines changed

contrib/pgcrypto/imath.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ mp_int_init(mp_int z)
478478
mp_int
479479
mp_int_alloc(void)
480480
{
481-
mp_int out = px_alloc(sizeof(mpz_t));
481+
mp_int out = palloc(sizeof(mpz_t));
482482

483483
if (out != NULL)
484484
mp_int_init(out);
@@ -604,7 +604,7 @@ mp_int_free(mp_int z)
604604
assert(z != NULL);
605605

606606
mp_int_clear(z);
607-
px_free(z); /* note: NOT s_free() */
607+
pfree(z); /* note: NOT s_free() */
608608
}
609609

610610
mp_result
@@ -2212,7 +2212,7 @@ static const mp_digit fill = (mp_digit) 0xdeadbeefabad1dea;
22122212
static mp_digit *
22132213
s_alloc(mp_size num)
22142214
{
2215-
mp_digit *out = px_alloc(num * sizeof(mp_digit));
2215+
mp_digit *out = palloc(num * sizeof(mp_digit));
22162216

22172217
assert(out != NULL);
22182218

@@ -2235,7 +2235,7 @@ s_realloc(mp_digit *old, mp_size osize, mp_size nsize)
22352235
new[ix] = fill;
22362236
memcpy(new, old, osize * sizeof(mp_digit));
22372237
#else
2238-
mp_digit *new = px_realloc(old, nsize * sizeof(mp_digit));
2238+
mp_digit *new = repalloc(old, nsize * sizeof(mp_digit));
22392239

22402240
assert(new != NULL);
22412241
#endif
@@ -2246,7 +2246,7 @@ s_realloc(mp_digit *old, mp_size osize, mp_size nsize)
22462246
static void
22472247
s_free(void *ptr)
22482248
{
2249-
px_free(ptr);
2249+
pfree(ptr);
22502250
}
22512251

22522252
static bool

contrib/pgcrypto/internal-sha2.c

+12-16
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ int_sha224_free(PX_MD *h)
8585
pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
8686

8787
px_memset(ctx, 0, sizeof(*ctx));
88-
px_free(ctx);
89-
px_free(h);
88+
pfree(ctx);
89+
pfree(h);
9090
}
9191

9292
/* SHA256 */
@@ -133,8 +133,8 @@ int_sha256_free(PX_MD *h)
133133
pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
134134

135135
px_memset(ctx, 0, sizeof(*ctx));
136-
px_free(ctx);
137-
px_free(h);
136+
pfree(ctx);
137+
pfree(h);
138138
}
139139

140140
/* SHA384 */
@@ -181,8 +181,8 @@ int_sha384_free(PX_MD *h)
181181
pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
182182

183183
px_memset(ctx, 0, sizeof(*ctx));
184-
px_free(ctx);
185-
px_free(h);
184+
pfree(ctx);
185+
pfree(h);
186186
}
187187

188188
/* SHA512 */
@@ -229,8 +229,8 @@ int_sha512_free(PX_MD *h)
229229
pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
230230

231231
px_memset(ctx, 0, sizeof(*ctx));
232-
px_free(ctx);
233-
px_free(h);
232+
pfree(ctx);
233+
pfree(h);
234234
}
235235

236236
/* init functions */
@@ -240,8 +240,7 @@ init_sha224(PX_MD *md)
240240
{
241241
pg_sha224_ctx *ctx;
242242

243-
ctx = px_alloc(sizeof(*ctx));
244-
memset(ctx, 0, sizeof(*ctx));
243+
ctx = palloc0(sizeof(*ctx));
245244

246245
md->p.ptr = ctx;
247246

@@ -260,8 +259,7 @@ init_sha256(PX_MD *md)
260259
{
261260
pg_sha256_ctx *ctx;
262261

263-
ctx = px_alloc(sizeof(*ctx));
264-
memset(ctx, 0, sizeof(*ctx));
262+
ctx = palloc0(sizeof(*ctx));
265263

266264
md->p.ptr = ctx;
267265

@@ -280,8 +278,7 @@ init_sha384(PX_MD *md)
280278
{
281279
pg_sha384_ctx *ctx;
282280

283-
ctx = px_alloc(sizeof(*ctx));
284-
memset(ctx, 0, sizeof(*ctx));
281+
ctx = palloc0(sizeof(*ctx));
285282

286283
md->p.ptr = ctx;
287284

@@ -300,8 +297,7 @@ init_sha512(PX_MD *md)
300297
{
301298
pg_sha512_ctx *ctx;
302299

303-
ctx = px_alloc(sizeof(*ctx));
304-
memset(ctx, 0, sizeof(*ctx));
300+
ctx = palloc0(sizeof(*ctx));
305301

306302
md->p.ptr = ctx;
307303

contrib/pgcrypto/internal.c

+13-19
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ int_md5_free(PX_MD *h)
123123
MD5_CTX *ctx = (MD5_CTX *) h->p.ptr;
124124

125125
px_memset(ctx, 0, sizeof(*ctx));
126-
px_free(ctx);
127-
px_free(h);
126+
pfree(ctx);
127+
pfree(h);
128128
}
129129

130130
/* SHA1 */
@@ -171,8 +171,8 @@ int_sha1_free(PX_MD *h)
171171
SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
172172

173173
px_memset(ctx, 0, sizeof(*ctx));
174-
px_free(ctx);
175-
px_free(h);
174+
pfree(ctx);
175+
pfree(h);
176176
}
177177

178178
/* init functions */
@@ -182,8 +182,7 @@ init_md5(PX_MD *md)
182182
{
183183
MD5_CTX *ctx;
184184

185-
ctx = px_alloc(sizeof(*ctx));
186-
memset(ctx, 0, sizeof(*ctx));
185+
ctx = palloc0(sizeof(*ctx));
187186

188187
md->p.ptr = ctx;
189188

@@ -202,8 +201,7 @@ init_sha1(PX_MD *md)
202201
{
203202
SHA1_CTX *ctx;
204203

205-
ctx = px_alloc(sizeof(*ctx));
206-
memset(ctx, 0, sizeof(*ctx));
204+
ctx = palloc0(sizeof(*ctx));
207205

208206
md->p.ptr = ctx;
209207

@@ -246,9 +244,9 @@ intctx_free(PX_Cipher *c)
246244
if (cx)
247245
{
248246
px_memset(cx, 0, sizeof *cx);
249-
px_free(cx);
247+
pfree(cx);
250248
}
251-
px_free(c);
249+
pfree(c);
252250
}
253251

254252
/*
@@ -373,8 +371,7 @@ rj_load(int mode)
373371
PX_Cipher *c;
374372
struct int_ctx *cx;
375373

376-
c = px_alloc(sizeof *c);
377-
memset(c, 0, sizeof *c);
374+
c = palloc0(sizeof *c);
378375

379376
c->block_size = rj_block_size;
380377
c->key_size = rj_key_size;
@@ -384,8 +381,7 @@ rj_load(int mode)
384381
c->decrypt = rj_decrypt;
385382
c->free = intctx_free;
386383

387-
cx = px_alloc(sizeof *cx);
388-
memset(cx, 0, sizeof *cx);
384+
cx = palloc0(sizeof *cx);
389385
cx->mode = mode;
390386

391387
c->ptr = cx;
@@ -482,8 +478,7 @@ bf_load(int mode)
482478
PX_Cipher *c;
483479
struct int_ctx *cx;
484480

485-
c = px_alloc(sizeof *c);
486-
memset(c, 0, sizeof *c);
481+
c = palloc0(sizeof *c);
487482

488483
c->block_size = bf_block_size;
489484
c->key_size = bf_key_size;
@@ -493,8 +488,7 @@ bf_load(int mode)
493488
c->decrypt = bf_decrypt;
494489
c->free = intctx_free;
495490

496-
cx = px_alloc(sizeof *cx);
497-
memset(cx, 0, sizeof *cx);
491+
cx = palloc0(sizeof *cx);
498492
cx->mode = mode;
499493
c->ptr = cx;
500494
return c;
@@ -564,7 +558,7 @@ px_find_digest(const char *name, PX_MD **res)
564558
for (p = int_digest_list; p->name; p++)
565559
if (pg_strcasecmp(p->name, name) == 0)
566560
{
567-
h = px_alloc(sizeof(*h));
561+
h = palloc(sizeof(*h));
568562
p->init(h);
569563

570564
*res = h;

contrib/pgcrypto/mbuf.c

+14-16
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ mbuf_free(MBuf *mbuf)
7070
if (mbuf->own_data)
7171
{
7272
px_memset(mbuf->data, 0, mbuf->buf_end - mbuf->data);
73-
px_free(mbuf->data);
73+
pfree(mbuf->data);
7474
}
75-
px_free(mbuf);
75+
pfree(mbuf);
7676
return 0;
7777
}
7878

@@ -88,7 +88,7 @@ prepare_room(MBuf *mbuf, int block_len)
8888
newlen = (mbuf->buf_end - mbuf->data)
8989
+ ((block_len + STEP + STEP - 1) & -STEP);
9090

91-
newbuf = px_realloc(mbuf->data, newlen);
91+
newbuf = repalloc(mbuf->data, newlen);
9292

9393
mbuf->buf_end = newbuf + newlen;
9494
mbuf->data_end = newbuf + (mbuf->data_end - mbuf->data);
@@ -121,8 +121,8 @@ mbuf_create(int len)
121121
if (!len)
122122
len = 8192;
123123

124-
mbuf = px_alloc(sizeof *mbuf);
125-
mbuf->data = px_alloc(len);
124+
mbuf = palloc(sizeof *mbuf);
125+
mbuf->data = palloc(len);
126126
mbuf->buf_end = mbuf->data + len;
127127
mbuf->data_end = mbuf->data;
128128
mbuf->read_pos = mbuf->data;
@@ -138,7 +138,7 @@ mbuf_create_from_data(uint8 *data, int len)
138138
{
139139
MBuf *mbuf;
140140

141-
mbuf = px_alloc(sizeof *mbuf);
141+
mbuf = palloc(sizeof *mbuf);
142142
mbuf->data = (uint8 *) data;
143143
mbuf->buf_end = mbuf->data + len;
144144
mbuf->data_end = mbuf->data + len;
@@ -219,15 +219,14 @@ pullf_create(PullFilter **pf_p, const PullFilterOps *op, void *init_arg, PullFil
219219
res = 0;
220220
}
221221

222-
pf = px_alloc(sizeof(*pf));
223-
memset(pf, 0, sizeof(*pf));
222+
pf = palloc0(sizeof(*pf));
224223
pf->buflen = res;
225224
pf->op = op;
226225
pf->priv = priv;
227226
pf->src = src;
228227
if (pf->buflen > 0)
229228
{
230-
pf->buf = px_alloc(pf->buflen);
229+
pf->buf = palloc(pf->buflen);
231230
pf->pos = 0;
232231
}
233232
else
@@ -248,11 +247,11 @@ pullf_free(PullFilter *pf)
248247
if (pf->buf)
249248
{
250249
px_memset(pf->buf, 0, pf->buflen);
251-
px_free(pf->buf);
250+
pfree(pf->buf);
252251
}
253252

254253
px_memset(pf, 0, sizeof(*pf));
255-
px_free(pf);
254+
pfree(pf);
256255
}
257256

258257
/* 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
386385
res = 0;
387386
}
388387

389-
mp = px_alloc(sizeof(*mp));
390-
memset(mp, 0, sizeof(*mp));
388+
mp = palloc0(sizeof(*mp));
391389
mp->block_size = res;
392390
mp->op = op;
393391
mp->priv = priv;
394392
mp->next = next;
395393
if (mp->block_size > 0)
396394
{
397-
mp->buf = px_alloc(mp->block_size);
395+
mp->buf = palloc(mp->block_size);
398396
mp->pos = 0;
399397
}
400398
else
@@ -415,11 +413,11 @@ pushf_free(PushFilter *mp)
415413
if (mp->buf)
416414
{
417415
px_memset(mp->buf, 0, mp->block_size);
418-
px_free(mp->buf);
416+
pfree(mp->buf);
419417
}
420418

421419
px_memset(mp, 0, sizeof(*mp));
422-
px_free(mp);
420+
pfree(mp);
423421
}
424422

425423
void

contrib/pgcrypto/openssl.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ digest_free(PX_MD *h)
156156
OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
157157

158158
free_openssl_digest(digest);
159-
px_free(h);
159+
pfree(h);
160160
}
161161

162162
static int px_openssl_initialized = 0;
@@ -214,7 +214,7 @@ px_find_digest(const char *name, PX_MD **res)
214214
open_digests = digest;
215215

216216
/* The PX_MD object is allocated in the current memory context. */
217-
h = px_alloc(sizeof(*h));
217+
h = palloc(sizeof(*h));
218218
h->result_size = digest_result_size;
219219
h->block_size = digest_block_size;
220220
h->reset = digest_reset;
@@ -353,7 +353,7 @@ gen_ossl_free(PX_Cipher *c)
353353
OSSLCipher *od = (OSSLCipher *) c->ptr;
354354

355355
free_openssl_cipher(od);
356-
px_free(c);
356+
pfree(c);
357357
}
358358

359359
static int
@@ -790,7 +790,7 @@ px_find_cipher(const char *name, PX_Cipher **res)
790790
od->evp_ciph = i->ciph->cipher_func();
791791

792792
/* The PX_Cipher is allocated in current memory context */
793-
c = px_alloc(sizeof(*c));
793+
c = palloc(sizeof(*c));
794794
c->block_size = gen_ossl_block_size;
795795
c->key_size = gen_ossl_key_size;
796796
c->iv_size = gen_ossl_iv_size;

contrib/pgcrypto/pgp-cfb.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len,
6767
return res;
6868
}
6969

70-
ctx = px_alloc(sizeof(*ctx));
71-
memset(ctx, 0, sizeof(*ctx));
70+
ctx = palloc0(sizeof(*ctx));
7271
ctx->ciph = ciph;
7372
ctx->block_size = px_cipher_block_size(ciph);
7473
ctx->resync = resync;
@@ -85,7 +84,7 @@ pgp_cfb_free(PGP_CFB *ctx)
8584
{
8685
px_cipher_free(ctx->ciph);
8786
px_memset(ctx, 0, sizeof(*ctx));
88-
px_free(ctx);
87+
pfree(ctx);
8988
}
9089

9190
/*

0 commit comments

Comments
 (0)