Abort if dup fail (src/bin/pg_dump/compress_none.c)
Started by Ranier Vilelaalmost 2 years ago1 messages
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
Attachments:
abort-if-dup-fail-pg_dump.patchapplication/octet-stream; name=abort-if-dup-fail-pg_dump.patchDownload
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);