logo

utils-std

Collection of commonly available Unix tools
commit: a8df527cb6012b9039c9bf9cfc0e3c55309b0b40
parent a4d1c458191481817f9fca1b5887721def2b9b49
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 26 May 2024 11:47:58 +0200

cmd/expr: Reduce memory leaks

One which still happens is on syntax errors, which I don't think
can be plugged due to lack of proper arguments in yyerror().

Diffstat:

Mcmd/expr.y18++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/cmd/expr.y b/cmd/expr.y @@ -126,7 +126,7 @@ make_integer(intmax_t i) vp = (struct val *)malloc(sizeof(*vp)); if (vp == NULL) - errx(ERR_EXIT, "malloc() failed"); + errx(ERR_EXIT, "malloc(vp) failed"); vp->type = integer; vp->u.i = i; @@ -139,8 +139,11 @@ make_str(const char *s) struct val *vp; vp = (struct val *)malloc(sizeof(*vp)); - if (vp == NULL || ((vp->u.s = strdup(s)) == NULL)) - errx(ERR_EXIT, "malloc() failed"); + if (vp == NULL) + errx(ERR_EXIT, "malloc(vp) failed"); + + if ((vp->u.s = strdup(s)) == NULL) + errx(ERR_EXIT, "strdup(s) failed"); if (is_integer(s)) vp->type = numeric_string; @@ -155,6 +158,9 @@ free_value(struct val *vp) { if (vp->type == string || vp->type == numeric_string) free(vp->u.s); + + if (vp->type == string || vp->type == numeric_string || vp->type == integer) + free(vp); } int @@ -298,7 +304,11 @@ main(int argc, char *argv[]) else printf("%s\n", result->u.s); - return (is_zero_or_null(result)); + int err = is_zero_or_null(result); + + free_value(result); + + return err; } int