From 02bb89dafd82d8ba85c988de0aaca1b82c486f37 Mon Sep 17 00:00:00 2001 From: Kristaps Dzonsons Date: Wed, 28 Oct 2009 19:21:59 +0000 Subject: Slow movement of internal allocations to fail completely. --- libmandoc.h | 7 +++++- mandoc.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- mdoc_argv.c | 47 ++++++++++++-------------------------- 3 files changed, 94 insertions(+), 35 deletions(-) diff --git a/libmandoc.h b/libmandoc.h index 1c78db26..017e4ed6 100644 --- a/libmandoc.h +++ b/libmandoc.h @@ -1,4 +1,4 @@ -/* $Id: libmandoc.h,v 1.1 2009/07/04 09:01:55 kristaps Exp $ */ +/* $Id: libmandoc.h,v 1.2 2009/10/28 19:21:59 kristaps Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons * @@ -20,6 +20,11 @@ __BEGIN_DECLS int mandoc_special(const char *); +void *mandoc_calloc(size_t, size_t); +char *mandoc_strdup(const char *); +void *mandoc_malloc(size_t); +void *mandoc_realloc(void *, size_t); +void *mandoc_reallocf(void *, size_t); __END_DECLS diff --git a/mandoc.c b/mandoc.c index 7839c37d..91a0fa92 100644 --- a/mandoc.c +++ b/mandoc.c @@ -1,4 +1,4 @@ -/* $Id: mandoc.c,v 1.3 2009/07/24 20:22:24 kristaps Exp $ */ +/* $Id: mandoc.c,v 1.4 2009/10/28 19:21:59 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include "libmandoc.h" @@ -103,3 +105,74 @@ mandoc_special(const char *p) return(*p == ']' ? c : 0); } + +void * +mandoc_calloc(size_t num, size_t size) +{ + void *ptr; + + ptr = calloc(num, size); + if (NULL == ptr) { + fprintf(stderr, "memory exhausted\n"); + exit(EXIT_FAILURE); + } + + return(ptr); +} + + +void * +mandoc_malloc(size_t size) +{ + void *ptr; + + ptr = malloc(size); + if (NULL == ptr) { + fprintf(stderr, "memory exhausted\n"); + exit(EXIT_FAILURE); + } + + return(ptr); +} + + +void * +mandoc_realloc(void *ptr, size_t size) +{ + + ptr = realloc(ptr, size); + if (NULL == ptr) { + fprintf(stderr, "memory exhausted\n"); + exit(EXIT_FAILURE); + } + + return(ptr); +} + + +void * +mandoc_reallocf(void *old_ptr, size_t size) /* FIXME: remove (not used) */ +{ + void *ptr; + + ptr = realloc(old_ptr, size); + if (NULL == ptr) + free(old_ptr); + + return(ptr); +} + + +char * +mandoc_strdup(const char *ptr) +{ + char *p; + + p = strdup(ptr); + if (NULL == p) { + fprintf(stderr, "memory exhausted\n"); + exit(EXIT_FAILURE); + } + + return(p); +} diff --git a/mdoc_argv.c b/mdoc_argv.c index 9ed4d68a..2505acb9 100644 --- a/mdoc_argv.c +++ b/mdoc_argv.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_argv.c,v 1.30 2009/10/24 05:52:13 kristaps Exp $ */ +/* $Id: mdoc_argv.c,v 1.31 2009/10/28 19:21:59 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -23,6 +23,7 @@ #include #include "libmdoc.h" +#include "libmandoc.h" /* * Routines to parse arguments of macros. Arguments follow the syntax @@ -266,23 +267,12 @@ mdoc_argv(struct mdoc *m, int line, int tok, if ( ! argv(m, line, &tmp, pos, buf)) return(ARGV_ERROR); - if (NULL == (arg = *v)) { - *v = calloc(1, sizeof(struct mdoc_arg)); - if (NULL == *v) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(ARGV_ERROR); - } - arg = *v; - } + if (NULL == (arg = *v)) + arg = mandoc_calloc(1, sizeof(struct mdoc_arg)); arg->argc++; - arg->argv = realloc(arg->argv, arg->argc * - sizeof(struct mdoc_argv)); - - if (NULL == arg->argv) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(ARGV_ERROR); - } + arg->argv = mandoc_realloc + (arg->argv, arg->argc * sizeof(struct mdoc_argv)); (void)memcpy(&arg->argv[(int)arg->argc - 1], &tmp, sizeof(struct mdoc_argv)); @@ -673,16 +663,11 @@ argv_multi(struct mdoc *m, int line, else if (ARGS_EOLN == c) break; - if (0 == v->sz % MULTI_STEP) { - v->value = realloc(v->value, + if (0 == v->sz % MULTI_STEP) + v->value = mandoc_realloc(v->value, (v->sz + MULTI_STEP) * sizeof(char *)); - if (NULL == v->value) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(ARGV_ERROR); - } - } - if (NULL == (v->value[(int)v->sz] = strdup(p))) - return(mdoc_nerr(m, m->last, EMALLOC)); + + v->value[(int)v->sz] = mandoc_strdup(p); } return(1); @@ -706,10 +691,8 @@ argv_opt_single(struct mdoc *m, int line, return(1); v->sz = 1; - if (NULL == (v->value = calloc(1, sizeof(char *)))) - return(mdoc_nerr(m, m->last, EMALLOC)); - if (NULL == (v->value[0] = strdup(p))) - return(mdoc_nerr(m, m->last, EMALLOC)); + v->value = mandoc_malloc(sizeof(char *)); + v->value[0] = mandoc_strdup(p); return(1); } @@ -734,10 +717,8 @@ argv_single(struct mdoc *m, int line, return(mdoc_perr(m, line, ppos, EARGVAL)); v->sz = 1; - if (NULL == (v->value = calloc(1, sizeof(char *)))) - return(mdoc_nerr(m, m->last, EMALLOC)); - if (NULL == (v->value[0] = strdup(p))) - return(mdoc_nerr(m, m->last, EMALLOC)); + v->value = mandoc_malloc(sizeof(char *)); + v->value[0] = mandoc_strdup(p); return(1); } -- cgit v1.2.3-56-ge451