[PATCH v2 3/5] ratp: getenv: Do not crash on non-existing variables

Sascha Hauer s.hauer at pengutronix.de
Thu Sep 8 02:30:03 PDT 2022


getenv() returns NULL on non-existing variables. Replace result with
empty string to avoid doing strlen() on a NULL pointer.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 common/ratp/getenv.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/common/ratp/getenv.c b/common/ratp/getenv.c
index 7b38d2e363..9617ab1b43 100644
--- a/common/ratp/getenv.c
+++ b/common/ratp/getenv.c
@@ -32,6 +32,9 @@ static int ratp_cmd_getenv(const struct ratp_bb *req, int req_len,
 	value = getenv(varname);
 	free(varname);
 
+	if (!value)
+		value = "";
+
 	dlen = strlen(value);
 
 	*rsp_len = sizeof(struct ratp_bb) + dlen;
-- 
2.30.2




More information about the barebox mailing list