From d3c23739400d2f010187f619059e0699dc7bf5f0 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 30 Sep 2018 11:40:10 -0600 Subject: [PATCH] sched/environ/env_unsetenv.c: Fix and error in unsetenv() when unsetting the last of the environment variables. --- sched/environ/env_removevar.c | 12 ++++++------ sched/environ/env_unsetenv.c | 27 ++++++++++++++++++--------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/sched/environ/env_removevar.c b/sched/environ/env_removevar.c index 0ac54e7100..7efc6057ea 100644 --- a/sched/environ/env_removevar.c +++ b/sched/environ/env_removevar.c @@ -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. diff --git a/sched/environ/env_unsetenv.c b/sched/environ/env_unsetenv.c index 637b655a0a..7f04d485d1 100644 --- a/sched/environ/env_unsetenv.c +++ b/sched/environ/env_unsetenv.c @@ -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 * * 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; + } } }