From 2433842ea33bcb80a9c157cbac472efedae8c8d4 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Fri, 5 Jul 2024 10:57:31 +0200 Subject: [PATCH] shared/shell: Fix fd leak if -s is passed multiple times Error: RESOURCE_LEAK (CWE-772): [#def37] [important] src/shared/shell.c:1305:5: open_fn: Returning handle opened by "open". [Note: The source code implementation of the function has been overridden by a user model.] src/shared/shell.c:1305:5: var_assign: Assigning: "data.init_fd" = handle returned from "open(optarg, 0)". src/shared/shell.c:1305:5: overwrite_var: Overwriting handle "data.init_fd" in "data.init_fd = open(optarg, 0)" leaks the handle. 1303| case 's': 1304| if (optarg) 1305|-> data.init_fd = open(optarg, O_RDONLY); 1306| if (data.init_fd < 0) 1307| printf("Unable to open %s: %s (%d)\n", optarg, --- src/shared/shell.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/shared/shell.c b/src/shared/shell.c index add4fa131..73caa77ce 100644 --- a/src/shared/shell.c +++ b/src/shared/shell.c @@ -1302,11 +1302,12 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt) data.mode = 1; goto done; case 's': - if (optarg) + if (optarg && data.init_fd < 0) { data.init_fd = open(optarg, O_RDONLY); - if (data.init_fd < 0) - printf("Unable to open %s: %s (%d)\n", optarg, - strerror(errno), errno); + if (data.init_fd < 0) + printf("Unable to open %s: %s (%d)\n", + optarg, strerror(errno), errno); + } break; case 't': if (optarg) -- 2.47.3