pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid leaking duplicated file descriptors in corner cases.
Date: Thu, 19 Mar 2026 18:25:34 +0000
Message-ID: <E1w3I3q-000WyH-0M@gemulon.postgresql.org> (raw)

Avoid leaking duplicated file descriptors in corner cases.

pg_dump's compression modules had variations on the theme of

        fp = fdopen(dup(fd), mode);
        if (fp == NULL)
            // fail, reporting errno

which is problematic for two reasons.  First, if dup() succeeds but
fdopen() fails, we'd leak the duplicated FD.  That's not important
at present since the program will just exit immediately after failure
anyway; but perhaps someday we'll try to continue, making the resource
leak potentially significant.  Second, if dup() fails then fdopen()
will overwrite the useful errno (perhaps EMFILE) with a misleading
value EBADF, making it difficult to understand what went wrong.
Fix both issues by testing for dup() failure before proceeding to
the next call.

These failures are sufficiently unlikely, and the consequences minor
enough, that this doesn't seem worth the effort to back-patch.
But let's fix it in HEAD.

Author: Jianghua Yang <yjhjstz@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/62bbe34d-2315-4b42-b768-56d901aa83e1@gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/8b02c22bb43cb480f437704dc547ea77196b7e93

Modified Files
--------------
src/bin/pg_dump/compress_gzip.c | 20 ++++++++++++++++----
src/bin/pg_dump/compress_lz4.c  | 27 ++++++++++++++++++++++-----
src/bin/pg_dump/compress_none.c | 20 ++++++++++++++++----
src/bin/pg_dump/compress_zstd.c | 28 ++++++++++++++++++++++------
4 files changed, 76 insertions(+), 19 deletions(-)



Message-ID: <E1w3I3q-000WyH-0M@gemulon.postgresql.org>
Permalink:  ../E1w3I3q-000WyH-0M@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1w3I3q-000WyH-0M@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Avoid leaking duplicated file descriptors in corner cases.
  In-Reply-To: <E1w3I3q-000WyH-0M@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox