From abf388a5dfd9349d5f07fe0ac94bf826d2277b47 Mon Sep 17 00:00:00 2001 From: millert Date: Fri, 3 Jun 2022 19:40:56 +0000 Subject: [PATCH] Fix a file management memory leak that appears to have been there since the files array was first initialized with stdin, stdout, and stderr (circa 1992). From Miguel Pineiro Jr. --- usr.bin/awk/FIXES | 8 +++++++- usr.bin/awk/main.c | 4 ++-- usr.bin/awk/run.c | 11 +++++------ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/usr.bin/awk/FIXES b/usr.bin/awk/FIXES index bf18508ff19..17115123f0d 100644 --- a/usr.bin/awk/FIXES +++ b/usr.bin/awk/FIXES @@ -1,4 +1,4 @@ -/* $OpenBSD: FIXES,v 1.43 2022/01/27 16:58:37 millert Exp $ */ +/* $OpenBSD: FIXES,v 1.44 2022/06/03 19:40:56 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -26,6 +26,12 @@ THIS SOFTWARE. This file lists all bug fixes, changes, etc., made since the AWK book was sent to the printers in August, 1987. +Mar 3, 2022: + Fixed file management memory leak that appears to have been + there since the files array was first initialized with stdin, + stdout, and stderr (circa 1992). Thanks to Miguel Pineiro Jr. + . + December 8, 2021: The error handling in closefile and closeall was mangled. Long standing warnings had been made fatal and some fatal errors went diff --git a/usr.bin/awk/main.c b/usr.bin/awk/main.c index 9d4d1a48ebb..f3a3fe423dc 100644 --- a/usr.bin/awk/main.c +++ b/usr.bin/awk/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.51 2022/01/27 16:58:37 millert Exp $ */ +/* $OpenBSD: main.c,v 1.52 2022/06/03 19:40:56 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 20211208"; +const char *version = "version 20220303"; #define DEBUG #include diff --git a/usr.bin/awk/run.c b/usr.bin/awk/run.c index 1a86cec5fea..860911acc40 100644 --- a/usr.bin/awk/run.c +++ b/usr.bin/awk/run.c @@ -1,4 +1,4 @@ -/* $OpenBSD: run.c,v 1.71 2022/01/27 16:58:37 millert Exp $ */ +/* $OpenBSD: run.c,v 1.72 2022/06/03 19:40:56 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -1904,13 +1904,13 @@ static void stdinit(void) /* in case stdin, etc., are not constants */ if (files == NULL) FATAL("can't allocate file memory for %zu files", nfiles); files[0].fp = stdin; - files[0].fname = "/dev/stdin"; + files[0].fname = tostring("/dev/stdin"); files[0].mode = LT; files[1].fp = stdout; - files[1].fname = "/dev/stdout"; + files[1].fname = tostring("/dev/stdout"); files[1].mode = GT; files[2].fp = stderr; - files[2].fname = "/dev/stderr"; + files[2].fname = tostring("/dev/stderr"); files[2].mode = GT; } @@ -2014,8 +2014,7 @@ Cell *closefile(Node **a, int n) stat = fclose(files[i].fp) == EOF; if (stat) WARNING("i/o error occurred closing %s", files[i].fname); - if (i > 2) /* don't do /dev/std... */ - xfree(files[i].fname); + xfree(files[i].fname); files[i].fname = NULL; /* watch out for ref thru this */ files[i].fp = NULL; break; -- 2.20.1