diff mbox series

[BlueZ,04/12] shared/shell: Fix fd leak if -s is passed multiple times

Message ID 20240704102617.1132337-5-hadess@hadess.net
State Superseded
Headers show
Series Fix a number of static analysis issues #5 | expand

Commit Message

Bastien Nocera July 4, 2024, 10:24 a.m. UTC
Error: RESOURCE_LEAK (CWE-772): [#def37] [important]
bluez-5.76/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.]
bluez-5.76/src/shared/shell.c:1305:5: var_assign: Assigning: "data.init_fd" = handle returned from "open(optarg, 0)".
bluez-5.76/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 | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/src/shared/shell.c b/src/shared/shell.c
index c09d41ee54df..d500dddf8acf 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -1306,11 +1306,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,
+				if (data.init_fd < 0)
+					printf("Unable to open %s: %s (%d)\n", optarg,
 						strerror(errno), errno);
+			}
 			break;
 		case 't':
 			if (optarg)