From e81e60bc54a17129f6cb672bfd58094193955b78 Mon Sep 17 00:00:00 2001
From: Ray Gardner <raygard@gmail.com>
Date: Thu, 8 Aug 2024 17:47:40 -0600
Subject: [PATCH 2/7] Add awk_exit() function

ASAN test showed exiting toybox awk via exit() caused a memory leak, because
toybox did not have a chance to do its normal cleanup. Replace calls to exit()
with awk_exit(), which sets toys.exitval and calls xexit() in toybox, but
simply calls exit() in standalone wak.
---
 toys/pending/awk.c            |  9 +++++++--

diff --git a/toys/pending/awk.c b/toys/pending/awk.c
index dfbeb24..a0183fe 100644
--- a/toys/pending/awk.c
+++ b/toys/pending/awk.c
@@ -144,6 +144,11 @@ GLOBALS(
   } *zfiles, *cfile, *zstdout;
 )
 
+static void awk_exit(int status)
+{
+  toys.exitval = status;
+  xexit();
+}
 #ifdef __GNUC__
 #define ATTR_FALLTHROUGH_INTENDED __attribute__ ((fallthrough))
 #else
@@ -356,7 +361,7 @@ static void zzerr(char *format, ...)
   va_end(args);
   if (format[strlen(format)-1] != '\n') fputc('\n', stderr); // TEMP FIXME !!!
   fflush(stderr);
-  if (fatal_sw) exit(2);
+  if (fatal_sw) awk_exit(2);
         // Don't bump error count for warnings
   else if (!strstr(format, "arning")) TT.cgl.compile_error_count++;
 }
@@ -4486,7 +4491,7 @@ static void run(int optind, int argc, char **argv, char *sepstring,
   regfree(&TT.rx_last);
   free_literal_regex();
   close_file(0);    // close all files
-  if (status >= 0) exit(status);
+  if (status >= 0) awk_exit(status);
 }
 
 ////////////////////
-- 
2.43.0

