From b4b75b29434226f97505afa14a9b8838d9b93f61 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 10 Sep 2005 09:44:19 +0000 Subject: Fix infinite loops and false positive matches --- common/textfile.c | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) (limited to 'common/textfile.c') diff --git a/common/textfile.c b/common/textfile.c index 095e0355..f15d439c 100644 --- a/common/textfile.c +++ b/common/textfile.c @@ -113,6 +113,17 @@ static inline int write_key_value(int fd, char *key, char *value) return err; } +static inline char *find_key(char *map, char *key, size_t len) +{ + char *off = strstr(map, key); + + while (off && ((off > map && *(off - 1) != '\r' && + *(off - 1) != '\n') || *(off + len) != ' ')) + off = strstr(off + len, key); + + return off; +} + int textfile_put(char *pathname, char *key, char *value) { struct stat st; @@ -148,7 +159,7 @@ int textfile_put(char *pathname, char *key, char *value) goto unlock; } - off = strstr(map, key); + off = find_key(map, key, strlen(key)); if (!off) { munmap(map, size); pos = lseek(fd, size, SEEK_SET); @@ -156,18 +167,6 @@ int textfile_put(char *pathname, char *key, char *value) goto unlock; } - if (off > map) { - while (*(off - 1) != '\r' && *(off - 1) != '\n') { - off = strstr(off, key); - if (!off) { - munmap(map, size); - pos = lseek(fd, size, SEEK_SET); - err = write_key_value(fd, key, value); - goto unlock; - } - } - } - base = off - map; end = strpbrk(off, "\r\n"); @@ -254,29 +253,19 @@ char *textfile_get(char *pathname, char *key) goto unlock; } - off = strstr(map, key); + len = strlen(key); + off = find_key(map, key, len); if (!off) { err = EILSEQ; goto unmap; } - if (off > map) { - while (*(off - 1) != '\r' && *(off - 1) != '\n') { - off = strstr(off, key); - if (!off) { - err = EILSEQ; - goto unmap; - } - } - } - end = strpbrk(off, "\r\n"); if (!end) { err = EILSEQ; goto unmap; } - len = strlen(key); str = malloc(end - off - len); if (!str) { err = EILSEQ; -- cgit