[PATCH] default environment: make string arguments const

Sascha Hauer s.hauer at pengutronix.de
Mon Feb 19 02:55:07 PST 2024


Change default_environment_path_set() to take a const char * and let
default_environment_path_get() return a const char *.
Also, do not keep a copy of the string passed to
default_environment_path_set() rather than the original string to make
it more clear where the path is allocated. This allows us to free the
string passed to default_environment_path_set() after usage by some
callers.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 arch/arm/mach-omap/omap_generic.c |  1 +
 commands/loadenv.c                |  3 ++-
 common/efi/payload/init.c         |  1 +
 common/environment.c              | 13 +++++++++----
 include/envfs.h                   |  8 ++++----
 5 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/arch/arm/mach-omap/omap_generic.c b/arch/arm/mach-omap/omap_generic.c
index 36f2e80af2..99e14fb540 100644
--- a/arch/arm/mach-omap/omap_generic.c
+++ b/arch/arm/mach-omap/omap_generic.c
@@ -180,6 +180,7 @@ static int omap_env_init(void)
 								partname);
 	default_environment_path_set(envpath);
 
+	free(envpath);
 out:
 	free(partname);
 
diff --git a/commands/loadenv.c b/commands/loadenv.c
index 279ee52da5..ddbf66b764 100644
--- a/commands/loadenv.c
+++ b/commands/loadenv.c
@@ -18,7 +18,8 @@
 
 static int do_loadenv(int argc, char *argv[])
 {
-	char *filename = NULL, *dirname;
+	const char *filename = NULL;
+	char *dirname;
 	unsigned flags = 0;
 	int opt, ret;
 	int scrub = 0;
diff --git a/common/efi/payload/init.c b/common/efi/payload/init.c
index cfacdffa67..e2f763853f 100644
--- a/common/efi/payload/init.c
+++ b/common/efi/payload/init.c
@@ -258,6 +258,7 @@ static int efi_init(void)
 
 	env = xasprintf("/efivars/barebox-env-%pUl", &efi_barebox_vendor_guid);
 	default_environment_path_set(env);
+	free(env);
 
 	return 0;
 }
diff --git a/common/environment.c b/common/environment.c
index e8c487c1a2..a7432a84f3 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -48,15 +48,20 @@ struct action_data {
 
 #define TMPDIR "/.defaultenv"
 
-static char *default_environment_path = "/dev/env0";
+static char *default_environment_path;
 
-void default_environment_path_set(char *path)
+void default_environment_path_set(const char *path)
 {
-	default_environment_path = path;
+	free(default_environment_path);
+
+	default_environment_path = xstrdup(path);
 }
 
-char *default_environment_path_get(void)
+const char *default_environment_path_get(void)
 {
+	if (!default_environment_path)
+		default_environment_path = xstrdup("/dev/env0");
+
 	return default_environment_path;
 }
 
diff --git a/include/envfs.h b/include/envfs.h
index abe0ffb963..767b34c943 100644
--- a/include/envfs.h
+++ b/include/envfs.h
@@ -105,14 +105,14 @@ int envfs_load_from_buf(void *buf, int len, const char *dir, unsigned flags);
 
 /* defaults to /dev/env0 */
 #ifdef CONFIG_ENV_HANDLING
-void default_environment_path_set(char *path);
-char *default_environment_path_get(void);
+void default_environment_path_set(const char *path);
+const char *default_environment_path_get(void);
 #else
-static inline void default_environment_path_set(char *path)
+static inline void default_environment_path_set(const char *path)
 {
 }
 
-static inline char *default_environment_path_get(void)
+static inline const char *default_environment_path_get(void)
 {
 	return NULL;
 }
-- 
2.39.2




More information about the barebox mailing list