tParse raw buffer for correctness before processing - sacc - sacc(omys), simple console gopher client (mirror) HTML git clone https://git.parazyd.org/sacc DIR Log DIR Files DIR Refs DIR LICENSE --- DIR commit 0d43e40b023b104e36cf2802f0bde64ba2150066 DIR parent c89fcb00443d7f4f49a9766d4aba82a1f7059826 HTML Author: Quentin Rameau <quinq@fifth.space> Date: Thu, 22 Jun 2017 18:59:06 +0200 Parse raw buffer for correctness before processing Diffstat: M sacc.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) --- DIR diff --git a/sacc.c b/sacc.c t@@ -190,15 +190,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); t@@ -208,7 +216,7 @@ molddiritem(char *raw) item->entry = NULL; item->dir = NULL; - items[nitems-1] = item; + items[i] = item; } dir->items = items; t@@ -314,8 +322,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; }