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) |
553 | 553 | { |
554 | 554 | int nullfd; |
555 | 555 | |
| 556 | (void) close (comm[0]); |
556 | 557 | parent_fd = comm[1]; |
| 558 | |
| 559 | (void) close (back_comm[1]); |
557 | 560 | from_parent_fd = back_comm[0]; |
558 | 561 | |
559 | 562 | mc_global.we_are_background = TRUE; |
… |
… |
do_background (file_op_context_t * ctx, char *info) |
580 | 583 | } |
581 | 584 | else |
582 | 585 | { |
| 586 | (void) close (comm[1]); |
| 587 | (void) close (back_comm[0]); |
583 | 588 | ctx->pid = pid; |
584 | 589 | register_task_running (ctx, pid, comm[0], back_comm[1], info); |
585 | 590 | return 1; |