Re: putenv broken in the current CVS
Giorgio Dal Molin <[email protected]> Sat, 17 Aug 2019 16:16:32 +0200
| Newsgroups | gmane.linux.lib.dietlibc |
|---|---|
| Message-ID | <[email protected]> |
On 8/17/19 1:35 PM, Felix von Leitner wrote:
>> I think the current implementation of putenv() in lib/putenv.c is broken.
>
> Can you please provide a failing test case?
> We do have test/putenv.c and it runs through.
>
> I'm not touching that code unless I have a failing test case :)
>
> Felix
>
Hi,
here you have two test cases: main_1() is intended for humans, main_2()
is rather in unit test style: it inserts a new variable in the env list,
then a second one and then tries to read back the first one. It fails
with a segfault because getenv() returns a NULL.
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
static void dump_env(void)
{
for(char **ep=environ; *ep; ep++) {
write(1, *ep, strlen(*ep)); write(1,"\n",1);
};
}
int main_1(int argc, const char *argv[])
{
write(1,"env at start:\n", 14);
dump_env();
putenv("NEWVAR=NEWVAL");
write(1,"\n\n",2);
write(1,"env after putenv():\n", 20);
dump_env();
return 0;
}
int main_2(int argc, const char *argv[])
{
putenv("NEWVAR1=NEWVAL1");
putenv("NEWVAR2=NEWVAL2");
dump_env();
assert (!strcmp(getenv("NEWVAR1"), "NEWVAL1"));
return 0;
}
giorgio