From 1b79df4e80c4135217ea56a20ab6c3dee5144b2d Mon Sep 17 00:00:00 2001 From: Matteo Bruni Date: Thu, 26 Jun 2014 19:52:31 +0200 Subject: [PATCH] wpp: Free some filename strings after processing an include (Valgrind). --- libs/wpp/ppl.l | 16 +++++++++++----- libs/wpp/wpp.c | 8 ++++++-- libs/wpp/wpp_private.h | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/libs/wpp/ppl.l b/libs/wpp/ppl.l index 9655e34af6a..0ce64c223fb 100644 --- a/libs/wpp/ppl.l +++ b/libs/wpp/ppl.l @@ -213,7 +213,7 @@ typedef struct bufferstackentry { pp_entry_t *define; /* Points to expanding define or NULL if handling includes */ int line_number; /* Line that we were handling */ int char_number; /* The current position on that line */ - const char *filename; /* Filename that we were handling */ + char *filename; /* Filename that we were handling */ int if_depth; /* How many #if:s deep to check matching #endif:s */ int ncontinuations; /* Remember the continuation state */ int should_pop; /* Set if we must pop the start-state on EOF */ @@ -1340,6 +1340,8 @@ static bufferstackentry_t *pop_buffer(void) bufferstack[bufferstackidx].define->expanding = 0; else { + includelogicentry_t *iep = NULL; + if(!bufferstack[bufferstackidx].should_pop) { wpp_callbacks->close(pp_status.file); @@ -1351,7 +1353,7 @@ static bufferstackentry_t *pop_buffer(void) pp_entry_t *ppp = pplookup(pp_incl_state.ppp); if(ppp) { - includelogicentry_t *iep = pp_xmalloc(sizeof(includelogicentry_t)); + iep = pp_xmalloc(sizeof(includelogicentry_t)); if(!iep) return NULL; @@ -1366,17 +1368,21 @@ static bufferstackentry_t *pop_buffer(void) if(pp_status.debug) fprintf(stderr, "pop_buffer: %s:%d: includelogic added, include_ppp='%s', file='%s'\n", bufferstack[bufferstackidx].filename, bufferstack[bufferstackidx].line_number, pp_incl_state.ppp, iep->filename); } - else - free(bufferstack[bufferstackidx].include_filename); } free(pp_incl_state.ppp); pp_incl_state = bufferstack[bufferstackidx].incl; } + if (bufferstack[bufferstackidx].include_filename) + { + free(pp_status.input); + pp_status.input = bufferstack[bufferstackidx].filename; + } pp_status.line_number = bufferstack[bufferstackidx].line_number; pp_status.char_number = bufferstack[bufferstackidx].char_number; - pp_status.input = bufferstack[bufferstackidx].filename; ncontinuations = bufferstack[bufferstackidx].ncontinuations; + if (!iep) + free(bufferstack[bufferstackidx].include_filename); } if(ppy_debug) diff --git a/libs/wpp/wpp.c b/libs/wpp/wpp.c index 66eb0261e4d..60b2d388581 100644 --- a/libs/wpp/wpp.c +++ b/libs/wpp/wpp.c @@ -206,7 +206,7 @@ int wpp_parse( const char *input, FILE *output ) return 2; } - pp_status.input = input; + pp_status.input = input ? pp_xstrdup(input) : NULL; ppy_out = output; pp_writestring("# 1 \"%s\" 1\n", input ? input : ""); @@ -215,7 +215,11 @@ int wpp_parse( const char *input, FILE *output ) /* If there were errors during processing, return an error code */ if (!ret && pp_status.state) ret = pp_status.state; - if (input) wpp_callbacks->close(pp_status.file); + if (input) + { + wpp_callbacks->close(pp_status.file); + free(pp_status.input); + } /* Clean if_stack, it could remain dirty on errors */ while (pp_get_if_depth()) pp_pop_if(); del_special_defines(); diff --git a/libs/wpp/wpp_private.h b/libs/wpp/wpp_private.h index 4e2aa5bffc4..caa52b9c563 100644 --- a/libs/wpp/wpp_private.h +++ b/libs/wpp/wpp_private.h @@ -228,7 +228,7 @@ void pp_internal_error(const char *file, int line, const char *s, ...) __attribu /* everything is in this structure to avoid polluting the global symbol space */ struct pp_status { - const char *input; /* current input file name */ + char *input; /* current input file name */ void *file; /* current input file descriptor */ int line_number; /* current line number */ int char_number; /* current char number in line */ -- 2.11.4.GIT