parser: free unused token strings in token_accept()

If token_accept() is provided a null token pointer argument, the
previous "current" token is discarded.  If the token matched is a
symbol type, the token string (which will have been dynamically
allocated in yylex()) will be leaked.

Fix this by freeing the current token string if token pointer passed
is null.

The compiler warns when we attempt to free a pointer to constant
data, so change the type of the string pointer in the symbol
structure to be pointer to non-constant.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-5-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
This commit is contained in:
Alex Elder 2021-10-01 18:23:08 -05:00 committed by Bjorn Andersson
parent 23710a79b3
commit 405e4e2b01

View File

@ -32,7 +32,7 @@ enum {
struct token {
int id;
const char *str;
char *str;
unsigned num;
struct qmi_struct *qmi_struct;
};
@ -206,6 +206,8 @@ static int token_accept(int id, struct token *tok)
if (curr_token.id == id) {
if (tok)
*tok = curr_token;
else if (curr_token.str)
free(curr_token.str);
curr_token = yylex();
return 1;