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

View File

@ -1,7 +1,8 @@
/**************************************************************************** /****************************************************************************
* sched/environ/env_unsetenv.c * 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> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * 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 */ /* Reallocate the new environment buffer */
newsize = group->tg_envsize; newsize = group->tg_envsize;
newenvp = (FAR char *)kumm_realloc(group->tg_envp, newsize); if (newsize <= 0)
if (!newenvp)
{ {
set_errno(ENOMEM); group->tg_envp = NULL;
ret = ERROR; group->tg_envsize = 0;
} }
else else
{ {
/* Save the new environment pointer (it might have changed due to newenvp = (FAR char *)kumm_realloc(group->tg_envp, newsize);
* reallocation. 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;
}
} }
} }