[prev in list] [next in list] [prev in thread] [next in thread] 

List:       postgresql-hackers
Subject:    Abort if dup fail (src/bin/pg_dump/compress_none.c)
From:       Ranier Vilela <ranier.vf () gmail ! com>
Date:       2024-01-31 17:12:00
Message-ID: CAEudQAr9fu0W9ULyT_+08v4BAn2bMU=D-VON=OwvciVRy5N_4A () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hi.

Per Coverity.
CID 1506240: (#1 of 1): Argument cannot be negative (NEGATIVE_RETURNS)
3. negative_returns: dup(fd) is passed to a parameter that cannot be
negative.

pg_dump function open_none, tries to associate a stream to a file
descriptor,
using function dup, which may fail and return negative value.

fdopen cannot receive negative parameters, in this case fail and return
EBADF.

This can be confusing for the user, who will be trying to figure out what's
wrong.
Better abort and report the correct failure to the user.

Patch attached.

Best regards,
Ranier Vilela

[Attachment #5 (text/html)]

<div dir="ltr"><div>Hi.</div><div><br></div><div>Per Coverity.</div><div>
<span><div id="gmail-defect-event-34975202-2" class="event-header gmail-with-event \
gmail-main-event gmail-selected-event gmail-code-event">  <div>

<span class="gmail-defect-text">CID 1506240: (#1 of 1): Argument cannot be negative \
(NEGATIVE_RETURNS) </span></div><span class="event-order">3.</span>
    <span class="event-tag">negative_returns:</span>
    <span class="gmail-description">dup(fd) is passed to a parameter that cannot be \
negative.</span>  
</div></span>

</div><div><br></div><div>pg_dump function open_none, tries to associate a stream to \
a file descriptor,</div><div>using function dup, which may fail and return negative \
value.</div><div><br></div><div>fdopen cannot receive negative parameters, in this \
case fail and return EBADF.</div><div><br></div><div>This can be confusing for the \
user, who will be trying to figure out what&#39;s wrong.</div><div>Better abort and \
report the correct failure to the user.</div><div><br></div><div>Patch \
attached.</div><div><br></div><div>Best regards,</div><div>Ranier \
Vilela<br></div><div><br></div><div><br></div></div>

--000000000000c14f93061040fa74--


["abort-if-dup-fail-pg_dump.patch" (application/octet-stream)]

diff --git a/src/bin/pg_dump/compress_none.c b/src/bin/pg_dump/compress_none.c
index 06c400424a..1f71e47a1c 100644
--- a/src/bin/pg_dump/compress_none.c
+++ b/src/bin/pg_dump/compress_none.c
@@ -171,7 +171,15 @@ open_none(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
 	Assert(CFH->private_data == NULL);
 
 	if (fd >= 0)
-		CFH->private_data = fdopen(dup(fd), mode);
+	{
+		int	thisfd;
+
+		thisfd = dup(fd);
+		if (thisfd < 0)		
+			pg_fatal("could not duplicate file: %m");
+
+		CFH->private_data = fdopen(thisfd, mode);
+	}
 	else
 		CFH->private_data = fopen(path, mode);

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic