Ticket #4103: mc-4103-cid-utilunix.c-fix.resource-leak.patch

File mc-4103-cid-utilunix.c-fix.resource-leak.patch, 1.2 KB (added by and, 3 years ago)
  • lib/utilunix.c

    From d0986ff1042eab5b76da949b69bbd609f852c911 Mon Sep 17 00:00:00 2001
    From: Andreas Mohr <and@gmx.li>
    Date: Tue, 8 Dec 2020 19:04:15 +0000
    Subject: [PATCH] (utilunix.c) fix resource leak
    
    Save file handle for later close, if valid
    
    Found by Coverity
    Coverity id #32607
    
    Signed-off-by: Andreas Mohr <and@gmx.li>
    ---
     lib/utilunix.c | 6 +++++-
     1 file changed, 5 insertions(+), 1 deletion(-)
    
    diff --git a/lib/utilunix.c b/lib/utilunix.c
    index bbcf86b78..8488afd63 100644
    a b tilde_expand (const char *directory) 
    715715void 
    716716open_error_pipe (void) 
    717717{ 
     718    int error_fd = -1; 
     719 
    718720    if (pipe (error_pipe) < 0) 
    719721        message (D_NORMAL, _("Warning"), _("Pipe failed")); 
    720722 
    721723    old_error = dup (STDERR_FILENO); 
    722     if (old_error < 0 || close (STDERR_FILENO) != 0 || dup (error_pipe[1]) != STDERR_FILENO) 
     724    if (old_error < 0 || close (STDERR_FILENO) != 0 || (error_fd = dup (error_pipe[1])) != STDERR_FILENO) 
    723725    { 
    724726        message (D_NORMAL, _("Warning"), _("Dup failed")); 
    725727 
    open_error_pipe (void) 
    749751            } 
    750752        } 
    751753    } 
     754    if (error_fd >= 0) 
     755        close (error_fd); 
    752756    /* we never write there */ 
    753757    close (error_pipe[1]); 
    754758    error_pipe[1] = -1;