Ticket #4490: mc-4490-background.c-leak--file-descriptor-fix.patch

File mc-4490-background.c-leak--file-descriptor-fix.patch, 7.0 KB (added by and, 14 months ago)
  • src/background.c

    From f5f2829baece22edcaeb363071e0b1bd793d2af5 Mon Sep 17 00:00:00 2001
    From: Andreas Mohr <and@gmx.li>
    Date: Thu, 28 Sep 2023 11:48:41 +0000
    Subject: [PATCH] (background.c) fix leak of file descriptor
    
    background.c: In function 'do_background':
    background.c:587:1: error: leak of file descriptor 'comm[0]' []8;;https://cwe.mitre.org/data/definitions/775.htmlCWE-775]8;;] []8;;https://gcc.gnu.org/onlinedocs/gcc/Static-Analyzer-Options.html#index-Wanalyzer-fd-leak-Werror=analyzer-fd-leak]8;;]
      587 | }
          | ^
      'do_background': events 1-12
        |
        |  527 |     if (pipe (comm) == -1)
        |      |        ~^~~~~~~~~~~
        |      |        ||
        |      |        |(1) opened here as read-write
        |      |        |(2) when 'pipe' succeeds
        |      |        (3) following 'false' branch...
        |......
        |  530 |     if (pipe (back_comm) == -1)
        |      |     ~~ ~~~~~~~~~~~~~~~~~
        |      |     |  ||
        |      |     |  |(5) when 'pipe' succeeds
        |      |     |  (6) following 'false' branch...
        |      |     (4) ...to here
        |......
        |  538 |     pid = fork ();
        |      |     ~~~  
        |      |     |
        |      |     (7) ...to here
        |  539 |     if (pid == -1)
        |      |        ~ 
        |      |        |
        |      |        (8) following 'false' branch (when 'pid != -1')...
        |......
        |  552 |     if (pid == 0)
        |      |     ~~ ~ 
        |      |     |  |
        |      |     |  (10) following 'true' branch (when 'pid == 0')...
        |      |     (9) ...to here
        |  553 |     {
        |  554 |         int nullfd;
        |      |         ~~~
        |      |         |
        |      |         (11) ...to here
        |......
        |  587 | }
        |      | ~        
        |      | |
        |      | (12) 'comm[0]' leaks here
        |
    background.c:587:1: error: leak of file descriptor 'back_comm[1]' []8;;https://cwe.mitre.org/data/definitions/775.htmlCWE-775]8;;] []8;;https://gcc.gnu.org/onlinedocs/gcc/Static-Analyzer-Options.html#index-Wanalyzer-fd-leak-Werror=analyzer-fd-leak]8;;]
      587 | }
          | ^
      'do_background': events 1-12
        |
        |  527 |     if (pipe (comm) == -1)
        |      |        ~^~~~~~~~~~~
        |      |        ||
        |      |        |(1) when 'pipe' succeeds
        |      |        (2) following 'false' branch...
        |......
        |  530 |     if (pipe (back_comm) == -1)
        |      |     ~~ ~~~~~~~~~~~~~~~~~
        |      |     |  ||
        |      |     |  |(4) opened here as read-write
        |      |     |  |(5) when 'pipe' succeeds
        |      |     |  (6) following 'false' branch...
        |      |     (3) ...to here
        |......
        |  538 |     pid = fork ();
        |      |     ~~~  
        |      |     |
        |      |     (7) ...to here
        |  539 |     if (pid == -1)
        |      |        ~ 
        |      |        |
        |      |        (8) following 'false' branch (when 'pid != -1')...
        |......
        |  552 |     if (pid == 0)
        |      |     ~~ ~ 
        |      |     |  |
        |      |     |  (10) following 'true' branch (when 'pid == 0')...
        |      |     (9) ...to here
        |  553 |     {
        |  554 |         int nullfd;
        |      |         ~~~
        |      |         |
        |      |         (11) ...to here
        |......
        |  587 | }
        |      | ~        
        |      | |
        |      | (12) 'back_comm[1]' leaks here
        |
    background.c:587:1: error: leak of file descriptor 'comm[1]' []8;;https://cwe.mitre.org/data/definitions/775.htmlCWE-775]8;;] []8;;https://gcc.gnu.org/onlinedocs/gcc/Static-Analyzer-Options.html#index-Wanalyzer-fd-leak-Werror=analyzer-fd-leak]8;;]
      587 | }
          | ^
      'do_background': events 1-12
        |
        |  527 |     if (pipe (comm) == -1)
        |      |        ~^~~~~~~~~~~
        |      |        ||
        |      |        |(1) opened here as read-write
        |      |        |(2) when 'pipe' succeeds
        |      |        (3) following 'false' branch...
        |......
        |  530 |     if (pipe (back_comm) == -1)
        |      |     ~~ ~~~~~~~~~~~~~~~~~
        |      |     |  ||
        |      |     |  |(5) when 'pipe' succeeds
        |      |     |  (6) following 'false' branch...
        |      |     (4) ...to here
        |......
        |  538 |     pid = fork ();
        |      |     ~~~  
        |      |     |
        |      |     (7) ...to here
        |  539 |     if (pid == -1)
        |      |        ~ 
        |      |        |
        |      |        (8) following 'false' branch (when 'pid != -1')...
        |......
        |  552 |     if (pid == 0)
        |      |     ~~ ~ 
        |      |     |  |
        |      |     |  (10) following 'false' branch (when 'pid != 0')...
        |      |     (9) ...to here
        |......
        |  583 |         ctx->pid = pid;
        |      |         ~~~
        |      |         |
        |      |         (11) ...to here
        |......
        |  587 | }
        |      | ~        
        |      | |
        |      | (12) 'comm[1]' leaks here
        |
    background.c:587:1: error: leak of file descriptor 'back_comm[0]' []8;;https://cwe.mitre.org/data/definitions/775.htmlCWE-775]8;;] []8;;https://gcc.gnu.org/onlinedocs/gcc/Static-Analyzer-Options.html#index-Wanalyzer-fd-leak-Werror=analyzer-fd-leak]8;;]
      587 | }
          | ^
      'do_background': events 1-12
        |
        |  527 |     if (pipe (comm) == -1)
        |      |        ~^~~~~~~~~~~
        |      |        ||
        |      |        |(1) when 'pipe' succeeds
        |      |        (2) following 'false' branch...
        |......
        |  530 |     if (pipe (back_comm) == -1)
        |      |     ~~ ~~~~~~~~~~~~~~~~~
        |      |     |  ||
        |      |     |  |(4) opened here as read-write
        |      |     |  |(5) when 'pipe' succeeds
        |      |     |  (6) following 'false' branch...
        |      |     (3) ...to here
        |......
        |  538 |     pid = fork ();
        |      |     ~~~  
        |      |     |
        |      |     (7) ...to here
        |  539 |     if (pid == -1)
        |      |        ~ 
        |      |        |
        |      |        (8) following 'false' branch (when 'pid != -1')...
        |......
        |  552 |     if (pid == 0)
        |      |     ~~ ~ 
        |      |     |  |
        |      |     |  (10) following 'false' branch (when 'pid != 0')...
        |      |     (9) ...to here
        |......
        |  583 |         ctx->pid = pid;
        |      |         ~~~
        |      |         |
        |      |         (11) ...to here
        |......
        |  587 | }
        |      | ~        
        |      | |
        |      | (12) 'back_comm[0]' leaks here
        |
    
    Found by gcc-13 (-fanalyzer-fd-leak)
    
    Signed-off-by: Andreas Mohr <and@gmx.li>
    ---
     src/background.c | 5 +++++
     1 file changed, 5 insertions(+)
    
    diff --git a/src/background.c b/src/background.c
    index 41a7f40d4..ea41bc9ed 100644
    a b do_background (file_op_context_t * ctx, char *info) 
    553553    { 
    554554        int nullfd; 
    555555 
     556        (void) close (comm[0]); 
    556557        parent_fd = comm[1]; 
     558 
     559        (void) close (back_comm[1]); 
    557560        from_parent_fd = back_comm[0]; 
    558561 
    559562        mc_global.we_are_background = TRUE; 
    do_background (file_op_context_t * ctx, char *info) 
    580583    } 
    581584    else 
    582585    { 
     586        (void) close (comm[1]); 
     587        (void) close (back_comm[0]); 
    583588        ctx->pid = pid; 
    584589        register_task_running (ctx, pid, comm[0], back_comm[1], info); 
    585590        return 1;