]> granicus.if.org Git - yasm/commitdiff
malloc->xmalloc, strdup->xstrdup, and calloc->xcalloc. The x* family performs
authorPeter Johnson <peter@tortall.net>
Wed, 3 Oct 2001 02:27:41 +0000 (02:27 -0000)
committerPeter Johnson <peter@tortall.net>
Wed, 3 Oct 2001 02:27:41 +0000 (02:27 -0000)
error checking.  Remove check for strdup() from configure, as we don't need it.

svn path=/trunk/yasm/; revision=253

36 files changed:
configure.ac
configure.in
frontends/yasm/yasm.c
libyasm/bytecode.c
libyasm/errwarn.c
libyasm/expr.c
libyasm/floatnum.c
libyasm/section.c
libyasm/symrec.c
libyasm/util.h
libyasm/xmalloc.c [new file with mode: 0644]
libyasm/xstrdup.c
modules/arch/x86/expr.c
modules/arch/x86/x86expr.c
modules/parsers/nasm/bison.y.in
modules/parsers/nasm/nasm-bison.y
modules/parsers/nasm/token.l.in
src/Makefile.am
src/arch/x86/expr.c
src/arch/x86/x86expr.c
src/bytecode.c
src/errwarn.c
src/expr.c
src/floatnum.c
src/main.c
src/parsers/nasm/bison.y.in
src/parsers/nasm/nasm-bison.y
src/parsers/nasm/token.l.in
src/section.c
src/strdup.c [deleted file]
src/symrec.c
src/ternary.c
src/util.h
src/xmalloc.c [new file with mode: 0644]
src/xstrdup.c
util.h

index 8340ae3c1ad919120bc1aafd0410a5bbb3d481a6..20c6bd52c0882941222d1159739492181691afe3 100644 (file)
@@ -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)
 
index 8340ae3c1ad919120bc1aafd0410a5bbb3d481a6..20c6bd52c0882941222d1159739492181691afe3 100644 (file)
@@ -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)
 
