From 31ab84f2dff6285396d93d5a13433c7a75368022 Mon Sep 17 00:00:00 2001
From: Richard Manby <rmanby@gitlab.riscosopen.org>
Date: Fri, 21 Aug 1998 09:42:58 +0000
Subject: [PATCH] Fixed an RMA leak.

If a module written in C uses function getenv, buffer space
is allocated in the RMA. This buffer was never freed, leading
to RMA leakage when the module was 'RMKill'ed or 'RMReInit'ed.

Changes

 c.armsys
   added function _terminate_getenv to free the buffer

 c.stdlib
   added flag getenv_finalised to the exit_s structure

   initialised getenv_finalised in function _exit_init

   atexit tests getenv_finalised, calls _terminate_getenv

 s.version
   version string changed to 4.86 (27 Jul 1998)
---
 c/armsys  | 8 ++++++++
 c/stdlib  | 5 +++--
 s/version | 2 +-
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/c/armsys b/c/armsys
index 66f69aa..61ca6a2 100644
--- a/c/armsys
+++ b/c/armsys
@@ -370,6 +370,14 @@ char *getenv(const char *name)
        return _getenv_value;
 }
 
+void _terminate_getenv(void)
+{
+    if (_getenv_value)
+        free(_getenv_value);
+
+    _getenv_value = NULL;
+}
+
 #ifdef DDE
 #define DDEUtils_SetCLSize 0x42581
 #define DDEUtils_SetCL     0x42582
diff --git a/c/stdlib b/c/stdlib
index 1fa5063..f845ca5 100644
--- a/c/stdlib
+++ b/c/stdlib
@@ -103,7 +103,7 @@ static union { vprocp p; int i; } _exitvector[EXIT_LIMIT] = {};
        /* initialised so not in bss (or shared library trouble) */
 static struct {
     char number_of_exit_functions;
-    char alloc_finalised, io_finalised;
+    char alloc_finalised, io_finalised, getenv_finalised;
 } exit_s;
 
 void _exit_init()
@@ -117,7 +117,7 @@ void _exit_init()
             }
     } else
         exit_s.number_of_exit_functions = 0;
-    exit_s.alloc_finalised = 0; exit_s.io_finalised = 0;
+    exit_s.alloc_finalised = 0; exit_s.io_finalised = 0; exit_s.getenv_finalised = 0;
 }
 
 int atexit(vprocp func)
@@ -137,6 +137,7 @@ void _lib_shutdown()
         _call_client_0(fn);
     }
     /* ensure no recursion if finalisation fails */
+    if (!exit_s.getenv_finalised) { exit_s.getenv_finalised = 1; _terminate_getenv(); }
     if (!exit_s.alloc_finalised) { exit_s.alloc_finalised = 1; _terminate_user_alloc(); }
     if (!exit_s.io_finalised) { exit_s.io_finalised = 1; _terminateio(); }
 }
diff --git a/s/version b/s/version
index 62fce08..bcd26e5 100644
--- a/s/version
+++ b/s/version
@@ -12,5 +12,5 @@
 ; See the License for the specific language governing permissions and
 ; limitations under the License.
 ;
-        DCB     "4.85 (19 Dec 1996)"
+        DCB     "4.86 (27 Jul 1998)"
         END
-- 
GitLab