error checking. Remove check for strdup() from configure, as we don't need it.
svn path=/trunk/yasm/; revision=253
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)
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)
if (!files_open)
{
in = stdin;
- in_filename = strdup("<STDIN>");
+ in_filename = xstrdup("<STDIN>");
}
/* Get initial BITS setting from object format */
ErrorNow(_("could not open file `%s'"), param);
return 1;
}
- in_filename = strdup(param);
+ in_filename = xstrdup(param);
files_open++;
return 0;
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;
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;
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)
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;
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;
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;
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;
return;
if (!errwarns) {
- errwarns = malloc(sizeof(errwarnhead));
- if (!errwarns)
- Fatal(FATAL_NOMEM);
+ errwarns = xmalloc(sizeof(errwarnhead));
STAILQ_INIT(errwarns);
}
/* 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;
}
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);
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;
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--) {
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)
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;
}
/* 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;
{
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) {
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;
# 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);
#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
--- /dev/null
+/* $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;
+}
/* $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.
# 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);
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);
}
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;
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;
label_id: ID {
$$ = $1;
- nasm_parser_locallabel_base = strdup($1);
+ nasm_parser_locallabel_base = xstrdup($1);
}
| SPECIAL_ID
| LOCAL_ID
label_id: ID {
$$ = $1;
- nasm_parser_locallabel_base = strdup($1);
+ nasm_parser_locallabel_base = xstrdup($1);
}
| SPECIAL_ID
| LOCAL_ID
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();
<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>. {
/* 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;
}
\.[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);
}
/* 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;
}
ternary.h \
bitvect.c \
bitvect.h \
+ xmalloc.c \
+ xstrdup.c \
strcasecmp.c
CFLAGS = @ANSI_CFLAGS@
EXTRA_DIST = \
instrs.dat \
compat-queue.h \
- strdup.c \
strtoul.c
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;
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;
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;
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;
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)
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;
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;
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;
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;
return;
if (!errwarns) {
- errwarns = malloc(sizeof(errwarnhead));
- if (!errwarns)
- Fatal(FATAL_NOMEM);
+ errwarns = xmalloc(sizeof(errwarnhead));
STAILQ_INIT(errwarns);
}
/* 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;
}
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);
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;
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--) {
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)
if (!files_open)
{
in = stdin;
- in_filename = strdup("<STDIN>");
+ in_filename = xstrdup("<STDIN>");
}
/* Get initial BITS setting from object format */
ErrorNow(_("could not open file `%s'"), param);
return 1;
}
- in_filename = strdup(param);
+ in_filename = xstrdup(param);
files_open++;
return 0;
label_id: ID {
$$ = $1;
- nasm_parser_locallabel_base = strdup($1);
+ nasm_parser_locallabel_base = xstrdup($1);
}
| SPECIAL_ID
| LOCAL_ID
label_id: ID {
$$ = $1;
- nasm_parser_locallabel_base = strdup($1);
+ nasm_parser_locallabel_base = xstrdup($1);
}
| SPECIAL_ID
| LOCAL_ID
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();
<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>. {
/* 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;
}
\.[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);
}
/* 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;
}
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;
}
/* 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;
+++ /dev/null
-/* $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);
-}
{
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) {
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;
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;
# 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);
#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
--- /dev/null
+/* $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;
+}
/* $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.
# 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);
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);
}
# 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);
#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