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

File mc-4103-cid-cons.handler.c-fix.resource-leak.patch, 3.5 KB (added by and, 3 years ago)
  • src/cons.handler.c

    From 964412fc7557b844d63682db7f074fc28e02f2f4 Mon Sep 17 00:00:00 2001
    From: Andreas Mohr <and@gmx.li>
    Date: Tue, 8 Dec 2020 15:10:05 +0000
    Subject: [PATCH] (cons.handler.c) fix resource leak
    
    handle open failure properly
    
    Found by Coverity
    Coverity id #32608
    
    Signed-off-by: Andreas Mohr <and@gmx.li>
    ---
     src/cons.handler.c | 34 +++++++++++++++++++---------------
     1 file changed, 19 insertions(+), 15 deletions(-)
    
    diff --git a/src/cons.handler.c b/src/cons.handler.c
    index 79297c2a7..9aa24b187 100644
    a b handle_console_linux (console_action_t action) 
    142142    { 
    143143    case CONSOLE_INIT: 
    144144        /* Close old pipe ends in case it is the 2nd time we run cons.saver */ 
    145         status = close (pipefd1[1]); 
    146         status = close (pipefd2[0]); 
     145        close (pipefd1[1]); 
     146        close (pipefd2[0]); 
    147147        /* Create two pipes for communication */ 
    148148        if (!((pipe (pipefd1) == 0) && ((pipe (pipefd2)) == 0))) 
    149149        { 
    handle_console_linux (console_action_t action) 
    156156        { 
    157157            /* Cannot fork */ 
    158158            /* Delete pipes */ 
    159             status = close (pipefd1[1]); 
    160             status = close (pipefd1[0]); 
    161             status = close (pipefd2[1]); 
    162             status = close (pipefd2[0]); 
     159            close (pipefd1[1]); 
     160            close (pipefd1[0]); 
     161            close (pipefd2[1]); 
     162            close (pipefd2[0]); 
    163163            mc_global.tty.console_flag = '\0'; 
    164164        } 
    165165        else if (cons_saver_pid > 0) 
    166166        { 
    167167            /* Parent */ 
    168168            /* Close the extra pipe ends */ 
    169             status = close (pipefd1[0]); 
    170             status = close (pipefd2[1]); 
     169            close (pipefd1[0]); 
     170            close (pipefd2[1]); 
    171171            /* Was the child successful? */ 
    172172            status = read (pipefd2[0], &mc_global.tty.console_flag, 1); 
    173173            if (mc_global.tty.console_flag == '\0') 
    174174            { 
    175175                pid_t ret; 
    176                 status = close (pipefd1[1]); 
     176                close (pipefd1[1]); 
    177177                status = close (pipefd2[0]); 
    178178                ret = waitpid (cons_saver_pid, &status, 0); 
    179179                (void) ret; 
    handle_console_linux (console_action_t action) 
    185185            char *tty_name; 
    186186 
    187187            /* Close the extra pipe ends */ 
    188             status = close (pipefd1[1]); 
    189             status = close (pipefd2[0]); 
     188            close (pipefd1[1]); 
     189            close (pipefd2[0]); 
    190190            tty_name = ttyname (0); 
    191191            /* Bind the pipe 0 to the standard input */ 
    192192            do 
    193193            { 
    194194                if (dup2 (pipefd1[0], STDIN_FILENO) == -1) 
    195195                    break; 
    196                 status = close (pipefd1[0]); 
     196                close (pipefd1[0]); 
    197197                /* Bind the pipe 1 to the standard output */ 
    198198                if (dup2 (pipefd2[1], STDOUT_FILENO) == -1) 
    199199                    break; 
    200200 
    201                 status = close (pipefd2[1]); 
     201                close (pipefd2[1]); 
    202202                /* Bind standard error to /dev/null */ 
    203                 status = open ("/dev/null", O_WRONLY); 
     203                if ((status = open ("/dev/null", O_WRONLY)) == -1) 
     204                     break; 
    204205                if (dup2 (status, STDERR_FILENO) == -1) 
     206                { 
     207                    close (status); 
    205208                    break; 
    206                 status = close (status); 
     209                } 
     210                close (status); 
    207211                if (tty_name != NULL) 
    208212                { 
    209213                    char *mc_conssaver;