From: Peter Johnson Date: Wed, 3 Oct 2001 02:27:41 +0000 (-0000) Subject: malloc->xmalloc, strdup->xstrdup, and calloc->xcalloc. The x* family performs X-Git-Tag: v0.1.0~277 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b13f4e4f8770b380446afa7d81dfb3d343d765f7;p=yasm malloc->xmalloc, strdup->xstrdup, and calloc->xcalloc. The x* family performs error checking. Remove check for strdup() from configure, as we don't need it. svn path=/trunk/yasm/; revision=253 --- diff --git a/configure.ac b/configure.ac index 8340ae3c..20c6bd52 100644 --- a/configure.ac +++ b/configure.ac @@ -117,7 +117,7 @@ AC_TYPE_SIZE_T AC_FUNC_VPRINTF AC_CHECK_FUNCS(memcpy toascii abort) AC_CHECK_FUNCS(strcasecmp stricmp strcmpi, break) -AC_REPLACE_FUNCS(strdup strsep strtoul) +AC_REPLACE_FUNCS(strsep strtoul) AC_CHECK_HEADERS(limits.h sys/queue.h sys/cdefs.h) diff --git a/configure.in b/configure.in index 8340ae3c..20c6bd52 100644 --- a/configure.in +++ b/configure.in @@ -117,7 +117,7 @@ AC_TYPE_SIZE_T AC_FUNC_VPRINTF AC_CHECK_FUNCS(memcpy toascii abort) AC_CHECK_FUNCS(strcasecmp stricmp strcmpi, break) -AC_REPLACE_FUNCS(strdup strsep strtoul) +AC_REPLACE_FUNCS(strsep strtoul) AC_CHECK_HEADERS(limits.h sys/queue.h sys/cdefs.h) diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c index fd7a4993..2c5d9a13 100644 --- a/frontends/yasm/yasm.c +++ b/frontends/yasm/yasm.c @@ -117,7 +117,7 @@ main(int argc, char *argv[]) if (!files_open) { in = stdin; - in_filename = strdup(""); + in_filename = xstrdup(""); } /* Get initial BITS setting from object format */ @@ -150,7 +150,7 @@ not_an_option_handler(char *param) ErrorNow(_("could not open file `%s'"), param); return 1; } - in_filename = strdup(param); + in_filename = xstrdup(param); files_open++; return 0; diff --git a/libyasm/bytecode.c b/libyasm/bytecode.c index f031635f..48d88268 100644 --- a/libyasm/bytecode.c +++ b/libyasm/bytecode.c @@ -57,10 +57,7 @@ static bytecode *bytecode_new_common(void); effaddr * effaddr_new_reg(unsigned long reg) { - effaddr *ea = malloc(sizeof(effaddr)); - - if (!ea) - Fatal(FATAL_NOMEM); + effaddr *ea = xmalloc(sizeof(effaddr)); ea->len = 0; ea->segment = 0; @@ -76,10 +73,7 @@ effaddr_new_reg(unsigned long reg) effaddr * effaddr_new_expr(expr *expr_ptr) { - effaddr *ea = malloc(sizeof(effaddr)); - - if (!ea) - Fatal(FATAL_NOMEM); + effaddr *ea = xmalloc(sizeof(effaddr)); ea->segment = 0; @@ -96,10 +90,7 @@ effaddr_new_expr(expr *expr_ptr) effaddr * effaddr_new_imm(immval *im_ptr, unsigned char im_len) { - effaddr *ea = malloc(sizeof(effaddr)); - - if (!ea) - Fatal(FATAL_NOMEM); + effaddr *ea = xmalloc(sizeof(effaddr)); ea->disp = im_ptr->val; if (im_ptr->len > im_len) @@ -267,14 +258,11 @@ SetOpcodeSel(jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel) static bytecode * bytecode_new_common(void) { - bytecode *bc = malloc(sizeof(bytecode)); - - if (!bc) - Fatal(FATAL_NOMEM); + bytecode *bc = xmalloc(sizeof(bytecode)); bc->len = 0; - bc->filename = strdup(in_filename); + bc->filename = xstrdup(in_filename); bc->lineno = line_number; bc->offset = 0; @@ -578,10 +566,7 @@ bytecodes_append(bytecodehead *headp, bytecode *bc) dataval * dataval_new_expr(expr *expn) { - dataval *retval = malloc(sizeof(dataval)); - - if (!retval) - Fatal(FATAL_NOMEM); + dataval *retval = xmalloc(sizeof(dataval)); retval->type = DV_EXPR; retval->data.expn = expn; @@ -592,10 +577,7 @@ dataval_new_expr(expr *expn) dataval * dataval_new_float(floatnum *flt) { - dataval *retval = malloc(sizeof(dataval)); - - if (!retval) - Fatal(FATAL_NOMEM); + dataval *retval = xmalloc(sizeof(dataval)); retval->type = DV_FLOAT; retval->data.flt = flt; @@ -606,10 +588,7 @@ dataval_new_float(floatnum *flt) dataval * dataval_new_string(char *str_val) { - dataval *retval = malloc(sizeof(dataval)); - - if (!retval) - Fatal(FATAL_NOMEM); + dataval *retval = xmalloc(sizeof(dataval)); retval->type = DV_STRING; retval->data.str_val = str_val; diff --git a/libyasm/errwarn.c b/libyasm/errwarn.c index f6a24d54..14a8e86c 100644 --- a/libyasm/errwarn.c +++ b/libyasm/errwarn.c @@ -162,9 +162,7 @@ Error(const char *fmt, ...) return; if (!errwarns) { - errwarns = malloc(sizeof(errwarnhead)); - if (!errwarns) - Fatal(FATAL_NOMEM); + errwarns = xmalloc(sizeof(errwarnhead)); STAILQ_INIT(errwarns); } @@ -172,14 +170,10 @@ Error(const char *fmt, ...) /* overwrite last (parser) error */ we = STAILQ_LAST(errwarns, errwarn_s, link); } else { - we = malloc(sizeof(errwarn)); - if (!we) - Fatal(FATAL_NOMEM); + we = xmalloc(sizeof(errwarn)); we->type = WE_ERROR; - we->filename = strdup(in_filename); - if (!we->filename) - Fatal(FATAL_NOMEM); + we->filename = xstrdup(in_filename); we->line = line_number; } @@ -210,23 +204,17 @@ Warning(const char *fmt, ...) previous_warning_line = line_number; - we = malloc(sizeof(errwarn)); - if (!we) - Fatal(FATAL_NOMEM); + we = xmalloc(sizeof(errwarn)); we->type = WE_WARNING; - we->filename = strdup(in_filename); - if (!we->filename) - Fatal(FATAL_NOMEM); + we->filename = xstrdup(in_filename); we->line = line_number; va_start(ap, fmt); vsprintf(we->msg, fmt, ap); va_end(ap); if (!errwarns) { - errwarns = malloc(sizeof(errwarnhead)); - if (!errwarns) - Fatal(FATAL_NOMEM); + errwarns = xmalloc(sizeof(errwarnhead)); STAILQ_INIT(errwarns); } STAILQ_INSERT_TAIL(errwarns, we, link); diff --git a/libyasm/expr.c b/libyasm/expr.c index 030b2c58..94740c00 100644 --- a/libyasm/expr.c +++ b/libyasm/expr.c @@ -50,9 +50,8 @@ expr_new(ExprType ltype, ExprItem right) { expr *ptr; - ptr = malloc(sizeof(expr)); - if (ptr == NULL) - Fatal(FATAL_NOMEM); + ptr = xmalloc(sizeof(expr)); + ptr->ltype = ltype; ptr->op = op; ptr->rtype = rtype; diff --git a/libyasm/floatnum.c b/libyasm/floatnum.c index b2303758..3fa8ff76 100644 --- a/libyasm/floatnum.c +++ b/libyasm/floatnum.c @@ -161,12 +161,8 @@ POT_Table_Init(void) int i; /* Allocate space for two POT tables */ - POT_TableN = malloc(14*sizeof(POT_Entry)); - if (!POT_TableN) - Fatal(FATAL_NOMEM); - POT_TableP = malloc(15*sizeof(POT_Entry)); /* note 1 extra for -1 */ - if (!POT_TableP) - Fatal(FATAL_NOMEM); + POT_TableN = xmalloc(14*sizeof(POT_Entry)); + POT_TableP = xmalloc(15*sizeof(POT_Entry)); /* note 1 extra for -1 */ /* Initialize entry[0..12] */ for (i=12; i>=0; i--) { @@ -299,9 +295,7 @@ floatnum_new(char *str) if (!POT_TableN) POT_Table_Init(); - flt = malloc(sizeof(floatnum)); - if (!flt) - Fatal(FATAL_NOMEM); + flt = xmalloc(sizeof(floatnum)); flt->mantissa = BitVector_Create(MANT_BITS, TRUE); if (!flt->mantissa) diff --git a/libyasm/section.c b/libyasm/section.c index fafa443a..7670e8b2 100644 --- a/libyasm/section.c +++ b/libyasm/section.c @@ -59,14 +59,12 @@ sections_initialize(sectionhead *headp, objfmt *of) STAILQ_INIT(headp); /* Add an initial "default" section to the list */ - s = calloc(1, sizeof(section)); - if (!s) - Fatal(FATAL_NOMEM); + s = xcalloc(1, sizeof(section)); STAILQ_INSERT_TAIL(headp, s, link); /* Initialize default section */ s->type = SECTION_GENERAL; - s->name = strdup(of->default_section_name); + s->name = xstrdup(of->default_section_name); bytecodes_initialize(&s->bc); return s; @@ -100,13 +98,11 @@ sections_switch(sectionhead *headp, objfmt *of, const char *name) } /* Okay, the name is valid; now allocate and initialize */ - s = calloc(1, sizeof(section)); - if (!s) - Fatal(FATAL_NOMEM); + s = xcalloc(1, sizeof(section)); STAILQ_INSERT_TAIL(headp, s, link); s->type = SECTION_GENERAL; - s->name = strdup(name); + s->name = xstrdup(name); bytecodes_initialize(&s->bc); return s; diff --git a/libyasm/symrec.c b/libyasm/symrec.c index 2283f0d5..cd3704e2 100644 --- a/libyasm/symrec.c +++ b/libyasm/symrec.c @@ -56,10 +56,7 @@ symrec_get_or_new(const char *name) { symrec *rec, *rec2; - rec = malloc(sizeof(symrec)); - if (!rec) - Fatal(FATAL_NOMEM); - + rec = xmalloc(sizeof(symrec)); rec2 = ternary_insert(&sym_table, name, rec, 0); if (rec2 != rec) { @@ -67,11 +64,9 @@ symrec_get_or_new(const char *name) return rec2; } - rec->name = strdup(name); - if (!rec->name) - Fatal(FATAL_NOMEM); + rec->name = xstrdup(name); rec->type = SYM_UNKNOWN; - rec->filename = strdup(in_filename); + rec->filename = xstrdup(in_filename); rec->line = line_number; rec->status = SYM_NOSTATUS; rec->visibility = SYM_LOCAL; diff --git a/libyasm/util.h b/libyasm/util.h index 2ad3237b..558715f9 100644 --- a/libyasm/util.h +++ b/libyasm/util.h @@ -26,9 +26,8 @@ # include #endif -#if !defined(HAVE_STRDUP) || defined(HAVE_GNU_C_LIBRARY) -char *strdup(const char *str); -#endif +/* strdup() implementation with error checking (using xmalloc). */ +char *xstrdup(const char *str); #if !defined(HAVE_STRSEP) || defined(HAVE_GNU_C_LIBRARY) char *strsep(char **stringp, const char *delim); @@ -67,6 +66,11 @@ int strncasecmp(const char *s1, const char *s2, size_t n); #include "ternary.h" +/* Error-checking memory allocation routines in xmalloc.c. */ +void *xmalloc(size_t size); +void *xcalloc(size_t nelem, size_t elsize); +void *xrealloc(void *oldmem, size_t size); + #ifdef HAVE_SYS_CDEFS_H # include #endif diff --git a/libyasm/xmalloc.c b/libyasm/xmalloc.c new file mode 100644 index 00000000..9a3cd351 --- /dev/null +++ b/libyasm/xmalloc.c @@ -0,0 +1,80 @@ +/* $IdPath$ + * Memory allocation routines with error checking. Idea from GNU libiberty. + * + * Copyright (C) 2001 Peter Johnson + * + * This file is part of YASM. + * + * YASM is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * YASM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "util.h" + +#ifdef STDC_HEADERS +# include +#endif + +#include "errwarn.h" + +RCSID("$IdPath$"); + +void * +xmalloc(size_t size) +{ + void *newmem; + + if (size == 0) + size = 1; + newmem = malloc(size); + if (!newmem) + Fatal(FATAL_NOMEM); + + return newmem; +} + +void * +xcalloc(size_t nelem, size_t elsize) +{ + void *newmem; + + if (nelem == 0 || elsize == 0) + nelem = elsize = 1; + + newmem = calloc(nelem, elsize); + if (!newmem) + Fatal(FATAL_NOMEM); + + return newmem; +} + +void * +xrealloc(void *oldmem, size_t size) +{ + void *newmem; + + if (size == 0) + size = 1; + if (!oldmem) + newmem = malloc(size); + else + newmem = realloc(oldmem, size); + if (!newmem) + Fatal(FATAL_NOMEM); + + return newmem; +} diff --git a/libyasm/xstrdup.c b/libyasm/xstrdup.c index 31f80c00..2efc8c8b 100644 --- a/libyasm/xstrdup.c +++ b/libyasm/xstrdup.c @@ -1,5 +1,5 @@ /* $IdPath$ - * strdup() implementation for systems that don't have it. + * strdup() implementation with error checking (using xmalloc). * * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -44,7 +44,6 @@ static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; # include # include #else -void *malloc(size_t); size_t strlen(const char *); # ifndef HAVE_MEMCPY void bcopy(const void *, void *, size_t); @@ -57,14 +56,13 @@ void memcpy(void *, const void *, size_t); RCSID("$IdPath$"); char * -strdup(const char *str) +xstrdup(const char *str) { size_t len; char *copy; len = strlen(str) + 1; - if ((copy = malloc(len)) == NULL) - return (NULL); + copy = xmalloc(len); memcpy(copy, str, len); return (copy); } diff --git a/modules/arch/x86/expr.c b/modules/arch/x86/expr.c index 030b2c58..94740c00 100644 --- a/modules/arch/x86/expr.c +++ b/modules/arch/x86/expr.c @@ -50,9 +50,8 @@ expr_new(ExprType ltype, ExprItem right) { expr *ptr; - ptr = malloc(sizeof(expr)); - if (ptr == NULL) - Fatal(FATAL_NOMEM); + ptr = xmalloc(sizeof(expr)); + ptr->ltype = ltype; ptr->op = op; ptr->rtype = rtype; diff --git a/modules/arch/x86/x86expr.c b/modules/arch/x86/x86expr.c index 030b2c58..94740c00 100644 --- a/modules/arch/x86/x86expr.c +++ b/modules/arch/x86/x86expr.c @@ -50,9 +50,8 @@ expr_new(ExprType ltype, ExprItem right) { expr *ptr; - ptr = malloc(sizeof(expr)); - if (ptr == NULL) - Fatal(FATAL_NOMEM); + ptr = xmalloc(sizeof(expr)); + ptr->ltype = ltype; ptr->op = op; ptr->rtype = rtype; diff --git a/modules/parsers/nasm/bison.y.in b/modules/parsers/nasm/bison.y.in index f0c66ad8..34129f71 100644 --- a/modules/parsers/nasm/bison.y.in +++ b/modules/parsers/nasm/bison.y.in @@ -184,7 +184,7 @@ label: label_id { label_id: ID { $$ = $1; - nasm_parser_locallabel_base = strdup($1); + nasm_parser_locallabel_base = xstrdup($1); } | SPECIAL_ID | LOCAL_ID diff --git a/modules/parsers/nasm/nasm-bison.y b/modules/parsers/nasm/nasm-bison.y index f0c66ad8..34129f71 100644 --- a/modules/parsers/nasm/nasm-bison.y +++ b/modules/parsers/nasm/nasm-bison.y @@ -184,7 +184,7 @@ label: label_id { label_id: ID { $$ = $1; - nasm_parser_locallabel_base = strdup($1); + nasm_parser_locallabel_base = xstrdup($1); } | SPECIAL_ID | LOCAL_ID diff --git a/modules/parsers/nasm/token.l.in b/modules/parsers/nasm/token.l.in index 43c95f59..5329e1b4 100644 --- a/modules/parsers/nasm/token.l.in +++ b/modules/parsers/nasm/token.l.in @@ -126,9 +126,7 @@ WS [ \t\r] int inch, count; char endch = yytext[0]; - strbuf = malloc(STRBUF_ALLOC_SIZE); - if (!strbuf) - Fatal(FATAL_NOMEM); + strbuf = xmalloc(STRBUF_ALLOC_SIZE); strbuf_size = STRBUF_ALLOC_SIZE; inch = input(); @@ -166,16 +164,12 @@ WS [ \t\r] [a-z]+ { BEGIN DIRECTIVE2; - yylval.str_val = strdup(yytext); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); + yylval.str_val = xstrdup(yytext); return DIRECTIVE_NAME; } /* everything printable except for ' ', '[' and ']'. */ [!-@a-z\\^-`{|}~]+ { - yylval.str_val = strdup(yytext); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); + yylval.str_val = xstrdup(yytext); return DIRECTIVE_VAL; } . { @@ -298,10 +292,7 @@ gs { yylval.int_val = 5; return REG_GS; } /* special non-local ..@label and labels like ..start */ $$|$|\.\.[a-z0-9_$#@~.?]+ { - yylval.str_val = strdup(yytext); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); - + yylval.str_val = xstrdup(yytext); return SPECIAL_ID; } @@ -309,14 +300,10 @@ $$|$|\.\.[a-z0-9_$#@~.?]+ { \.[a-z0-9_$#@~?][a-z0-9_$#@~.?]* { if (!nasm_parser_locallabel_base) { Warning(_("no non-local label before `%s'"), yytext); - yylval.str_val = strdup(yytext); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); + yylval.str_val = xstrdup(yytext); } else { - yylval.str_val = malloc(strlen(yytext) + - strlen(nasm_parser_locallabel_base) + 1); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); + yylval.str_val = xmalloc(strlen(yytext) + + strlen(nasm_parser_locallabel_base) + 1); strcpy(yylval.str_val, nasm_parser_locallabel_base); strcat(yylval.str_val, yytext); } @@ -329,10 +316,7 @@ $$|$|\.\.[a-z0-9_$#@~.?]+ { /* label */ [a-z_?][a-z0-9_$#@~.?]* { - yylval.str_val = strdup(yytext); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); - + yylval.str_val = xstrdup(yytext); return ID; } diff --git a/src/Makefile.am b/src/Makefile.am index ac07f691..41a5782e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -47,6 +47,8 @@ libyasm_a_SOURCES = \ ternary.h \ bitvect.c \ bitvect.h \ + xmalloc.c \ + xstrdup.c \ strcasecmp.c CFLAGS = @ANSI_CFLAGS@ @@ -54,5 +56,4 @@ CFLAGS = @ANSI_CFLAGS@ EXTRA_DIST = \ instrs.dat \ compat-queue.h \ - strdup.c \ strtoul.c diff --git a/src/arch/x86/expr.c b/src/arch/x86/expr.c index 030b2c58..94740c00 100644 --- a/src/arch/x86/expr.c +++ b/src/arch/x86/expr.c @@ -50,9 +50,8 @@ expr_new(ExprType ltype, ExprItem right) { expr *ptr; - ptr = malloc(sizeof(expr)); - if (ptr == NULL) - Fatal(FATAL_NOMEM); + ptr = xmalloc(sizeof(expr)); + ptr->ltype = ltype; ptr->op = op; ptr->rtype = rtype; diff --git a/src/arch/x86/x86expr.c b/src/arch/x86/x86expr.c index 030b2c58..94740c00 100644 --- a/src/arch/x86/x86expr.c +++ b/src/arch/x86/x86expr.c @@ -50,9 +50,8 @@ expr_new(ExprType ltype, ExprItem right) { expr *ptr; - ptr = malloc(sizeof(expr)); - if (ptr == NULL) - Fatal(FATAL_NOMEM); + ptr = xmalloc(sizeof(expr)); + ptr->ltype = ltype; ptr->op = op; ptr->rtype = rtype; diff --git a/src/bytecode.c b/src/bytecode.c index f031635f..48d88268 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -57,10 +57,7 @@ static bytecode *bytecode_new_common(void); effaddr * effaddr_new_reg(unsigned long reg) { - effaddr *ea = malloc(sizeof(effaddr)); - - if (!ea) - Fatal(FATAL_NOMEM); + effaddr *ea = xmalloc(sizeof(effaddr)); ea->len = 0; ea->segment = 0; @@ -76,10 +73,7 @@ effaddr_new_reg(unsigned long reg) effaddr * effaddr_new_expr(expr *expr_ptr) { - effaddr *ea = malloc(sizeof(effaddr)); - - if (!ea) - Fatal(FATAL_NOMEM); + effaddr *ea = xmalloc(sizeof(effaddr)); ea->segment = 0; @@ -96,10 +90,7 @@ effaddr_new_expr(expr *expr_ptr) effaddr * effaddr_new_imm(immval *im_ptr, unsigned char im_len) { - effaddr *ea = malloc(sizeof(effaddr)); - - if (!ea) - Fatal(FATAL_NOMEM); + effaddr *ea = xmalloc(sizeof(effaddr)); ea->disp = im_ptr->val; if (im_ptr->len > im_len) @@ -267,14 +258,11 @@ SetOpcodeSel(jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel) static bytecode * bytecode_new_common(void) { - bytecode *bc = malloc(sizeof(bytecode)); - - if (!bc) - Fatal(FATAL_NOMEM); + bytecode *bc = xmalloc(sizeof(bytecode)); bc->len = 0; - bc->filename = strdup(in_filename); + bc->filename = xstrdup(in_filename); bc->lineno = line_number; bc->offset = 0; @@ -578,10 +566,7 @@ bytecodes_append(bytecodehead *headp, bytecode *bc) dataval * dataval_new_expr(expr *expn) { - dataval *retval = malloc(sizeof(dataval)); - - if (!retval) - Fatal(FATAL_NOMEM); + dataval *retval = xmalloc(sizeof(dataval)); retval->type = DV_EXPR; retval->data.expn = expn; @@ -592,10 +577,7 @@ dataval_new_expr(expr *expn) dataval * dataval_new_float(floatnum *flt) { - dataval *retval = malloc(sizeof(dataval)); - - if (!retval) - Fatal(FATAL_NOMEM); + dataval *retval = xmalloc(sizeof(dataval)); retval->type = DV_FLOAT; retval->data.flt = flt; @@ -606,10 +588,7 @@ dataval_new_float(floatnum *flt) dataval * dataval_new_string(char *str_val) { - dataval *retval = malloc(sizeof(dataval)); - - if (!retval) - Fatal(FATAL_NOMEM); + dataval *retval = xmalloc(sizeof(dataval)); retval->type = DV_STRING; retval->data.str_val = str_val; diff --git a/src/errwarn.c b/src/errwarn.c index f6a24d54..14a8e86c 100644 --- a/src/errwarn.c +++ b/src/errwarn.c @@ -162,9 +162,7 @@ Error(const char *fmt, ...) return; if (!errwarns) { - errwarns = malloc(sizeof(errwarnhead)); - if (!errwarns) - Fatal(FATAL_NOMEM); + errwarns = xmalloc(sizeof(errwarnhead)); STAILQ_INIT(errwarns); } @@ -172,14 +170,10 @@ Error(const char *fmt, ...) /* overwrite last (parser) error */ we = STAILQ_LAST(errwarns, errwarn_s, link); } else { - we = malloc(sizeof(errwarn)); - if (!we) - Fatal(FATAL_NOMEM); + we = xmalloc(sizeof(errwarn)); we->type = WE_ERROR; - we->filename = strdup(in_filename); - if (!we->filename) - Fatal(FATAL_NOMEM); + we->filename = xstrdup(in_filename); we->line = line_number; } @@ -210,23 +204,17 @@ Warning(const char *fmt, ...) previous_warning_line = line_number; - we = malloc(sizeof(errwarn)); - if (!we) - Fatal(FATAL_NOMEM); + we = xmalloc(sizeof(errwarn)); we->type = WE_WARNING; - we->filename = strdup(in_filename); - if (!we->filename) - Fatal(FATAL_NOMEM); + we->filename = xstrdup(in_filename); we->line = line_number; va_start(ap, fmt); vsprintf(we->msg, fmt, ap); va_end(ap); if (!errwarns) { - errwarns = malloc(sizeof(errwarnhead)); - if (!errwarns) - Fatal(FATAL_NOMEM); + errwarns = xmalloc(sizeof(errwarnhead)); STAILQ_INIT(errwarns); } STAILQ_INSERT_TAIL(errwarns, we, link); diff --git a/src/expr.c b/src/expr.c index 030b2c58..94740c00 100644 --- a/src/expr.c +++ b/src/expr.c @@ -50,9 +50,8 @@ expr_new(ExprType ltype, ExprItem right) { expr *ptr; - ptr = malloc(sizeof(expr)); - if (ptr == NULL) - Fatal(FATAL_NOMEM); + ptr = xmalloc(sizeof(expr)); + ptr->ltype = ltype; ptr->op = op; ptr->rtype = rtype; diff --git a/src/floatnum.c b/src/floatnum.c index b2303758..3fa8ff76 100644 --- a/src/floatnum.c +++ b/src/floatnum.c @@ -161,12 +161,8 @@ POT_Table_Init(void) int i; /* Allocate space for two POT tables */ - POT_TableN = malloc(14*sizeof(POT_Entry)); - if (!POT_TableN) - Fatal(FATAL_NOMEM); - POT_TableP = malloc(15*sizeof(POT_Entry)); /* note 1 extra for -1 */ - if (!POT_TableP) - Fatal(FATAL_NOMEM); + POT_TableN = xmalloc(14*sizeof(POT_Entry)); + POT_TableP = xmalloc(15*sizeof(POT_Entry)); /* note 1 extra for -1 */ /* Initialize entry[0..12] */ for (i=12; i>=0; i--) { @@ -299,9 +295,7 @@ floatnum_new(char *str) if (!POT_TableN) POT_Table_Init(); - flt = malloc(sizeof(floatnum)); - if (!flt) - Fatal(FATAL_NOMEM); + flt = xmalloc(sizeof(floatnum)); flt->mantissa = BitVector_Create(MANT_BITS, TRUE); if (!flt->mantissa) diff --git a/src/main.c b/src/main.c index fd7a4993..2c5d9a13 100644 --- a/src/main.c +++ b/src/main.c @@ -117,7 +117,7 @@ main(int argc, char *argv[]) if (!files_open) { in = stdin; - in_filename = strdup(""); + in_filename = xstrdup(""); } /* Get initial BITS setting from object format */ @@ -150,7 +150,7 @@ not_an_option_handler(char *param) ErrorNow(_("could not open file `%s'"), param); return 1; } - in_filename = strdup(param); + in_filename = xstrdup(param); files_open++; return 0; diff --git a/src/parsers/nasm/bison.y.in b/src/parsers/nasm/bison.y.in index f0c66ad8..34129f71 100644 --- a/src/parsers/nasm/bison.y.in +++ b/src/parsers/nasm/bison.y.in @@ -184,7 +184,7 @@ label: label_id { label_id: ID { $$ = $1; - nasm_parser_locallabel_base = strdup($1); + nasm_parser_locallabel_base = xstrdup($1); } | SPECIAL_ID | LOCAL_ID diff --git a/src/parsers/nasm/nasm-bison.y b/src/parsers/nasm/nasm-bison.y index f0c66ad8..34129f71 100644 --- a/src/parsers/nasm/nasm-bison.y +++ b/src/parsers/nasm/nasm-bison.y @@ -184,7 +184,7 @@ label: label_id { label_id: ID { $$ = $1; - nasm_parser_locallabel_base = strdup($1); + nasm_parser_locallabel_base = xstrdup($1); } | SPECIAL_ID | LOCAL_ID diff --git a/src/parsers/nasm/token.l.in b/src/parsers/nasm/token.l.in index 43c95f59..5329e1b4 100644 --- a/src/parsers/nasm/token.l.in +++ b/src/parsers/nasm/token.l.in @@ -126,9 +126,7 @@ WS [ \t\r] int inch, count; char endch = yytext[0]; - strbuf = malloc(STRBUF_ALLOC_SIZE); - if (!strbuf) - Fatal(FATAL_NOMEM); + strbuf = xmalloc(STRBUF_ALLOC_SIZE); strbuf_size = STRBUF_ALLOC_SIZE; inch = input(); @@ -166,16 +164,12 @@ WS [ \t\r] [a-z]+ { BEGIN DIRECTIVE2; - yylval.str_val = strdup(yytext); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); + yylval.str_val = xstrdup(yytext); return DIRECTIVE_NAME; } /* everything printable except for ' ', '[' and ']'. */ [!-@a-z\\^-`{|}~]+ { - yylval.str_val = strdup(yytext); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); + yylval.str_val = xstrdup(yytext); return DIRECTIVE_VAL; } . { @@ -298,10 +292,7 @@ gs { yylval.int_val = 5; return REG_GS; } /* special non-local ..@label and labels like ..start */ $$|$|\.\.[a-z0-9_$#@~.?]+ { - yylval.str_val = strdup(yytext); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); - + yylval.str_val = xstrdup(yytext); return SPECIAL_ID; } @@ -309,14 +300,10 @@ $$|$|\.\.[a-z0-9_$#@~.?]+ { \.[a-z0-9_$#@~?][a-z0-9_$#@~.?]* { if (!nasm_parser_locallabel_base) { Warning(_("no non-local label before `%s'"), yytext); - yylval.str_val = strdup(yytext); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); + yylval.str_val = xstrdup(yytext); } else { - yylval.str_val = malloc(strlen(yytext) + - strlen(nasm_parser_locallabel_base) + 1); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); + yylval.str_val = xmalloc(strlen(yytext) + + strlen(nasm_parser_locallabel_base) + 1); strcpy(yylval.str_val, nasm_parser_locallabel_base); strcat(yylval.str_val, yytext); } @@ -329,10 +316,7 @@ $$|$|\.\.[a-z0-9_$#@~.?]+ { /* label */ [a-z_?][a-z0-9_$#@~.?]* { - yylval.str_val = strdup(yytext); - if (!yylval.str_val) - Fatal(FATAL_NOMEM); - + yylval.str_val = xstrdup(yytext); return ID; } diff --git a/src/section.c b/src/section.c index fafa443a..7670e8b2 100644 --- a/src/section.c +++ b/src/section.c @@ -59,14 +59,12 @@ sections_initialize(sectionhead *headp, objfmt *of) STAILQ_INIT(headp); /* Add an initial "default" section to the list */ - s = calloc(1, sizeof(section)); - if (!s) - Fatal(FATAL_NOMEM); + s = xcalloc(1, sizeof(section)); STAILQ_INSERT_TAIL(headp, s, link); /* Initialize default section */ s->type = SECTION_GENERAL; - s->name = strdup(of->default_section_name); + s->name = xstrdup(of->default_section_name); bytecodes_initialize(&s->bc); return s; @@ -100,13 +98,11 @@ sections_switch(sectionhead *headp, objfmt *of, const char *name) } /* Okay, the name is valid; now allocate and initialize */ - s = calloc(1, sizeof(section)); - if (!s) - Fatal(FATAL_NOMEM); + s = xcalloc(1, sizeof(section)); STAILQ_INSERT_TAIL(headp, s, link); s->type = SECTION_GENERAL; - s->name = strdup(name); + s->name = xstrdup(name); bytecodes_initialize(&s->bc); return s; diff --git a/src/strdup.c b/src/strdup.c deleted file mode 100644 index 31f80c00..00000000 --- a/src/strdup.c +++ /dev/null @@ -1,70 +0,0 @@ -/* $IdPath$ - * strdup() implementation for systems that don't have it. - * - * Copyright (c) 1988, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; -#endif /* LIBC_SCCS and not lint */ - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include "util.h" - -#ifdef STDC_HEADERS -# include -# include -# include -#else -void *malloc(size_t); -size_t strlen(const char *); -# ifndef HAVE_MEMCPY -void bcopy(const void *, void *, size_t); -# define memcpy(d, s, n) bcopy((s), (d), (n)) -# else -void memcpy(void *, const void *, size_t); -# endif -#endif - -RCSID("$IdPath$"); - -char * -strdup(const char *str) -{ - size_t len; - char *copy; - - len = strlen(str) + 1; - if ((copy = malloc(len)) == NULL) - return (NULL); - memcpy(copy, str, len); - return (copy); -} diff --git a/src/symrec.c b/src/symrec.c index 2283f0d5..cd3704e2 100644 --- a/src/symrec.c +++ b/src/symrec.c @@ -56,10 +56,7 @@ symrec_get_or_new(const char *name) { symrec *rec, *rec2; - rec = malloc(sizeof(symrec)); - if (!rec) - Fatal(FATAL_NOMEM); - + rec = xmalloc(sizeof(symrec)); rec2 = ternary_insert(&sym_table, name, rec, 0); if (rec2 != rec) { @@ -67,11 +64,9 @@ symrec_get_or_new(const char *name) return rec2; } - rec->name = strdup(name); - if (!rec->name) - Fatal(FATAL_NOMEM); + rec->name = xstrdup(name); rec->type = SYM_UNKNOWN; - rec->filename = strdup(in_filename); + rec->filename = xstrdup(in_filename); rec->line = line_number; rec->status = SYM_NOSTATUS; rec->visibility = SYM_LOCAL; diff --git a/src/ternary.c b/src/ternary.c index d91b1332..b1320d54 100644 --- a/src/ternary.c +++ b/src/ternary.c @@ -79,9 +79,7 @@ ternary_insert (ternary_tree * root, const char *s, void *data, int replace) for (;;) { /* Allocate the memory for the node, and fill it in */ - *pcurr = (ternary_tree) malloc (sizeof (ternary_node)); - if (!*pcurr) - Fatal(FATAL_NOMEM); + *pcurr = (ternary_tree) xmalloc (sizeof (ternary_node)); curr = *pcurr; curr->splitchar = *s; curr->lokid = curr->hikid = curr->eqkid = 0; diff --git a/src/util.h b/src/util.h index 2ad3237b..558715f9 100644 --- a/src/util.h +++ b/src/util.h @@ -26,9 +26,8 @@ # include #endif -#if !defined(HAVE_STRDUP) || defined(HAVE_GNU_C_LIBRARY) -char *strdup(const char *str); -#endif +/* strdup() implementation with error checking (using xmalloc). */ +char *xstrdup(const char *str); #if !defined(HAVE_STRSEP) || defined(HAVE_GNU_C_LIBRARY) char *strsep(char **stringp, const char *delim); @@ -67,6 +66,11 @@ int strncasecmp(const char *s1, const char *s2, size_t n); #include "ternary.h" +/* Error-checking memory allocation routines in xmalloc.c. */ +void *xmalloc(size_t size); +void *xcalloc(size_t nelem, size_t elsize); +void *xrealloc(void *oldmem, size_t size); + #ifdef HAVE_SYS_CDEFS_H # include #endif diff --git a/src/xmalloc.c b/src/xmalloc.c new file mode 100644 index 00000000..9a3cd351 --- /dev/null +++ b/src/xmalloc.c @@ -0,0 +1,80 @@ +/* $IdPath$ + * Memory allocation routines with error checking. Idea from GNU libiberty. + * + * Copyright (C) 2001 Peter Johnson + * + * This file is part of YASM. + * + * YASM is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * YASM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "util.h" + +#ifdef STDC_HEADERS +# include +#endif + +#include "errwarn.h" + +RCSID("$IdPath$"); + +void * +xmalloc(size_t size) +{ + void *newmem; + + if (size == 0) + size = 1; + newmem = malloc(size); + if (!newmem) + Fatal(FATAL_NOMEM); + + return newmem; +} + +void * +xcalloc(size_t nelem, size_t elsize) +{ + void *newmem; + + if (nelem == 0 || elsize == 0) + nelem = elsize = 1; + + newmem = calloc(nelem, elsize); + if (!newmem) + Fatal(FATAL_NOMEM); + + return newmem; +} + +void * +xrealloc(void *oldmem, size_t size) +{ + void *newmem; + + if (size == 0) + size = 1; + if (!oldmem) + newmem = malloc(size); + else + newmem = realloc(oldmem, size); + if (!newmem) + Fatal(FATAL_NOMEM); + + return newmem; +} diff --git a/src/xstrdup.c b/src/xstrdup.c index 31f80c00..2efc8c8b 100644 --- a/src/xstrdup.c +++ b/src/xstrdup.c @@ -1,5 +1,5 @@ /* $IdPath$ - * strdup() implementation for systems that don't have it. + * strdup() implementation with error checking (using xmalloc). * * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -44,7 +44,6 @@ static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; # include # include #else -void *malloc(size_t); size_t strlen(const char *); # ifndef HAVE_MEMCPY void bcopy(const void *, void *, size_t); @@ -57,14 +56,13 @@ void memcpy(void *, const void *, size_t); RCSID("$IdPath$"); char * -strdup(const char *str) +xstrdup(const char *str) { size_t len; char *copy; len = strlen(str) + 1; - if ((copy = malloc(len)) == NULL) - return (NULL); + copy = xmalloc(len); memcpy(copy, str, len); return (copy); } diff --git a/util.h b/util.h index 2ad3237b..558715f9 100644 --- a/util.h +++ b/util.h @@ -26,9 +26,8 @@ # include #endif -#if !defined(HAVE_STRDUP) || defined(HAVE_GNU_C_LIBRARY) -char *strdup(const char *str); -#endif +/* strdup() implementation with error checking (using xmalloc). */ +char *xstrdup(const char *str); #if !defined(HAVE_STRSEP) || defined(HAVE_GNU_C_LIBRARY) char *strsep(char **stringp, const char *delim); @@ -67,6 +66,11 @@ int strncasecmp(const char *s1, const char *s2, size_t n); #include "ternary.h" +/* Error-checking memory allocation routines in xmalloc.c. */ +void *xmalloc(size_t size); +void *xcalloc(size_t nelem, size_t elsize); +void *xrealloc(void *oldmem, size_t size); + #ifdef HAVE_SYS_CDEFS_H # include #endif