* [PATCH] default environment: make string arguments const
@ 2024-02-19 10:55 Sascha Hauer
2024-02-20 10:51 ` Sascha Hauer
0 siblings, 1 reply; 2+ messages in thread
From: Sascha Hauer @ 2024-02-19 10:55 UTC (permalink / raw)
To: Barebox List
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@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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] default environment: make string arguments const
2024-02-19 10:55 [PATCH] default environment: make string arguments const Sascha Hauer
@ 2024-02-20 10:51 ` Sascha Hauer
0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2024-02-20 10:51 UTC (permalink / raw)
To: Barebox List, Sascha Hauer
On Mon, 19 Feb 2024 11:55:07 +0100, Sascha Hauer wrote:
> 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.
>
> [...]
Applied, thanks!
[1/1] default environment: make string arguments const
https://git.pengutronix.de/cgit/barebox/commit/?id=b4b4c2821454 (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-02-20 10:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-19 10:55 [PATCH] default environment: make string arguments const Sascha Hauer
2024-02-20 10:51 ` Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox