xmlIO: Handle error returns from dup()
If dup() fails and returns -1, gzdopen() will transparently return NULL,
but then close() will fail after being called on an invalid FD. Change
the code to only call close() on valid FDs.
Coverity issue: #72382
diff --git a/xmlIO.c b/xmlIO.c
index c8258e5..0e1092d 100644
--- a/xmlIO.c
+++ b/xmlIO.c
@@ -1158,7 +1158,7 @@
if (!strcmp(filename, "-")) {
int duped_fd = dup(fileno(stdin));
fd = gzdopen(duped_fd, "rb");
- if (fd == Z_NULL) {
+ if (fd == Z_NULL && duped_fd >= 0) {
close(duped_fd); /* gzdOpen() does not close on failure */
}
@@ -1237,7 +1237,7 @@
if (!strcmp(filename, "-")) {
int duped_fd = dup(fileno(stdout));
fd = gzdopen(duped_fd, "rb");
- if (fd == Z_NULL) {
+ if (fd == Z_NULL && duped_fd >= 0) {
close(duped_fd); /* gzdOpen() does not close on failure */
}