tUse xreallocarray instead of xrealloc - 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 153d2f1c774d180d75308fa1c7afce11e4fc522a DIR parent 96fd418befb74178138958e4803b35d2fc06c7a9 HTML Author: Quentin Rameau <quinq@fifth.space> Date: Thu, 22 Jun 2017 17:12:50 +0200 Use xreallocarray instead of xrealloc Diffstat: M sacc.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) --- DIR diff --git a/sacc.c b/sacc.c t@@ -44,11 +44,13 @@ die(const char *fmt, ...) } void * -xrealloc(void *m, const size_t n) +xreallocarray(void *m, const size_t n, const size_t s) { void *nm; - if (!(nm = realloc(m, n))) + if (s && n > (size_t)-1/s) + die("realloc: overflow"); + if (!(nm = realloc(m, n * s))) die("realloc: %s", strerror(errno)); return nm; t@@ -189,13 +191,12 @@ molddiritem(char *raw) { Item *item, **items = NULL; Dir *dir; - size_t n, nitems = 0; + size_t nitems = 0; dir = xmalloc(sizeof(Dir)); while (strncmp(raw, ".\r\n", 3)) { - n = (++nitems) * sizeof(Item*); - items = xrealloc(items, n); + items = xreallocarray(items, ++nitems, sizeof(Item*)); item = xmalloc(sizeof(Item)); item->type = *raw++; t@@ -231,7 +232,7 @@ getrawitem(int sock) buf += n; if (bs <= 0) { ns = is + BUFSIZ; - item = xrealloc(item, ns+1); + item = xreallocarray(item, ns+1, 1); is = ns; buf = item + is-BUFSIZ; bs = BUFSIZ;