This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 321105a3b4d arch: Pass the environment to main() as its third argument.
321105a3b4d is described below
commit 321105a3b4d67fac061ceafe5e5e988de15bc6f1
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Oct 8 19:34:46 2026 +0200
arch: Pass the environment to main() as its third argument.
The startup code of ELF programs called main(argc, argv). Programs that
declare main(int argc, char *argv[], char *envp[]), a common Unix
extension, read a random value as envp and crash when they walk
it.
crt0 now passes environ (NULL with CONFIG_DISABLE_ENVIRON). A program
with a two-argument main() is not affected.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
arch/arm/src/common/crt0.c | 13 ++++++++++---
arch/arm64/src/common/crt0.c | 13 ++++++++++---
arch/risc-v/src/common/crt0.c | 13 ++++++++++---
arch/x86_64/src/common/crt0.c | 13 ++++++++++---
arch/xtensa/src/common/crt0.c | 13 ++++++++++---
5 files changed, 50 insertions(+), 15 deletions(-)
diff --git a/arch/arm/src/common/crt0.c b/arch/arm/src/common/crt0.c
index b2a24e8276b..8acc03893d2 100644
--- a/arch/arm/src/common/crt0.c
+++ b/arch/arm/src/common/crt0.c
@@ -50,7 +50,7 @@ extern initializer_t _edtors[];
* Public Function Prototypes
****************************************************************************/
-int main(int argc, char *argv[]);
+int main(int argc, char *argv[], char *envp[]);
/****************************************************************************
* Private Functions
@@ -189,9 +189,16 @@ void _start(int argc, char *argv[])
# endif
#endif
- /* Call the main() entry point passing argc and argv. */
+ /* Call the main() entry point passing argc, argv and the environment.
+ * The third argument is a common Unix extension; a two-argument main()
+ * ignores it.
+ */
- ret = main(argc, argv);
+#ifdef CONFIG_DISABLE_ENVIRON
+ ret = main(argc, argv, NULL);
+#else
+ ret = main(argc, argv, environ);
+#endif
#if defined(CONFIG_HAVE_CXXINITIALIZE) && CONFIG_LIBC_MAX_EXITFUNS <= 0
exec_dtors();
diff --git a/arch/arm64/src/common/crt0.c b/arch/arm64/src/common/crt0.c
index 0bc58917ac2..4377cfd31ba 100644
--- a/arch/arm64/src/common/crt0.c
+++ b/arch/arm64/src/common/crt0.c
@@ -38,7 +38,7 @@
* Public Function Prototypes
****************************************************************************/
-int main(int argc, char *argv[]);
+int main(int argc, char *argv[], char *envp[]);
/****************************************************************************
* Private Functions
@@ -191,9 +191,16 @@ void _start(int argc, char *argv[])
# endif
#endif
- /* Call the main() entry point passing argc and argv. */
+ /* Call the main() entry point passing argc, argv and the environment.
+ * The third argument is a common Unix extension; a two-argument main()
+ * ignores it.
+ */
- ret = main(argc, argv);
+#ifdef CONFIG_DISABLE_ENVIRON
+ ret = main(argc, argv, NULL);
+#else
+ ret = main(argc, argv, environ);
+#endif
#if defined(CONFIG_HAVE_CXXINITIALIZE) && CONFIG_LIBC_MAX_EXITFUNS <= 0
exec_dtors();
diff --git a/arch/risc-v/src/common/crt0.c b/arch/risc-v/src/common/crt0.c
index 93ff42e4627..cb191f3835c 100644
--- a/arch/risc-v/src/common/crt0.c
+++ b/arch/risc-v/src/common/crt0.c
@@ -39,7 +39,7 @@
* Public Function Prototypes
****************************************************************************/
-int main(int argc, char *argv[]);
+int main(int argc, char *argv[], char *envp[]);
/****************************************************************************
* Private Functions
@@ -188,9 +188,16 @@ void _start(int argc, char *argv[])
# endif
#endif
- /* Call the main() entry point passing argc and argv. */
+ /* Call the main() entry point passing argc, argv and the environment.
+ * The third argument is a common Unix extension; a two-argument main()
+ * ignores it.
+ */
- ret = main(argc, argv);
+#ifdef CONFIG_DISABLE_ENVIRON
+ ret = main(argc, argv, NULL);
+#else
+ ret = main(argc, argv, environ);
+#endif
#if defined(CONFIG_HAVE_CXXINITIALIZE) && CONFIG_LIBC_MAX_EXITFUNS <= 0
exec_dtors();
diff --git a/arch/x86_64/src/common/crt0.c b/arch/x86_64/src/common/crt0.c
index a095b0a3e97..c6053698b36 100644
--- a/arch/x86_64/src/common/crt0.c
+++ b/arch/x86_64/src/common/crt0.c
@@ -49,7 +49,7 @@ extern initializer_t _edtors[];
* Public Function Prototypes
****************************************************************************/
-int main(int argc, char *argv[]);
+int main(int argc, char *argv[], char *envp[]);
/****************************************************************************
* Private Functions
@@ -173,9 +173,16 @@ void _start(int argc, char *argv[])
# endif
#endif
- /* Call the main() entry point passing argc and argv. */
+ /* Call the main() entry point passing argc, argv and the environment.
+ * The third argument is a common Unix extension; a two-argument main()
+ * ignores it.
+ */
- ret = main(argc, argv);
+#ifdef CONFIG_DISABLE_ENVIRON
+ ret = main(argc, argv, NULL);
+#else
+ ret = main(argc, argv, environ);
+#endif
#if defined(CONFIG_HAVE_CXXINITIALIZE) && CONFIG_LIBC_MAX_EXITFUNS <= 0
exec_dtors();
diff --git a/arch/xtensa/src/common/crt0.c b/arch/xtensa/src/common/crt0.c
index 5dd1458e982..f3491918ea5 100644
--- a/arch/xtensa/src/common/crt0.c
+++ b/arch/xtensa/src/common/crt0.c
@@ -50,7 +50,7 @@ extern initializer_t _edtors[];
* Public Function Prototypes
****************************************************************************/
-int main(int argc, char *argv[]);
+int main(int argc, char *argv[], char *envp[]);
/****************************************************************************
* Private Functions
@@ -187,9 +187,16 @@ void _start(int argc, char *argv[])
# endif
#endif
- /* Call the main() entry point passing argc and argv. */
+ /* Call the main() entry point passing argc, argv and the environment.
+ * The third argument is a common Unix extension; a two-argument main()
+ * ignores it.
+ */
- ret = main(argc, argv);
+#ifdef CONFIG_DISABLE_ENVIRON
+ ret = main(argc, argv, NULL);
+#else
+ ret = main(argc, argv, environ);
+#endif
#if defined(CONFIG_HAVE_CXXINITIALIZE) && CONFIG_LIBC_MAX_EXITFUNS <= 0
exec_dtors();