From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] default environment: make string arguments const
Date: Mon, 19 Feb 2024 11:55:07 +0100 [thread overview]
Message-ID: <20240219105507.1618465-1-s.hauer@pengutronix.de> (raw)
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
next reply other threads:[~2024-02-19 10:55 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-19 10:55 Sascha Hauer [this message]
2024-02-20 10:51 ` Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240219105507.1618465-1-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox