From 4aa09bc17e6aa7a876bd9b362cc5c30fbee16736 Mon Sep 17 00:00:00 2001 From: sof Date: Sat, 8 Aug 1998 17:38:42 +0000 Subject: [PATCH] [project @ 1998-08-08 17:38:42 by sof] _setenv: off by one error when extending the env block. (has this code ever been used in anger?) --- ghc/lib/posix/cbits/env.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ghc/lib/posix/cbits/env.c b/ghc/lib/posix/cbits/env.c index 936039c..79a85e7 100644 --- a/ghc/lib/posix/cbits/env.c +++ b/ghc/lib/posix/cbits/env.c @@ -68,7 +68,8 @@ copyenv(void) char **new; int i; - for (i = 0; environ[i] != NULL; i++); + for (i = 0; environ[i] != NULL; i++) + ; if ((new = (char **) malloc((i + 1) * sizeof(char *))) == NULL) return -1; @@ -120,7 +121,8 @@ _setenv(char *mapping) free(environ[i]); environ[i] = p; } else { - if ((new = (char **) realloc(environ, (i + 1) * sizeof(char *))) == NULL) { + /* We want to grow the table by *two*, one for the new entry, one for the terminator */ + if ((new = (char **) realloc((void*)environ, (i + 2) * sizeof(char *))) == NULL) { free(p); return -1; } -- 1.7.10.4