sacc

sacc (saccomys): simple gopher client.
Log | Files | Refs | LICENSE

commit 0d43e40b023b104e36cf2802f0bde64ba2150066
parent c89fcb00443d7f4f49a9766d4aba82a1f7059826
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 @@ -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); @@ -208,7 +216,7 @@ molddiritem(char *raw) item->entry = NULL; item->dir = NULL; - items[nitems-1] = item; + items[i] = item; } dir->items = items; @@ -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; }