From a06fd6562409ed4ad8efcc54f57b249b6d183416 Mon Sep 17 00:00:00 2001 From: millert Date: Fri, 3 Jun 2022 19:46:09 +0000 Subject: [PATCH] Memory leak when assigning a string to some of the built-in variables. Allocated string erroneously marked DONTFREE. From Miguel Pineiro Jr. --- usr.bin/awk/FIXES | 13 +++++++++---- usr.bin/awk/main.c | 4 ++-- usr.bin/awk/tran.c | 17 ++--------------- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/usr.bin/awk/FIXES b/usr.bin/awk/FIXES index 77fa8227e51..e5ee5cfc803 100644 --- a/usr.bin/awk/FIXES +++ b/usr.bin/awk/FIXES @@ -1,4 +1,3 @@ -/* $OpenBSD: FIXES,v 1.45 2022/06/03 19:42:27 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -26,10 +25,16 @@ THIS SOFTWARE. This file lists all bug fixes, changes, etc., made since the AWK book was sent to the printers in August, 1987. + +May 23, 2022: + Memory leak when assigning a string to some of the built-in + variables. allocated string erroneously marked DONTFREE. + Thanks to Miguel Pineiro Jr. . + Mar 14, 2022: - The fulfillment of an assignment operand had been truncating its - entry in ARGV (since circa 1989). Thanks to Miguel Pineiro Jr. - . + Historic bug: command-line "name=value" assignment had been + truncating its entry in ARGV. (circa 1989) Thanks to + Miguel Pineiro Jr. . Mar 3, 2022: Fixed file management memory leak that appears to have been diff --git a/usr.bin/awk/main.c b/usr.bin/awk/main.c index e1aef4a6b30..6ce4ed58d4b 100644 --- a/usr.bin/awk/main.c +++ b/usr.bin/awk/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.53 2022/06/03 19:42:27 millert Exp $ */ +/* $OpenBSD: main.c,v 1.54 2022/06/03 19:46:09 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -23,7 +23,7 @@ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ****************************************************************/ -const char *version = "version 20220314"; +const char *version = "version 20220530"; #define DEBUG #include diff --git a/usr.bin/awk/tran.c b/usr.bin/awk/tran.c index 41acfc0fa39..6e8b159022c 100644 --- a/usr.bin/awk/tran.c +++ b/usr.bin/awk/tran.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tran.c,v 1.34 2021/11/02 15:29:41 millert Exp $ */ +/* $OpenBSD: tran.c,v 1.35 2022/06/03 19:46:09 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -71,18 +71,6 @@ Cell *literal0; extern Cell **fldtab; -static void -setfree(Cell *vp) -{ - if (&vp->sval == FS || &vp->sval == RS || - &vp->sval == OFS || &vp->sval == ORS || - &vp->sval == OFMT || &vp->sval == CONVFMT || - &vp->sval == FILENAME || &vp->sval == SUBSEP) - vp->tval |= DONTFREE; - else - vp->tval &= ~DONTFREE; -} - void syminit(void) /* initialize symbol table with builtin vars */ { literal0 = setsymtab("0", "0", 0.0, NUM|STR|CON|DONTFREE, symtab); @@ -378,10 +366,9 @@ char *setsval(Cell *vp, const char *s) /* set string val of a Cell */ t = s ? tostring(s) : tostring(""); /* in case it's self-assign */ if (freeable(vp)) xfree(vp->sval); - vp->tval &= ~(NUM|CONVC|CONVO); + vp->tval &= ~(NUM|DONTFREE|CONVC|CONVO); vp->tval |= STR; vp->fmt = NULL; - setfree(vp); DPRINTF("setsval %p: %s = \"%s (%p) \", t=%o r,f=%d,%d\n", (void*)vp, NN(vp->nval), t, (void*)t, vp->tval, donerec, donefld); vp->sval = t; -- 2.20.1