Jorge Almeida
2012-06-02 20:51:33 UTC
What would be the proper way of clearing the environment before setting some
variable and then execve()'ing?
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
extern char** environ;
int main(){
environ=NULL;
// clearenv();
printf("here\n");
if(-1 == setenv("FOO", "bar", 1)) printf("%s", strerror(errno));
char* foobar=getenv("FOO");
printf("FOO=%s\n", foobar);
exit(0);
}
$ ./test
here
Segmentation fault
Setting "environ=NULL" is what the source for clearenv() says. Segfault
doesn't occur when this line is commented out.
On the other hand:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
extern char** environ;
int main(){
// environ=NULL;
clearenv();
printf("here\n");
if(-1 == setenv("FOO", "bar", 1)) printf("%s", strerror(errno));
char* foobar=getenv("FOO");
printf("FOO=%s\n", foobar);
exit(0);
}
$ make test
/opt/bin/diet gcc -O2 -march=atom -mfpmath=sse -mssse3
-fomit-frame-pointer -pipe -Wall -o test test.c
test.c: In function ‘main’:
test.c:17:3: warning: implicit declaration of function ‘clearenv’
[-Wimplicit-function-declaration]
/tmp/ccXEu0zU.o: In function `main':
test.c:(.text.startup+0x33): warning: setenv calls malloc. Avoid it
in small programs.
/extra/dietlibc/dietlibc-cvs/bin-i386/dietlibc.a(vprintf.o): In
function `vprintf':
vprintf.c:(.text+0x58): warning: warning: the printf functions add
several kilobytes of bloat.
/tmp/ccXEu0zU.o: In function `main':
test.c:(.text.startup+0xb): undefined reference to `clearenv'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
TIA
Jorge Almeida
variable and then execve()'ing?
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
extern char** environ;
int main(){
environ=NULL;
// clearenv();
printf("here\n");
if(-1 == setenv("FOO", "bar", 1)) printf("%s", strerror(errno));
char* foobar=getenv("FOO");
printf("FOO=%s\n", foobar);
exit(0);
}
$ ./test
here
Segmentation fault
Setting "environ=NULL" is what the source for clearenv() says. Segfault
doesn't occur when this line is commented out.
On the other hand:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
extern char** environ;
int main(){
// environ=NULL;
clearenv();
printf("here\n");
if(-1 == setenv("FOO", "bar", 1)) printf("%s", strerror(errno));
char* foobar=getenv("FOO");
printf("FOO=%s\n", foobar);
exit(0);
}
$ make test
/opt/bin/diet gcc -O2 -march=atom -mfpmath=sse -mssse3
-fomit-frame-pointer -pipe -Wall -o test test.c
test.c: In function ‘main’:
test.c:17:3: warning: implicit declaration of function ‘clearenv’
[-Wimplicit-function-declaration]
/tmp/ccXEu0zU.o: In function `main':
test.c:(.text.startup+0x33): warning: setenv calls malloc. Avoid it
in small programs.
/extra/dietlibc/dietlibc-cvs/bin-i386/dietlibc.a(vprintf.o): In
function `vprintf':
vprintf.c:(.text+0x58): warning: warning: the printf functions add
several kilobytes of bloat.
/tmp/ccXEu0zU.o: In function `main':
test.c:(.text.startup+0xb): undefined reference to `clearenv'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
TIA
Jorge Almeida