From 8589bffa4944a9898e0d9ef97cd27fcc5208aaeb Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Tue, 29 Jun 2010 09:26:21 -0400 Subject: [PATCH] Do not rely on env.env_len when unsetting a variable, just use the NULL terminator. --HG-- branch : 1.7 --- env.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/env.c b/env.c index 80fa45b37..59b59eb34 100644 --- a/env.c +++ b/env.c @@ -378,15 +378,16 @@ unsetenv(var) len = strlen(var); for (ep = env.envp; *ep != NULL;) { if (strncmp(var, *ep, len) == 0 && (*ep)[len] == '=') { - /* Found it; shift remainder + NULL over by one and update len. */ - memmove(ep, ep + 1, - (env.env_len - (ep - env.envp)) * sizeof(char *)); - env.env_len--; + /* Found it; shift remainder + NULL over by one. */ + char **cur = ep; + while ((*cur = *(cur + 1)) != NULL) + cur++; /* Keep going, could be multiple instances of the var. */ } else { ep++; } } + env.env_len = ep - env.envp; #ifndef UNSETENV_VOID return(0); #endif @@ -470,13 +471,14 @@ sudo_putenv(str, dupcheck, overwrite) if (found && overwrite) { while (*ep != NULL) { if (strncmp(str, *ep, len) == 0) { - memmove(ep, ep + 1, - (env.env_len - (ep - env.envp)) * sizeof(char *)); - env.env_len--; + char **cur = ep; + while ((*cur = *(cur + 1)) != NULL) + cur++; } else { ep++; } } + env.env_len = ep - env.envp; } } -- 2.40.0