"Todd C. Miller" <[email protected]> writes:
> On Mon, 25 Apr 2016 16:56:47 -0400, "Ted Unangst" wrote:
>
>> compilers will, however, "miscompile" such code. we should avoid it.
>
> Fair enough.
ok jca@
> - todd
>
> Index: lib/libc/stdlib/setenv.c
> ===================================================================
> RCS file: /cvs/src/lib/libc/stdlib/setenv.c,v
> retrieving revision 1.17
> diff -u -p -r1.17 setenv.c
> --- lib/libc/stdlib/setenv.c 13 Mar 2016 18:34:21 -0000 1.17
> +++ lib/libc/stdlib/setenv.c 25 Apr 2016 21:09:20 -0000
> @@ -43,7 +43,7 @@ int
> putenv(char *str)
> {
> char **P, *cp;
> - size_t cnt;
> + size_t cnt = 0;
> int offset = 0;
>
> for (cp = str; *cp && *cp != '='; ++cp)
> @@ -65,13 +65,15 @@ putenv(char *str)
> }
>
> /* create new slot for string */
> - for (P = environ; *P != NULL; P++)
> - ;
> - cnt = P - environ;
> + if (environ != NULL) {
> + for (P = environ; *P != NULL; P++)
> + ;
> + cnt = P - environ;
> + }
> P = reallocarray(lastenv, cnt + 2, sizeof(char *));
> if (!P)
> return (-1);
> - if (lastenv != environ)
> + if (lastenv != environ && environ != NULL)
> memcpy(P, environ, cnt * sizeof(char *));
> lastenv = environ = P;
> environ[cnt] = str;
> @@ -122,15 +124,17 @@ setenv(const char *name, const char *val
> break;
> }
> } else { /* create new slot */
> - size_t cnt;
> + size_t cnt = 0;
>
> - for (P = environ; *P != NULL; P++)
> - ;
> - cnt = P - environ;
> + if (environ != NULL) {
> + for (P = environ; *P != NULL; P++)
> + ;
> + cnt = P - environ;
> + }
> P = reallocarray(lastenv, cnt + 2, sizeof(char *));
> if (!P)
> return (-1);
> - if (lastenv != environ)
> + if (lastenv != environ && environ != NULL)
> memcpy(P, environ, cnt * sizeof(char *));
> lastenv = environ = P;
> offset = cnt;
>
--
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF DDCC 0DFA 74AE 1524 E7EE