From 869f0128b4b71fc3978fdc66ebae802aa3537a95 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sun, 20 Jul 2025 05:02:53 -0400 Subject: usr: oasm: Free pointers on error paths Signed-off-by: Ian Moffett --- usr.bin/oasm/lex.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'usr.bin/oasm') diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c index afbe21d..194f09a 100644 --- a/usr.bin/oasm/lex.c +++ b/usr.bin/oasm/lex.c @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -65,6 +66,20 @@ lex_skippable(struct oasm_state *state, char c) return -1; } +/* + * For cleaning up allocated sources + * during error conditions + * + * @p: Memory to free + */ +static inline void +lex_try_free(void *p) +{ + if (p != NULL) { + free(p); + } +} + /* * Put back a token to grab later * @@ -231,7 +246,7 @@ token_reg(char *p) int lex_tok(struct oasm_state *state, struct oasm_token *ttp) { - char *p; + char *p = NULL; char c = ' '; int tmp; tt_t tok; @@ -269,6 +284,7 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp) if ((tok = token_arith(p)) != TT_UNKNOWN) { ttp->type = tok; ttp->raw = p; + lex_try_free(p); return 0; } @@ -277,6 +293,7 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp) ttp->is_reg = 1; ttp->type = tok; ttp->raw = p; + lex_try_free(p); return 0; } @@ -284,6 +301,7 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp) if ((tok = token_operand(p)) != TT_UNKNOWN) { ttp->type = tok; ttp->raw = p; + lex_try_free(p); return 0; } oasm_err("bad token \"%s\"\n", p); -- cgit v1.2.3