From 1cfe0ce94eb6c513dc7af7c7ac4128052a164f6f Mon Sep 17 00:00:00 2001 From: Philippe Pepiot Date: Wed, 26 Aug 2009 03:03:07 +0200 Subject: [PATCH] Confparse: prevent multiple _strdup call in file_to_str() --- src/confparse/confparse.c | 25 +++++++++++++++++++------ src/confparse/util.c | 3 ++- src/wmfs.h | 4 ++-- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/confparse/confparse.c b/src/confparse/confparse.c index 55496cf..af4550a 100644 --- a/src/confparse/confparse.c +++ b/src/confparse/confparse.c @@ -38,6 +38,7 @@ file_to_str(char *path) char *buf, *ret, *p; int fd, i; struct stat st; + Bool is_char = False; if(!path || !(fd = open(path, O_RDONLY))) return NULL; @@ -49,17 +50,29 @@ file_to_str(char *path) if((buf = (char*)mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, SEEK_SET)) == (char*) -1) return NULL; - /* Copy buffer in return value */ - ret = _strdup(buf); + /* Copy buffer without comments in return value */ + ret = emalloc(strlen(buf) + 1, sizeof(char)); + + for(p = buf, i = 0; *p != '\0'; p++) + { + if(strchr("\"'", *p)) + is_char = (is_char == False) ? True : False; + + if(*p == COMMENT_CHAR && is_char == False) + { + if(!(p = strchr(p, '\n'))) + break; + ret[i++] = '\n'; + } + else + ret[i++] = *p; + } + ret[i++] = '\0'; /* Unmap buffer, thanks linkdd. */ munmap(buf, st.st_size); close(fd); - /* Erase comment line from return value */ - for(i = 0; (p = strchr(erase_delim_content(ret + i), COMMENT_CHAR));) - for(i = st.st_size - strlen(p); ret[i] && ret[i] != '\n'; ret[i++] = ' '); - fprintf(stderr, "WMFS Configuration info: '%s' read.\n", path); return ret; diff --git a/src/confparse/util.c b/src/confparse/util.c index e69cd26..9c19f2a 100644 --- a/src/confparse/util.c +++ b/src/confparse/util.c @@ -42,12 +42,13 @@ erase_delim_content(char *buf) return NULL; for(i = 0; i < strlen(str); ++i) - if((c = str[i]) == '"' || (c = str[i]) == '\'') + if(strchr("\"'", (c = str[i]))) { for(*(str + (j = i)) = ' '; str[j] && str[j] != c; str[j++] = ' '); str[j] = ' '; } + return str; } diff --git a/src/wmfs.h b/src/wmfs.h index 1359de6..5b08c16 100644 --- a/src/wmfs.h +++ b/src/wmfs.h @@ -55,14 +55,14 @@ #include "config.h" #include "structs.h" -/* Optional dependences */ +/* Optional dependencies */ #ifdef HAVE_XINERAMA #include #endif /* HAVE_XINERAMA */ #ifdef HAVE_XRANDR #include -#endif /* HAVE_XINERAMA */ +#endif /* HAVE_XRANDR */ /* MACRO */ #define ButtonMask (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)