sched/environ/env_unsetenv.c: Fix and error in unsetenv() when unsetting the last of the environment variables.

This commit is contained in:
Gregory Nutt 2018-09-30 11:40:10 -06:00
parent 4391b51cd3
commit d3c2373940
2 changed files with 24 additions and 15 deletions

View file

@ -81,17 +81,17 @@ int env_removevar(FAR struct task_group_s *group, FAR char *pvar)
/* Verify that the pointer lies within the environment region */
alloc = group->tg_envsize; /* Size of the allocated environment */
end = &group->tg_envp[alloc]; /* Pointer to the end+1 of the environment */
alloc = group->tg_envsize; /* Size of the allocated environment */
end = &group->tg_envp[alloc]; /* Pointer to the end+1 of the environment */
if (pvar >= group->tg_envp && pvar < end)
{
/* Set up for the removal */
int len = strlen(pvar) + 1; /* Length of name=value string to remove */
char *src = &pvar[len]; /* Address of name=value string after */
char *dest = pvar; /* Location to move the next string */
int count = end - src; /* Number of bytes to move (might be zero) */
int len = strlen(pvar) + 1; /* Length of name=value string to remove */
FAR char *src = &pvar[len]; /* Address of name=value string after */
FAR char *dest = pvar; /* Location to move the next string */
int count = end - src; /* Number of bytes to move (might be zero) */
/* Move all of the environment strings after the removed one 'down.'
* this is inefficient, but robably not high duty.

View file

@ -1,7 +1,8 @@
/****************************************************************************
* sched/environ/env_unsetenv.c
*
* Copyright (C) 2007, 2009, 2011, 2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2009, 2011, 2013, 2018 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -94,19 +95,27 @@ int unsetenv(FAR const char *name)
/* Reallocate the new environment buffer */
newsize = group->tg_envsize;
newenvp = (FAR char *)kumm_realloc(group->tg_envp, newsize);
if (!newenvp)
if (newsize <= 0)
{
set_errno(ENOMEM);
ret = ERROR;
group->tg_envp = NULL;
group->tg_envsize = 0;
}
else
{
/* Save the new environment pointer (it might have changed due to
* reallocation.
*/
newenvp = (FAR char *)kumm_realloc(group->tg_envp, newsize);
if (newenvp == NULL)
{
set_errno(ENOMEM);
ret = ERROR;
}
else
{
/* Save the new environment pointer (it might have changed due
* to reallocation).
*/
group->tg_envp = newenvp;
group->tg_envp = newenvp;
}
}
}