* Change eight reallocs to reallocarray to be safe from overflows.
* Change one malloc to reallocarray to be safe from overflows.
* Change one calloc to reallocarray, no zeroing needed.
* Change the order of arguments of three callocs (aesthetical).
-/* $Id: eqn.c,v 1.8 2014/04/20 19:39:35 schwarze Exp $ */
+/* $Id: eqn.c,v 1.9 2014/04/23 21:06:33 schwarze Exp $ */
/*
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
*
if (i == (int)ep->defsz) {
ep->defsz++;
- ep->defs = mandoc_realloc(ep->defs,
- ep->defsz * sizeof(struct eqn_def));
+ ep->defs = mandoc_reallocarray(ep->defs,
+ ep->defsz, sizeof(struct eqn_def));
ep->defs[i].key = ep->defs[i].val = NULL;
}
-/* $Id: mandoc_aux.c,v 1.2 2014/03/21 22:52:21 schwarze Exp $ */
+/* $Id: mandoc_aux.c,v 1.3 2014/04/23 21:06:33 schwarze Exp $ */
/*
* Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
return(ptr);
}
+void *
+mandoc_reallocarray(void *ptr, size_t num, size_t size)
+{
+
+ ptr = reallocarray(ptr, num, size);
+ if (NULL == ptr) {
+ perror(NULL);
+ exit((int)MANDOCLEVEL_SYSERR);
+ }
+ return(ptr);
+}
+
char *
mandoc_strdup(const char *ptr)
{
-/* $Id: mandoc_aux.h,v 1.2 2014/03/21 22:52:21 schwarze Exp $ */
+/* $Id: mandoc_aux.h,v 1.3 2014/04/23 21:06:33 schwarze Exp $ */
/*
* Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
void *mandoc_calloc(size_t, size_t);
void *mandoc_malloc(size_t);
void *mandoc_realloc(void *, size_t);
+void *mandoc_reallocarray(void *, size_t, size_t);
char *mandoc_strdup(const char *);
char *mandoc_strndup(const char *, size_t);
-/* $Id: mandocdb.c,v 1.100 2014/04/23 19:08:52 schwarze Exp $ */
+/* $Id: mandocdb.c,v 1.101 2014/04/23 21:06:33 schwarze Exp $ */
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
* manpath_parse() wants to do it.
*/
if (argc > 0) {
- dirs.paths = mandoc_calloc(argc,
- sizeof(char *));
+ dirs.paths = mandoc_reallocarray(NULL,
+ argc, sizeof(char *));
dirs.sz = (size_t)argc;
for (i = 0; i < argc; i++)
dirs.paths[i] = mandoc_strdup(argv[i]);
s->mask |= v;
return;
} else if (NULL == s) {
- s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
+ s = mandoc_calloc(1, sizeof(struct str) + sz + 1);
memcpy(s->key, cp, sz);
ohash_insert(htab, slot, s);
}
hash_halloc(size_t sz, void *arg)
{
- return(mandoc_calloc(sz, 1));
+ return(mandoc_calloc(1, sz));
}
static void *
-/* $Id: manpath.c,v 1.9 2014/04/20 16:44:44 schwarze Exp $ */
+/* $Id: manpath.c,v 1.10 2014/04/23 21:06:33 schwarze Exp $ */
/*
* Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
if (0 == strcmp(dirs->paths[i], dir))
return;
- dirs->paths = mandoc_realloc(dirs->paths,
- (dirs->sz + 1) * sizeof(char *));
+ dirs->paths = mandoc_reallocarray(dirs->paths,
+ dirs->sz + 1, sizeof(char *));
dirs->paths[dirs->sz++] = mandoc_strdup(cp);
}
-/* $Id: mansearch.c,v 1.25 2014/04/23 16:33:37 schwarze Exp $ */
+/* $Id: mansearch.c,v 1.26 2014/04/23 21:06:33 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
mp = ohash_next(&htab, &idx)) {
if (cur + 1 > maxres) {
maxres += 1024;
- *res = mandoc_realloc(*res,
- maxres * sizeof(struct manpage));
+ *res = mandoc_reallocarray(*res,
+ maxres, sizeof(struct manpage));
}
mpage = *res + cur;
mpage->form = mp->form;
hash_halloc(size_t sz, void *arg)
{
- return(mandoc_calloc(sz, 1));
+ return(mandoc_calloc(1, sz));
}
static void *
-/* $Id: mdoc_argv.c,v 1.49 2014/04/20 16:44:44 schwarze Exp $ */
+/* $Id: mdoc_argv.c,v 1.50 2014/04/23 21:06:33 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2012 Ingo Schwarze <schwarze@openbsd.org>
arg = *v = mandoc_calloc(1, sizeof(struct mdoc_arg));
arg->argc++;
- arg->argv = mandoc_realloc(arg->argv,
- arg->argc * sizeof(struct mdoc_argv));
+ arg->argv = mandoc_reallocarray(arg->argv,
+ arg->argc, sizeof(struct mdoc_argv));
memcpy(&arg->argv[(int)arg->argc - 1], &tmp,
sizeof(struct mdoc_argv));
break;
if (0 == v->sz % MULTI_STEP)
- v->value = mandoc_realloc(v->value,
- (v->sz + MULTI_STEP) * sizeof(char *));
+ v->value = mandoc_reallocarray(v->value,
+ v->sz + MULTI_STEP, sizeof(char *));
v->value[(int)v->sz] = mandoc_strdup(p);
}
-/* $Id: mdoc_validate.c,v 1.131 2014/04/23 16:07:06 schwarze Exp $ */
+/* $Id: mdoc_validate.c,v 1.132 2014/04/23 21:06:33 schwarze Exp $ */
/*
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
assert(n->args);
i = (int)(n->args->argc)++;
- n->args->argv = mandoc_realloc(n->args->argv,
- n->args->argc * sizeof(struct mdoc_argv));
+ n->args->argv = mandoc_reallocarray(n->args->argv,
+ n->args->argc, sizeof(struct mdoc_argv));
n->args->argv[i].arg = MDOC_Width;
n->args->argv[i].line = n->line;
*/
np->args->argv[j].sz = (size_t)mdoc->last->nchild;
- np->args->argv[j].value = mandoc_malloc(
- (size_t)mdoc->last->nchild * sizeof(char *));
+ np->args->argv[j].value = mandoc_reallocarray(NULL,
+ (size_t)mdoc->last->nchild, sizeof(char *));
mdoc->last->norm->Bl.ncols = np->args->argv[j].sz;
mdoc->last->norm->Bl.cols = (void *)np->args->argv[j].value;
-/* $Id: term.c,v 1.83 2014/04/20 16:44:44 schwarze Exp $ */
+/* $Id: term.c,v 1.84 2014/04/23 21:06:33 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
while (sz >= p->maxcols)
p->maxcols <<= 2;
- p->buf = mandoc_realloc(p->buf, sizeof(int) * p->maxcols);
+ p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
}
static void
-/* $Id: term_ps.c,v 1.24 2014/04/20 16:44:44 schwarze Exp $ */
+/* $Id: term_ps.c,v 1.25 2014/04/23 21:06:33 schwarze Exp $ */
/*
* Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
if ((obj - 1) >= p->ps->pdfobjsz) {
p->ps->pdfobjsz = obj + 128;
- p->ps->pdfobjs = realloc(p->ps->pdfobjs,
- p->ps->pdfobjsz * sizeof(size_t));
- if (NULL == p->ps->pdfobjs) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ p->ps->pdfobjs = mandoc_reallocarray(p->ps->pdfobjs,
+ p->ps->pdfobjsz, sizeof(size_t));
}
p->ps->pdfobjs[(int)obj - 1] = p->ps->pdfbytes;
sz = PS_BUFSLOP;
p->ps->psmargsz += sz;
-
- p->ps->psmarg = mandoc_realloc
- (p->ps->psmarg, p->ps->psmargsz);
+ p->ps->psmarg = mandoc_realloc(p->ps->psmarg, p->ps->psmargsz);
}