Re: [PATCH] Fix fd leak in pg_dump compression backends when dup()+fdopen() fails

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Jianghua Yang <yjhjstz@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org
Date: 2026-03-19T17:08:15Z
Lists: pgsql-hackers
Jianghua Yang <yjhjstz@gmail.com> writes:
>    == The Bug ==

>    All four compression open functions use this pattern when an existing
>    file descriptor is passed in:

>        if (fd >= 0)
>            fp = fdopen(dup(fd), mode);   /* or gzdopen() */

>        if (fp == NULL)
>            return false;                 /* dup'd fd is leaked here */

>    The problem is that dup(fd) and fdopen()/gzdopen() are two separate
>    steps, and their failure modes must be handled independently:

Hmm.  You're right that we could leak the dup'd FD, but would it matter?
I'm pretty sure all these programs will just exit immediately on
failure.

I'm not averse to improving the code, but I'm not sure there is
a live bug worth back-patching.

			regards, tom lane



Commits

  1. Avoid leaking duplicated file descriptors in corner cases.

  2. Fix expanding 'bounds' in pg_trgm's calc_word_similarity() function

  3. Introduce a generic pg_dump compression API