casaroli commented on code in PR #20583:
URL: https://github.com/apache/nuttx/pull/20583#discussion_r4263661995


##########
libs/libc/environ/env_dup.c:
##########
@@ -132,9 +133,42 @@ int env_dup(FAR struct task_info_s *info, FAR char * const 
*envcp)
             }
         }
 
+      /* libc allocated each string: record it as owned */
+
+      if (envp != NULL)
+        {
+          own = env_malloc(info, sizeof(*own) * info->ta_envpc);

Review Comment:
   The two blocks have different lives. The array is replaced when setenv() 
needs more entries: env_reserve() allocates a new one and frees the old one. 
The strings stay until exit, because a pointer that getenv() returned must stay 
valid. In one block, the array could not be freed alone.



##########
libs/libc/environ/env_sync.c:
##########
@@ -0,0 +1,153 @@
+/****************************************************************************
+ * libs/libc/environ/env_sync.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <stdbool.h>
+
+#include <nuttx/tls.h>
+
+#include "environ/environ.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: env_isowned
+ *
+ * Description:
+ *   Return true if libc allocated the string str and still owns it.
+ *
+ ****************************************************************************/
+
+static bool env_isowned(FAR struct task_info_s *info, FAR const char *str)
+{
+  ssize_t i;
+
+  if (info->ta_envown == NULL || str == NULL)
+    {
+      return false;
+    }
+
+  for (i = 0; i < info->ta_envpc; i++)
+    {
+      if (info->ta_envown[i] == str)
+        {
+          return true;
+        }
+    }
+
+  return false;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: env_sync
+ ****************************************************************************/
+
+int env_sync(FAR struct task_info_s *info)

Review Comment:
   Done



##########
include/nuttx/tls.h:
##########
@@ -153,7 +153,11 @@ struct task_info_s
 #ifndef CONFIG_DISABLE_ENVIRON
   /* Environment variables **************************************************/
 
-  FAR char            **ta_envp;    /* Allocated environment strings        */
+  FAR char            **ta_envp;    /* The array that environ names         */

Review Comment:
   A program can replace one variable many times, and each old string must stay 
valid until exit, because a pointer that getenv() returned may still be in use. 
So the number of strings to keep has no limit.
   
   A second half of ta_envp keeps one old string per entry, but not the strings 
of the earlier replacements. ta_envmem is a list of every block that libc 
allocated, so it holds any number of them, and they are all freed at exit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to