index fd7a4993d7d0c758b4a0d65d886f38d661f3c625..2c5d9a13d90ede77ff400ce1a6e5b9b8469fbf57 100644 (file)
@@ -117,7 +117,7 @@ main(int argc, char *argv[])
     if (!files_open)
     {
        in = stdin;
-       in_filename = strdup("<STDIN>");
+       in_filename = xstrdup("<STDIN>");
     }
 
     /* 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;
index f031635f894b253324a82058b61738f00f09950d..48d88268872af099f2c8417e226c4aa8bbd5f457 100644 (file)
@@ -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;
index f6a24d5455ac30a049444e476c8d767cdf8e2da3..14a8e86c0c977f322fdab672857721545466ce06 100644 (file)
@@ -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);
index 030b2c58190e3934504806e16e467f4bfec8b703..94740c00c2bce8af8c758aa68733ff4220163995 100644 (file)
@@ -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;
index b2303758c4052f997e01a0853a0f532b0cc47c31..3fa8ff76ef60d248d581b69c6ed66ed135b29d01 100644 (file)
@@ -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)
index fafa443a0901244d9620f88d263574ece605a998..7670e8b264027053eef5a00d3aaecf0b461793e6 100644 (file)
@@ -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;
index 2283f0d566f2f4645cdf25926b3fcfb407a3074a..cd3704e2c048235b4a7e82a73e1903d2e4a84314 100644 (file)
@@ -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;
index 2ad3237b635c029ab4a03f7c0d813b0917735096..558715f9de49f7315e72a27d31d5a2eed8b2a234 100644 (file)
@@ -26,9 +26,8 @@
 # include <stddef.h>
 #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 <sys/cdefs.h>
 #endif
diff --git a/libyasm/xmalloc.c b/libyasm/xmalloc.c
new file mode 100644 (file)
index 0000000..9a3cd35
--- /dev/null
@@ -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 <stdlib.h>
+#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;
+}
index 31f80c0006846185326fc772a2c9644aa4a94f0a..2efc8c8b444c52f08fd02b7fbdba6f0eede405e2 100644 (file)
@@ -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 <stdlib.h>
 # include <string.h>
 #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);
 }
index 030b2c58190e3934504806e16e467f4bfec8b703..94740c00c2bce8af8c758aa68733ff4220163995 100644 (file)
@@ -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;
index 030b2c58190e3934504806e16e467f4bfec8b703..94740c00c2bce8af8c758aa68733ff4220163995 100644 (file)
@@ -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;
index f0c66ad82ac859c57c187a3c24876d853f3297e8..34129f71c6e00007bf2d3f8086e6fa824a87f76b 100644 (file)
@@ -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
index f0c66ad82ac859c57c187a3c24876d853f3297e8..34129f71c6e00007bf2d3f8086e6fa824a87f76b 100644 (file)
@@ -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
index 43c95f593f27f941b594d5d6590681981cc81401..5329e1b4890575b1fb3bb1bb0254dee29c97d672 100644 (file)
@@ -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]
 
 <DIRECTIVE>[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 ']'. */
 <DIRECTIVE2>[!-@a-z\\^-`{|}~]+ {
-    yylval.str_val = strdup(yytext);
-    if (!yylval.str_val)
-       Fatal(FATAL_NOMEM);
+    yylval.str_val = xstrdup(yytext);
     return DIRECTIVE_VAL;
 }
 <DIRECTIVE>. {
@@ -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;
 }
 
index ac07f6918c2b16214ef66ed0e509df69bda45cfc..41a5782ec0272a819835c993629a9471d063b6b6 100644 (file)
@@ -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
index 030b2c58190e3934504806e16e467f4bfec8b703..94740c00c2bce8af8c758aa68733ff4220163995 100644 (file)
@@ -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;
index 030b2c58190e3934504806e16e467f4bfec8b703..94740c00c2bce8af8c758aa68733ff4220163995 100644 (file)
@@ -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;
index f031635f894b253324a82058b61738f00f09950d..48d88268872af099f2c8417e226c4aa8bbd5f457 100644 (file)
@@ -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;
index f6a24d5455ac30a049444e476c8d767cdf8e2da3..14a8e86c0c977f322fdab672857721545466ce06 100644 (file)
@@ -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);
index 030b2c58190e3934504806e16e467f4bfec8b703..94740c00c2bce8af8c758aa68733ff4220163995 100644 (file)
@@ -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;
index b2303758c4052f997e01a0853a0f532b0cc47c31..3fa8ff76ef60d248d581b69c6ed66ed135b29d01 100644 (file)
@@ -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)
index fd7a4993d7d0c758b4a0d65d886f38d661f3c625..2c5d9a13d90ede77ff400ce1a6e5b9b8469fbf57 100644 (file)
@@ -117,7 +117,7 @@ main(int argc, char *argv[])
     if (!files_open)
     {
        in = stdin;
-       in_filename = strdup("<STDIN>");
+       in_filename = xstrdup("<STDIN>");
     }
 
     /* 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;
index f0c66ad82ac859c57c187a3c24876d853f3297e8..34129f71c6e00007bf2d3f8086e6fa824a87f76b 100644 (file)
@@ -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
index f0c66ad82ac859c57c187a3c24876d853f3297e8..34129f71c6e00007bf2d3f8086e6fa824a87f76b 100644 (file)
@@ -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
index 43c95f593f27f941b594d5d6590681981cc81401..5329e1b4890575b1fb3bb1bb0254dee29c97d672 100644 (file)
@@ -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]
 
 <DIRECTIVE>[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 ']'. */
 <DIRECTIVE2>[!-@a-z\\^-`{|}~]+ {
-    yylval.str_val = strdup(yytext);
-    if (!yylval.str_val)
-       Fatal(FATAL_NOMEM);
+    yylval.str_val = xstrdup(yytext);
     return DIRECTIVE_VAL;
 }
 <DIRECTIVE>. {
@@ -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;
 }
 
index fafa443a0901244d9620f88d263574ece605a998..7670e8b264027053eef5a00d3aaecf0b461793e6 100644 (file)
@@ -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 (file)
index 31f80c0..0000000
+++ /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 <stddef.h>
-# include <stdlib.h>
-# include <string.h>
-#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);
-}
index 2283f0d566f2f4645cdf25926b3fcfb407a3074a..cd3704e2c048235b4a7e82a73e1903d2e4a84314 100644 (file)
@@ -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;
index d91b1332d92815411fffa50fc9a4813e202e26a1..b1320d545a9a68529a3549d75dd71074a25c75fa 100644 (file)
@@ -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;
index 2ad3237b635c029ab4a03f7c0d813b0917735096..558715f9de49f7315e72a27d31d5a2eed8b2a234 100644 (file)
@@ -26,9 +26,8 @@
 # include <stddef.h>
 #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 <sys/cdefs.h>
 #endif
diff --git a/src/xmalloc.c b/src/xmalloc.c
new file mode 100644 (file)
index 0000000..9a3cd35
--- /dev/null
@@ -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 <stdlib.h>
+#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;
+}
index 31f80c0006846185326fc772a2c9644aa4a94f0a..2efc8c8b444c52f08fd02b7fbdba6f0eede405e2 100644 (file)
@@ -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 <stdlib.h>
 # include <string.h>
 #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 2ad3237b635c029ab4a03f7c0d813b0917735096..558715f9de49f7315e72a27d31d5a2eed8b2a234 100644 (file)
--- a/util.h
+++ b/util.h
@@ -26,9 +26,8 @@
 # include <stddef.h>
 #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 <sys/cdefs.h>
 #endif