You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/04/16 18:18:55 UTC

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #6083: sched/environ: Refine the environment variables storage layout

xiaoxiang781216 commented on code in PR #6083:
URL: https://github.com/apache/incubator-nuttx/pull/6083#discussion_r851655609


##########
sched/environ/env_dup.c:
##########
@@ -64,8 +64,8 @@
 int env_dup(FAR struct task_group_s *group)
 {
   FAR struct tcb_s *ptcb = this_task();
-  FAR char *envp = NULL;
-  size_t envlen;
+  FAR char **envp = NULL;
+  size_t envlen = 0;

Review Comment:
   Done.



##########
sched/environ/env_getenv.c:
##########
@@ -106,7 +104,7 @@ FAR char *getenv(FAR const char *name)
 errout_with_lock:
   sched_unlock();
 errout:
-  set_errno(ret);
+  set_errno(-ret);

Review Comment:
   avoid at line 85:
   ```
   ret = -ret;
   ```



##########
sched/environ/env_setenv.c:
##########
@@ -144,36 +144,42 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
 
   if (group->tg_envp)
     {
-      newsize = group->tg_envsize + varlen;
-      newenvp = (FAR char *)group_realloc(group, group->tg_envp,
-                                          newsize + 1);
+      while (group->tg_envp[oldsize] != NULL)
+        {
+          oldsize++;
+        }
+
+      newenvp = group_realloc(group, group->tg_envp,
+                              sizeof(*newenvp) * (oldsize + 2));
       if (!newenvp)
         {
           ret = ENOMEM;
           goto errout_with_lock;
         }
 
-      pvar = &newenvp[group->tg_envsize];
+      newenvp[oldsize + 1] = NULL;
     }
   else
     {
-      newsize = varlen;
-      newenvp = (FAR char *)group_malloc(group, varlen + 1);
+      oldsize = 0;
+      newenvp = group_zalloc(group, sizeof(*newenvp) * 2);
       if (!newenvp)
         {
           ret = ENOMEM;
           goto errout_with_lock;
         }
-
-      pvar = newenvp;
     }
 
-  newenvp[newsize] = '\0';
+  group->tg_envp = newenvp;
 
-  /* Save the new buffer and size */
+  pvar = group_malloc(group, varlen);

Review Comment:
   Actually, the initial code just like what you suggest above, but I change it to the current form, because we don't need release newenvp or pvar at all.



##########
sched/environ/env_dup.c:
##########
@@ -94,28 +96,50 @@ int env_dup(FAR struct task_group_s *group)
         {
           /* There is an environment, duplicate it */
 
-          envp = (FAR char *)group_malloc(group, envlen + 1);
+          envp = group_malloc(group, sizeof(*envp) * (envlen + 1));
           if (envp == NULL)
             {
               /* The parent's environment can not be inherited due to a
                * failure in the allocation of the child environment.
                */
 
-              envlen = 0;
-              ret    = -ENOMEM;
+              ret = -ENOMEM;
             }
           else
             {
               /* Duplicate the parent environment. */
 
-              memcpy(envp, ptcb->group->tg_envp, envlen + 1);
+              do
+                {
+                  if (ptcb->group->tg_envp[envlen] == NULL)
+                    {
+                      envp[envlen] = NULL;
+                      continue;

Review Comment:
   Done.



##########
sched/environ/env_dup.c:
##########
@@ -94,28 +96,50 @@ int env_dup(FAR struct task_group_s *group)
         {
           /* There is an environment, duplicate it */
 
-          envp = (FAR char *)group_malloc(group, envlen + 1);
+          envp = group_malloc(group, sizeof(*envp) * (envlen + 1));
           if (envp == NULL)
             {
               /* The parent's environment can not be inherited due to a
                * failure in the allocation of the child environment.
                */
 
-              envlen = 0;
-              ret    = -ENOMEM;
+              ret = -ENOMEM;
             }
           else
             {
               /* Duplicate the parent environment. */
 
-              memcpy(envp, ptcb->group->tg_envp, envlen + 1);
+              do
+                {
+                  if (ptcb->group->tg_envp[envlen] == NULL)
+                    {
+                      envp[envlen] = NULL;
+                      continue;
+                    }
+
+                  envp[envlen] = group_malloc(group,
+                                  strlen(ptcb->group->tg_envp[envlen]) + 1);
+                  if (envp[envlen] == NULL)
+                    {
+                      while (envp[++envlen] != NULL)
+                        {
+                          group_free(group, envp[envlen]);
+                        }
+
+                      group_free(group, envp);
+                      ret = -ENOMEM;
+                      break;
+                    }
+
+                  strcpy(envp[envlen], ptcb->group->tg_envp[envlen]);
+                }
+              while (envlen--);

Review Comment:
   Done.



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org