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
This commit is contained in:
Peter Johnson 2001-10-03 02:27:41 +00:00
parent 98e25fc1f6
commit b13f4e4f87
36 changed files with 276 additions and 313 deletions

View 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)

View 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)

View 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;

View 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;

View 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);

View 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;

View 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)

View 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;

View 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;

View 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

80
libyasm/xmalloc.c Normal file
View file

@ -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;
}

View 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);
}

View 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;

View 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;

View 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

View 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

View 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;
}

View 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

View 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;

View 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;

View 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;

View 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);

View 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;

View 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)

View 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;

View 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

View 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

View 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;
}

View 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;

View file

@ -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);
}

View 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;

View 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;

View 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

80
src/xmalloc.c Normal file
View file

@ -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;
}

View 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);
}

10
util.h
View 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