2
0
Fork 0

libc: always set argv and envp to valid addresses

This commit is contained in:
Emery Hemingway 2020-11-23 01:21:38 +01:00
parent 40f86fadb1
commit 1348a45b8e
2 changed files with 76 additions and 0 deletions

View File

@ -151,6 +151,7 @@ in {
posix = {
buildInputs = [ self.libc ];
portInputs = with ports; [ ports.libc ];
patches = [ ./patches/libc.patch ];
};
ps2_drv = { };
qt5_base = { };

View File

@ -85,3 +85,78 @@ index 150640ddf3..e511897600 100644
DUMMY(int , -1, getpriority, (int, int))
DUMMY(int , -1, getrusage, (int, rusage *))
DUMMY_SILENT(uid_t , 0, getuid, (void))
From 01bac4b0add9da8bbb4fcbe339605533ab7e7be5 Mon Sep 17 00:00:00 2001
From: Emery Hemingway <ehmry@posteo.net>
Date: Thu, 26 Nov 2020 12:47:30 +0100
Subject: [PATCH] libc: always set argv and envp to valid arrays
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The argument array passed to main(…) must be a null terminated array, even
if argc is zero. Set at least one environment variable in any case.
Fix #3955
---
repos/libports/include/libc/args.h | 29 +++++++++++++++++++++++++----
1 file changed, 25 insertions(+), 4 deletions(-)
diff --git a/repos/libports/include/libc/args.h b/repos/libports/include/libc/args.h
index 76645d82cb..fab299b8d3 100644
--- a/repos/libports/include/libc/args.h
+++ b/repos/libports/include/libc/args.h
@@ -25,6 +25,7 @@ static void populate_args_and_env(Libc::Env &env, int &argc, char **&argv, char
{
using Genode::Xml_node;
using Genode::Xml_attribute;
+ using Genode::max;
env.config([&] (Xml_node const &node) {
@@ -40,12 +41,25 @@ static void populate_args_and_env(Libc::Env &env, int &argc, char **&argv, char
++envc;
});
- if (argc == 0 && envc == 0)
+ if (argc == 0 && envc == 0) {
+ /*
+ * If argc is zero then argv is still a NULL-terminated array.
+ *
+ * Invent an enviromental variable in case the application
+ * does not handle an empty environment. SHLVL=0 is
+ * just an indication that this application lacks a parent
+ * shell.
+ */
+ static char const *args[] = { nullptr, "SHLVL=0", nullptr };
+ argc = 0;
+ argv = (char**)&args;
+ envp = &argv[1];
return; /* from lambda */
+ }
- /* arguments and environment are a contiguous array (but don't count on it) */
- argv = (char**)malloc((argc + envc + 1) * sizeof(char*));
- envp = &argv[argc];
+ /* arguments and environment are arranged System V style (but don't count on it) */
+ argv = (char**)malloc((argc + max(envc, 1) + 2) * sizeof(char*));
+ envp = &argv[argc+1];
/* read the arguments */
int arg_i = 0;
@@ -123,6 +137,13 @@ static void populate_args_and_env(Libc::Env &env, int &argc, char **&argv, char
catch (Xml_node::Nonexistent_attribute) { }
});
+ /* argv and envp are both NULL terminated */
+
+ argv[arg_i] = NULL;
+
+ /* invent an env-var if none are present, see note above */
+ if (env_i == 0)
+ envp[env_i++] = (char*)"SHLVL=0";
envp[env_i] = NULL;
});
}
--
2.29.2