Use xreallocarray instead of xrealloc - sacc - sacc(omys), simple console gopher client
git clone git://bitreich.org/sacc/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/sacc/
Log
Files
Refs
Tags
LICENSE
---
commit 153d2f1c774d180d75308fa1c7afce11e4fc522a
parent 96fd418befb74178138958e4803b35d2fc06c7a9
Author: Quentin Rameau 
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(-)
---
diff --git a/sacc.c b/sacc.c
@@ -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;
@@ -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++;
@@ -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;