Jary Jary -3 years ago 126
C Question

Issue with putenv() on UNIX when using free()

I am trying to use putenv() on UNIX by concatenating str1 and str2 before that.
I want to add a variable, or modify a variable, in the environment, so I am calling putenv() (or I could call setenv() identically).

Basically, I receive str1 and str2, I create str1=str2 and pass it in putenv() as a parameter.

The code I am showing works but when I uncomment the free() call, then it does not: the variable do not get added/modified for the environment.

size_t size = strlen(str1) + strlen(str2) + 2; // 2 is for the '\0' and the '='
char *tmp = (char *) malloc(sizeof(char) * size);
char *p;
int pos = 0;

// Copy first word
p = str1;
while (*p != NULL) {
tmp[pos++] = *p++;

// Add the '='
tmp[pos++] = '=';

// Copy second word
p = str2;
while (*p != NULL) {
tmp[pos++] = *p++;

// Add null character
tmp[pos] = '\0';

int ret = putenv(tmp);
if (ret != 0) {
perror("putenv failed");

//free(tmp); // This line is the problem when not commented

I apologize for the code redundancy, I know that the two while loops are identical. The issue that I have is that if I uncomment the free statement, then calling "env" to print the environment, the putenv will not have added the value.

I am not sure why is that. Right now to have it work, I am having a memory leak which I do not like. When I used an array, rather than a pointer, it would produce the same issue as uncommenting free.

Any ideas?

Answer Source

putenv() requires the string that is set into the environment to exist, because it doesn't copy the string; it uses a pointer to the string provided for the argument.

This is noted in the OpenGroup description of putenv() in a rather vague way:


"A potential error is to call putenv() with an automatic variable as the argument, then return from the calling function while string is still part of the environment."

Two possible solutions for your problem are:

1 - To get what you want using putenv(), you could use a static string, or otherwise use a string that does not go out of scope until the environment variable is unneeded.

2 - Alternatively, use setenv(const char *envname, const char *envval, int overwrite), which is easier to use than putenv, allocates memory and copies the string, and does not require you to concatenate strings like you are doing right now.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download