[PATCH 13/32] commands: tutorial: fix memory leak

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Sep 5 02:55:38 PDT 2022


We don't need to duplicate argv[i], because it persists for the lieftime
of print_tutorial_step. We can thus drop the strdup and while at it,
just drop the redundant step variable altogether.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 commands/tutorial.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/commands/tutorial.c b/commands/tutorial.c
index 444177764311..afe5b66f0b0a 100644
--- a/commands/tutorial.c
+++ b/commands/tutorial.c
@@ -84,7 +84,6 @@ out:
 static int do_tutorial_next(int argc, char *argv[])
 {
 	int opt, i;
-	char *step = NULL;
 	char *oldcwd;
 	ssize_t ret = 0;
 	bool is_prev = *argv[0] == 'p';
@@ -121,14 +120,12 @@ static int do_tutorial_next(int argc, char *argv[])
 		next_step = next_step > 0 ? next_step - 1 : 0;
 
 	if (optind == argc) {
-		step = steps.gl_pathv[next_step];
-		ret = print_tutorial_step(step);
+		ret = print_tutorial_step(steps.gl_pathv[next_step]);
 		if (ret == 0 && !is_prev)
 			next_step = (next_step + 1) % steps.gl_pathc;
 	} else {
 		for (i = optind; i < argc; i++) {
-			step = strdup(argv[i]);
-			ret = print_tutorial_step(step);
+			ret = print_tutorial_step(argv[i]);
 			if (ret)
 				break;
 		}
-- 
2.30.2




More information about the barebox mailing list