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

commit 6f3b48ff4973494429c7421ebb57876e2dca009e
Author: ganjing <[email protected]>
AuthorDate: Tue May 5 01:38:30 2026 +0800

    libc/environ: Fail setenv() and unsetenv() without a task_info_s.
    
    getenv() returns NULL with EINVAL when the calling context has no
    task_info_s.  setenv() asserted on the same condition and unsetenv()
    dereferenced the NULL pointer.  Code that runs in such a context, for
    example on the assert path, could then recurse into a second assert or
    crash instead of seeing a failed call.
    
    Return -1 with EINVAL in both functions, as getenv() does.
    
    Signed-off-by: dengwenqi <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/environ/env_setenv.c   | 10 +++++++++-
 libs/libc/environ/env_unsetenv.c |  3 ++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/libs/libc/environ/env_setenv.c b/libs/libc/environ/env_setenv.c
index 6294ef28836..11b944bd7a1 100644
--- a/libs/libc/environ/env_setenv.c
+++ b/libs/libc/environ/env_setenv.c
@@ -100,7 +100,15 @@ int setenv(FAR const char *name, FAR const char *value, 
int overwrite)
         }
     }
 
-  DEBUGASSERT(info != NULL);
+  /* Like getenv(), fail with EINVAL when the caller has no task_info_s
+   * instead of asserting.
+   */
+
+  if (info == NULL)
+    {
+      set_errno(EINVAL);
+      return ERROR;
+    }
 
   namelen = strlen(name);
   nxrmutex_lock(&info->ta_lock);
diff --git a/libs/libc/environ/env_unsetenv.c b/libs/libc/environ/env_unsetenv.c
index 3711213e740..db9cc5e6605 100644
--- a/libs/libc/environ/env_unsetenv.c
+++ b/libs/libc/environ/env_unsetenv.c
@@ -61,7 +61,8 @@ int unsetenv(FAR const char *name)
 
   /* Check the incoming parameter */
 
-  if (name == NULL || *name == '\0' || strchr(name, '=') != NULL)
+  if (info == NULL || name == NULL || *name == '\0' ||
+      strchr(name, '=') != NULL)
     {
       set_errno(EINVAL);
       return ERROR;

Reply via email to