From 6310e40d47b2c4ba4af7c9098c72f8069386b90b Mon Sep 17 00:00:00 2001 From: "Carlos R. Mafra" Date: Mon, 17 Aug 2009 22:03:28 +0200 Subject: [PATCH] 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 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main.c b/src/main.c index 06eed084..3d30ef71 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); -- 2.11.4.GIT