From 46caa0d225918d4ae5cf4feca9b1829fcd1fd0db Mon Sep 17 00:00:00 2001 From: Matteo Bruni Date: Mon, 7 Feb 2011 15:15:13 +0100 Subject: [PATCH] wpp: Let pp_add_define take the responsibility of copying strings (Valgrind). --- libs/wpp/ppy.y | 2 +- libs/wpp/preproc.c | 37 ++++++++++++++++++++++--------------- libs/wpp/wpp.c | 10 +++++----- libs/wpp/wpp_private.h | 2 +- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/libs/wpp/ppy.y b/libs/wpp/ppy.y index b50e120e6e4..a66072d560b 100644 --- a/libs/wpp/ppy.y +++ b/libs/wpp/ppy.y @@ -275,7 +275,7 @@ preprocessor } } | tUNDEF tIDENT tNL { pp_del_define($2); free($2); } - | tDEFINE opt_text tNL { pp_add_define($1, $2); } + | tDEFINE opt_text tNL { pp_add_define($1, $2); free($1); free($2); } | tMACRO res_arg allmargs tMACROEND opt_mtexts tNL { pp_add_macro($1, macro_args, nmacro_args, $5); } diff --git a/libs/wpp/preproc.c b/libs/wpp/preproc.c index c88bff5a934..99934d69944 100644 --- a/libs/wpp/preproc.c +++ b/libs/wpp/preproc.c @@ -319,7 +319,7 @@ void pp_del_define(const char *name) printf("Deleted (%s, %d) <%s>\n", pp_status.input, pp_status.line_number, name); } -pp_entry_t *pp_add_define(char *def, char *text) +pp_entry_t *pp_add_define(const char *def, const char *text) { int len; char *cptr; @@ -339,38 +339,45 @@ pp_entry_t *pp_add_define(char *def, char *text) if(!ppp) return NULL; memset( ppp, 0, sizeof(*ppp) ); - ppp->ident = def; + ppp->ident = pp_xstrdup(def); + if(!ppp->ident) + goto error; ppp->type = def_define; - ppp->subst.text = text; + ppp->subst.text = text ? pp_xstrdup(text) : NULL; + if(text && !ppp->subst.text) + goto error; ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : ""); if(!ppp->filename) - { - free(ppp); - return NULL; - } + goto error; ppp->linenumber = pp_status.input ? pp_status.line_number : 0; ppp->next = pp_def_state->defines[idx]; pp_def_state->defines[idx] = ppp; if(ppp->next) ppp->next->prev = ppp; - if(text) + if(ppp->subst.text) { /* Strip trailing white space from subst text */ - len = strlen(text); - while(len && strchr(" \t\r\n", text[len-1])) + len = strlen(ppp->subst.text); + while(len && strchr(" \t\r\n", ppp->subst.text[len-1])) { - text[--len] = '\0'; + ppp->subst.text[--len] = '\0'; } /* Strip leading white space from subst text */ - for(cptr = text; *cptr && strchr(" \t\r", *cptr); cptr++) + for(cptr = ppp->subst.text; *cptr && strchr(" \t\r", *cptr); cptr++) ; - if(text != cptr) - memmove(text, cptr, strlen(cptr)+1); + if(ppp->subst.text != cptr) + memmove(ppp->subst.text, cptr, strlen(cptr)+1); } if(pp_status.debug) - printf("Added define (%s, %d) <%s> to <%s>\n", pp_status.input, pp_status.line_number, ppp->ident, text ? text : "(null)"); + printf("Added define (%s, %d) <%s> to <%s>\n", pp_status.input, pp_status.line_number, ppp->ident, ppp->subst.text ? ppp->subst.text : "(null)"); return ppp; + +error: + free(ppp->ident); + free(ppp->subst.text); + free(ppp); + return NULL; } pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp) diff --git a/libs/wpp/wpp.c b/libs/wpp/wpp.c index 41ac54fd52b..0c9c80852ea 100644 --- a/libs/wpp/wpp.c +++ b/libs/wpp/wpp.c @@ -45,7 +45,7 @@ static void add_cmdline_defines(void) for (def = cmdline_defines; def; def = def->next) { - if (def->value) pp_add_define( pp_xstrdup(def->name), pp_xstrdup(def->value) ); + if (def->value) pp_add_define( def->name, def->value ); } } @@ -56,16 +56,16 @@ static void add_special_defines(void) char buf[32]; strftime(buf, sizeof(buf), "\"%b %d %Y\"", localtime(&now)); - pp_add_define( pp_xstrdup("__DATE__"), pp_xstrdup(buf) ); + pp_add_define( "__DATE__", buf ); strftime(buf, sizeof(buf), "\"%H:%M:%S\"", localtime(&now)); - pp_add_define( pp_xstrdup("__TIME__"), pp_xstrdup(buf) ); + pp_add_define( "__TIME__", buf ); - ppp = pp_add_define( pp_xstrdup("__FILE__"), pp_xstrdup("") ); + ppp = pp_add_define( "__FILE__", "" ); if(ppp) ppp->type = def_special; - ppp = pp_add_define( pp_xstrdup("__LINE__"), pp_xstrdup("") ); + ppp = pp_add_define( "__LINE__", "" ); if(ppp) ppp->type = def_special; } diff --git a/libs/wpp/wpp_private.h b/libs/wpp/wpp_private.h index 615a450920b..288d709be36 100644 --- a/libs/wpp/wpp_private.h +++ b/libs/wpp/wpp_private.h @@ -204,7 +204,7 @@ char *pp_xstrdup(const char *str); pp_entry_t *pplookup(const char *ident); int pp_push_define_state(void); void pp_pop_define_state(void); -pp_entry_t *pp_add_define(char *def, char *text); +pp_entry_t *pp_add_define(const char *def, const char *text); pp_entry_t *pp_add_macro(char *ident, marg_t *args[], int nargs, mtext_t *exp); void pp_del_define(const char *name); void *pp_open_include(const char *name, const char *parent_name, char **newpath);