Potential memory leak in contrib/intarray's g_intbig_compress

From: Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Cc: Konstantin Knizhnik <knizhnik(at)garret(dot)ru>
Subject: Potential memory leak in contrib/intarray's g_intbig_compress
Date: 2023-07-13 12:02:36
Message-ID: CAEze2Wi86=DxErfvf+SCB2UKmU2amKOF60BKuJOX=w-RojRn0A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

My collegue Konstantin (cc-ed) noticed that the GiST code of intarray
may leak memory in certain index operations:

> g_intbig_compress(...):
> [...]
> ArrayType *in = DatumGetArrayTypeP(entry->key);
> [...]
> if (in != DatumGetArrayTypeP(entry->key))
> pfree(in);

DatumGetArrayTypeP will allocate a new, uncompressed copy if the given
Datum is compressed. So in this code, if entry->key is compressed we'd
allocate two decompressed copies, while only we only deallocate the
first of these two. I believe the attached patch fixes the issue.

It looks like this bug has existed since the code was first committed,
so backpatching would go back to 11 if this is an active issue.

Kind regards,

Matthias van de Meent

Attachment Content-Type Size
0001-Fix-memory-leak-in-g_intbig_compress.patch application/octet-stream 923 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Langote 2023-07-13 12:58:38 Re: generic plans and "initial" pruning
Previous Message Peter Eisentraut 2023-07-13 11:29:45 Re: Better help output for pgbench -I init_steps