commit e8214e1e8d0394f4569e5049f162794d69019a03
parent 50d40c927e6bfb06eea4e94865e2d5a9ee9978ca
Author: Quentin Rameau <quinq@fifth.space>
Date: Thu, 22 Jun 2017 17:12:50 +0200
Use xreallocarray instead of xrealloc
Diffstat:
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/sacc.c b/sacc.c
@@ -44,12 +44,14 @@ 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;
errno = 0;
- 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;
@@ -192,13 +194,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++;
@@ -234,7 +235,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;