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

@ -89,8 +89,8 @@ int env_removevar(FAR struct task_group_s *group, FAR char *pvar)
/* 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 */
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.'

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,21 +95,29 @@ int unsetenv(FAR const char *name)
/* Reallocate the new environment buffer */
newsize = group->tg_envsize;
if (newsize <= 0)
{
group->tg_envp = NULL;
group->tg_envsize = 0;
}
else
{
newenvp = (FAR char *)kumm_realloc(group->tg_envp, newsize);
if (!newenvp)
if (newenvp == NULL)
{
set_errno(ENOMEM);
ret = ERROR;
}
else
{
/* Save the new environment pointer (it might have changed due to
* reallocation.
/* Save the new environment pointer (it might have changed due
* to reallocation).
*/
group->tg_envp = newenvp;
}
}
}
sched_unlock();
return ret;