commit 5b0662d5646321f91ccc9f8ec97e5fa3a65f8118
parent 6442aecfc08e934d65b38ec83c76bd8a040370a0
Author: Quentin Rameau <quinq@fifth.space>
Date: Thu, 22 Jun 2017 18:59:06 +0200
Parse raw buffer for correctness before processing
Diffstat:
sacc.c | | | 23 | ++++++++++++++++------- |
1 file changed, 16 insertions(+), 7 deletions(-)
diff --git a/sacc.c b/sacc.c
@@ -193,15 +193,23 @@ Dir *
molddiritem(char *raw)
{
Item *item, **items = NULL;
+ char *crlf, *p;
Dir *dir;
- size_t nitems = 0;
+ size_t i, nitems;
- dir = xmalloc(sizeof(Dir));
+ for (crlf = raw, nitems = 0; p = strstr(crlf, "\r\n"); ++nitems)
+ crlf = p+2;
+ if (--nitems < 1)
+ return NULL;
+ if (strcmp(crlf-3, ".\r\n"))
+ return NULL;
- while (strncmp(raw, ".\r\n", 3)) {
- items = xreallocarray(items, ++nitems, sizeof(Item*));
+ dir = xmalloc(sizeof(Dir));
+ items = xreallocarray(items, nitems, sizeof(Item*));
+ for (i = 0; i < nitems; ++i) {
item = xmalloc(sizeof(Item));
+
item->type = *raw++;
item->username = pickfield(&raw);
item->selector = pickfield(&raw);
@@ -211,7 +219,7 @@ molddiritem(char *raw)
item->entry = NULL;
item->dir = NULL;
- items[nitems-1] = item;
+ items[i] = item;
}
dir->items = items;
@@ -319,8 +327,9 @@ dig(Item *entry, Item *item)
return 0;
}
- if (item->type == '1')
- item->dir = molddiritem(item->raw);
+ if (item->type == '1' &&
+ !(item->dir = molddiritem(item->raw)))
+ return 0;
return 1;
}