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) |
142 | 142 | { |
143 | 143 | case CONSOLE_INIT: |
144 | 144 | /* 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]); |
147 | 147 | /* Create two pipes for communication */ |
148 | 148 | if (!((pipe (pipefd1) == 0) && ((pipe (pipefd2)) == 0))) |
149 | 149 | { |
… |
… |
handle_console_linux (console_action_t action) |
156 | 156 | { |
157 | 157 | /* Cannot fork */ |
158 | 158 | /* 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]); |
163 | 163 | mc_global.tty.console_flag = '\0'; |
164 | 164 | } |
165 | 165 | else if (cons_saver_pid > 0) |
166 | 166 | { |
167 | 167 | /* Parent */ |
168 | 168 | /* Close the extra pipe ends */ |
169 | | status = close (pipefd1[0]); |
170 | | status = close (pipefd2[1]); |
| 169 | close (pipefd1[0]); |
| 170 | close (pipefd2[1]); |
171 | 171 | /* Was the child successful? */ |
172 | 172 | status = read (pipefd2[0], &mc_global.tty.console_flag, 1); |
173 | 173 | if (mc_global.tty.console_flag == '\0') |
174 | 174 | { |
175 | 175 | pid_t ret; |
176 | | status = close (pipefd1[1]); |
| 176 | close (pipefd1[1]); |
177 | 177 | status = close (pipefd2[0]); |
178 | 178 | ret = waitpid (cons_saver_pid, &status, 0); |
179 | 179 | (void) ret; |
… |
… |
handle_console_linux (console_action_t action) |
185 | 185 | char *tty_name; |
186 | 186 | |
187 | 187 | /* Close the extra pipe ends */ |
188 | | status = close (pipefd1[1]); |
189 | | status = close (pipefd2[0]); |
| 188 | close (pipefd1[1]); |
| 189 | close (pipefd2[0]); |
190 | 190 | tty_name = ttyname (0); |
191 | 191 | /* Bind the pipe 0 to the standard input */ |
192 | 192 | do |
193 | 193 | { |
194 | 194 | if (dup2 (pipefd1[0], STDIN_FILENO) == -1) |
195 | 195 | break; |
196 | | status = close (pipefd1[0]); |
| 196 | close (pipefd1[0]); |
197 | 197 | /* Bind the pipe 1 to the standard output */ |
198 | 198 | if (dup2 (pipefd2[1], STDOUT_FILENO) == -1) |
199 | 199 | break; |
200 | 200 | |
201 | | status = close (pipefd2[1]); |
| 201 | close (pipefd2[1]); |
202 | 202 | /* Bind standard error to /dev/null */ |
203 | | status = open ("/dev/null", O_WRONLY); |
| 203 | if ((status = open ("/dev/null", O_WRONLY)) == -1) |
| 204 | break; |
204 | 205 | if (dup2 (status, STDERR_FILENO) == -1) |
| 206 | { |
| 207 | close (status); |
205 | 208 | break; |
206 | | status = close (status); |
| 209 | } |
| 210 | close (status); |
207 | 211 | if (tty_name != NULL) |
208 | 212 | { |
209 | 213 | char *mc_conssaver; |