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 17:40:50 UTC

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

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


##########
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:
   probably renaming `envlen` -> `envc` can be a good option.



##########
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:
   optional: below code can be moved to `else` and `continue` can be eliminated. Or even this may be moved out of `do {} while` like:
   ```
   /* Duplicate the parent environment. */
   
   envp[envlen--] = NULL;
   
   for (; envlen > 0; envlen--)
     {
   /* perform allocation here */
     }
   ```



##########
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:
   optional `> 0`



-- 
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