summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2005-09-10 09:44:19 +0000
committerMarcel Holtmann <marcel@holtmann.org>2005-09-10 09:44:19 +0000
commitb4b75b29434226f97505afa14a9b8838d9b93f61 (patch)
tree464ce1cc9dbbe7f9db2b45abbe881dd203e855f6
parent92510c33c1ab156ced109ae1652248ef08beabe8 (diff)
Fix infinite loops and false positive matches
-rw-r--r--common/textfile.c39
1 files changed, 14 insertions, 25 deletions
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;