From b7a2d2da3a0e001a4ce3dab6ca61813506cc99a8 Mon Sep 17 00:00:00 2001 From: Matteo Bruni Date: Mon, 28 Sep 2009 20:48:36 +0200 Subject: [PATCH] wpp: Handle memory allocation failures in tokens management code. --- libs/wpp/ppl.l | 2 +- libs/wpp/ppy.y | 56 ++++++++++++++++++++++++++++++++++++---------- libs/wpp/preproc.c | 29 +++++++++++++++++++++--- 3 files changed, 71 insertions(+), 16 deletions(-) diff --git a/libs/wpp/ppl.l b/libs/wpp/ppl.l index 1c83547cc57..5281e1c41be 100644 --- a/libs/wpp/ppl.l +++ b/libs/wpp/ppl.l @@ -437,7 +437,7 @@ includelogicentry_t *pp_includelogiclist = NULL; /* * Handle left side of #define */ -{cident}\( ppy_lval.cptr = pp_xstrdup(ppy_text); ppy_lval.cptr[ppy_leng-1] = '\0'; yy_pp_state(pp_macro); return tMACRO; +{cident}\( ppy_lval.cptr = pp_xstrdup(ppy_text); if(ppy_lval.cptr) ppy_lval.cptr[ppy_leng-1] = '\0'; yy_pp_state(pp_macro); return tMACRO; {cident} ppy_lval.cptr = pp_xstrdup(ppy_text); yy_pp_state(pp_define); return tDEFINE; {ws}+ ; \\\r?\n newline(0); diff --git a/libs/wpp/ppy.y b/libs/wpp/ppy.y index 2098ca377a3..505d685c2e9 100644 --- a/libs/wpp/ppy.y +++ b/libs/wpp/ppy.y @@ -295,11 +295,17 @@ preprocessor | tPRAGMA opt_text tNL { fprintf(ppy_out, "#pragma %s\n", $2 ? $2 : ""); free($2); } | tPPIDENT opt_text tNL { if(pp_status.pedantic) ppy_warning("#ident ignored (arg: '%s')", $2); free($2); } | tRCINCLUDE tRCINCLUDEPATH { - int nl=strlen($2) +3; - char *fn=pp_xmalloc(nl); - sprintf(fn,"\"%s\"",$2); - free($2); - pp_do_include(fn,1); + if($2) + { + int nl=strlen($2) +3; + char *fn=pp_xmalloc(nl); + if(fn) + { + sprintf(fn,"\"%s\"",$2); + pp_do_include(fn,1); + } + free($2); + } } | tRCINCLUDE tDQSTRING { pp_do_include($2,1); @@ -361,7 +367,7 @@ mtext : tLITERAL { $$ = new_mtext($1, 0, exp_text); } int mat = marg_index($1); if(mat >= 0) $$ = new_mtext(NULL, mat, exp_subst); - else + else if($1) $$ = new_mtext($1, 0, exp_text); } ; @@ -546,6 +552,8 @@ static int boolean(cval_t *v) static marg_t *new_marg(char *str, def_arg_t type) { marg_t *ma = pp_xmalloc(sizeof(marg_t)); + if(!ma) + return NULL; ma->arg = str; ma->type = type; ma->nnl = 0; @@ -554,16 +562,27 @@ static marg_t *new_marg(char *str, def_arg_t type) static marg_t *add_new_marg(char *str, def_arg_t type) { - marg_t *ma = new_marg(str, type); + marg_t **new_macro_args; + marg_t *ma; + if(!str) + return NULL; + new_macro_args = pp_xrealloc(macro_args, (nmacro_args+1) * sizeof(macro_args[0])); + if(!new_macro_args) + return NULL; + macro_args = new_macro_args; + ma = new_marg(str, type); + if(!ma) + return NULL; + macro_args[nmacro_args] = ma; nmacro_args++; - macro_args = pp_xrealloc(macro_args, nmacro_args * sizeof(macro_args[0])); - macro_args[nmacro_args-1] = ma; return ma; } static int marg_index(char *id) { int t; + if(!id) + return -1; for(t = 0; t < nmacro_args; t++) { if(!strcmp(id, macro_args[t]->arg)) @@ -658,9 +677,22 @@ static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp) static char *merge_text(char *s1, char *s2) { - int l1 = strlen(s1); - int l2 = strlen(s2); - s1 = pp_xrealloc(s1, l1+l2+1); + int l1; + int l2; + char *snew; + if(!s1) + return s2; + if(!s2) + return s1; + l1 = strlen(s1); + l2 = strlen(s2); + snew = pp_xrealloc(s1, l1+l2+1); + if(!snew) + { + free(s2); + return s1; + } + s1 = snew; memcpy(s1+l1, s2, l2+1); free(s2); return s1; diff --git a/libs/wpp/preproc.c b/libs/wpp/preproc.c index c071c56d74b..1d9aed65387 100644 --- a/libs/wpp/preproc.c +++ b/libs/wpp/preproc.c @@ -126,9 +126,12 @@ static int pphash(const char *str) pp_entry_t *pplookup(const char *ident) { - int idx = pphash(ident); + int idx; pp_entry_t *ppp; + if(!ident) + return NULL; + idx = pphash(ident); for(ppp = pp_def_state->defines[idx]; ppp; ppp = ppp->next) { if(!strcmp(ident, ppp->ident)) @@ -220,9 +223,12 @@ pp_entry_t *pp_add_define(char *def, char *text) { int len; char *cptr; - int idx = pphash(def); + int idx; pp_entry_t *ppp; + if(!def) + return NULL; + idx = pphash(def); if((ppp = pplookup(def)) != NULL) { if(pp_status.pedantic) @@ -230,11 +236,18 @@ pp_entry_t *pp_add_define(char *def, char *text) pp_del_define(def); } ppp = pp_xmalloc(sizeof(pp_entry_t)); + if(!ppp) + return NULL; memset( ppp, 0, sizeof(*ppp) ); ppp->ident = def; ppp->type = def_define; ppp->subst.text = text; ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : ""); + if(!ppp->filename) + { + free(ppp); + return NULL; + } ppp->linenumber = pp_status.input ? pp_status.line_number : 0; ppp->next = pp_def_state->defines[idx]; pp_def_state->defines[idx] = ppp; @@ -262,9 +275,12 @@ pp_entry_t *pp_add_define(char *def, char *text) pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp) { - int idx = pphash(id); + int idx; pp_entry_t *ppp; + if(!id) + return NULL; + idx = pphash(id); if((ppp = pplookup(id)) != NULL) { if(pp_status.pedantic) @@ -272,6 +288,8 @@ pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp) pp_del_define(id); } ppp = pp_xmalloc(sizeof(pp_entry_t)); + if(!ppp) + return NULL; memset( ppp, 0, sizeof(*ppp) ); ppp->ident = id; ppp->type = def_macro; @@ -279,6 +297,11 @@ pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp) ppp->nargs = nargs; ppp->subst.mtext= exp; ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : ""); + if(!ppp->filename) + { + free(ppp); + return NULL; + } ppp->linenumber = pp_status.input ? pp_status.line_number : 0; ppp->next = pp_def_state->defines[idx]; pp_def_state->defines[idx] = ppp;