[libxml2] xmlIO: Fix an FD leak on gzdopen() failure



commit 31aa38158a0ec6075749838639775f3c01e01f6c
Author: Philip Withnall <philip withnall collabora co uk>
Date:   Fri Jun 20 21:11:40 2014 +0100

    xmlIO: Fix an FD leak on gzdopen() failure
    
    According to the documentation, gzdopen() does not close the FD on
    failure (but does effectively close it on success, since gzclose()
    closes it).
    
    Coverity issues: #60440, #60441
    
    https://bugzilla.gnome.org/show_bug.cgi?id=731990

 xmlIO.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
---
diff --git a/xmlIO.c b/xmlIO.c
index d1544f3..5baeba3 100644
--- a/xmlIO.c
+++ b/xmlIO.c
@@ -1159,7 +1159,12 @@ xmlGzfileOpen_real (const char *filename) {
     gzFile fd;
 
     if (!strcmp(filename, "-")) {
-        fd = gzdopen(dup(fileno(stdin)), "rb");
+        int duped_fd = dup(fileno(stdin));
+        fd = gzdopen(duped_fd, "rb");
+        if (fd == Z_NULL) {
+            close(duped_fd);  /* gzdOpen() does not close on failure */
+        }
+
        return((void *) fd);
     }
 
@@ -1233,7 +1238,12 @@ xmlGzfileOpenW (const char *filename, int compression) {
 
     snprintf(mode, sizeof(mode), "wb%d", compression);
     if (!strcmp(filename, "-")) {
-        fd = gzdopen(dup(fileno(stdout)), mode);
+        int duped_fd = dup(fileno(stdout));
+        fd = gzdopen(duped_fd, "rb");
+        if (fd == Z_NULL) {
+            close(duped_fd);  /* gzdOpen() does not close on failure */
+        }
+
        return((void *) fd);
     }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]