real_main: Use setenv() instead of putenv()

When investigating possible memory leaks with 'valgrind' I noticed
this leak report:

 602 bytes in 13 blocks are definitely lost in loss record 77 of 128
    at 0x4C2362E: malloc (vg_replace_malloc.c:207)
    by 0x4576ED: wmalloc (memory.c:88)
    by 0x45B4F7: wstrconcat (string.c:214)
    by 0x426FC2: main (main.c:608)

which happens here,

    str = wstrconcat("WMAKER_BIN_NAME=", argv[0]);
    putenv(str);

There is a comment further below (in another context)

/* return of wstrconcat should not be free-ed! read putenv man page */

so this leak report is probably a false positive anyway. However,
https://www.securecoding.cert.org/confluence/display/seccode/POS34-C.+Do+not+call+putenv%28%29+with+a+pointer+to+an+automatic+variable+as+the+argument
has some nice discussion about putenv() and after reading it I decided to
use setenv() instead, making 'valgrind' happy along the way. It also
makes the code a bit simpler, avoiding a call to wstrconcat().

I also changed the name of another variable called 'str' to 'pos', just
in case.
---
 src/main.c |   12 +++++-------
 1 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/src/main.c b/src/main.c
index 06eed08..3d30ef7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -593,7 +593,7 @@ static int
 real_main(int argc, char **argv)
 {
     int i, restart=0;
-    char *str;
+    char *str, *pos;
     int d, s;
     int flag;
 #ifdef DEBUG
@@ -604,9 +604,7 @@ real_main(int argc, char **argv)
     wsetabort(wAbort);
 
     /* for telling WPrefs what's the name of the wmaker binary being ran */
-
-    str = wstrconcat("WMAKER_BIN_NAME=", argv[0]);
-    putenv(str);
+    setenv("WMAKER_BIN_NAME", argv[0], 1);
 
     flag= 0;
     ArgCount = argc;
@@ -787,11 +785,11 @@ real_main(int argc, char **argv)
     /* check if the user specified a complete display name (with screen).
      * If so, only manage the specified screen */
     if (DisplayName)
-        str = strchr(DisplayName, ':');
+        pos = strchr(DisplayName, ':');
     else
-        str = NULL;
+        pos = NULL;
 
-    if (str && sscanf(str, ":%i.%i", &d, &s)==2)
+    if (pos && sscanf(pos, ":%i.%i", &d, &s)==2)
         multiHead = False;
 
     DisplayName = XDisplayName(DisplayName);
-- 
1.6.4.rc1.13.gec7b


-- 
To unsubscribe, send mail to [email protected].

Reply via email to