sched/environ: Ensure tg_envp terminated by double '\0'

so we can compute the whole environ string length from it

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2022-04-13 15:05:53 +08:00 committed by Petro Karashchenko
parent 4c693239c0
commit 701bbaac74
4 changed files with 9 additions and 9 deletions

View File

@ -94,7 +94,7 @@ int env_dup(FAR struct task_group_s *group)
{
/* There is an environment, duplicate it */
envp = (FAR char *)group_malloc(group, envlen);
envp = (FAR char *)group_malloc(group, envlen + 1);
if (envp == NULL)
{
/* The parent's environment can not be inherited due to a
@ -108,7 +108,7 @@ int env_dup(FAR struct task_group_s *group)
{
/* Duplicate the parent environment. */
memcpy(envp, ptcb->group->tg_envp, envlen);
memcpy(envp, ptcb->group->tg_envp, envlen + 1);
}
}

View File

@ -83,10 +83,7 @@ int env_removevar(FAR struct task_group_s *group, FAR char *pvar)
* this is inefficient, but robably not high duty.
*/
while (count-- > 0)
{
*dest++ = *src++;
}
memmove(dest, src, count + 1);
/* Then set to the new allocation size. The caller is expected to
* call realloc at some point but we don't do that here because the

View File

@ -145,7 +145,8 @@ 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);
newenvp = (FAR char *)group_realloc(group, group->tg_envp,
newsize + 1);
if (!newenvp)
{
ret = ENOMEM;
@ -157,7 +158,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
else
{
newsize = varlen;
newenvp = (FAR char *)group_malloc(group, varlen);
newenvp = (FAR char *)group_malloc(group, varlen + 1);
if (!newenvp)
{
ret = ENOMEM;
@ -167,6 +168,8 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
pvar = newenvp;
}
newenvp[newsize] = '\0';
/* Save the new buffer and size */
group->tg_envp = newenvp;

View File

@ -97,7 +97,7 @@ int unsetenv(FAR const char *name)
/* Reallocate the environment to reclaim a little memory */
newenvp = (FAR char *)group_realloc(group, group->tg_envp,
newsize);
newsize + 1);
if (newenvp == NULL)
{