From 9ed2f9a27a79d723fe09afebf833ca17d02bc8b8 Mon Sep 17 00:00:00 2001
From: Alex Shinn <ashinn@users.noreply.github.com>
Date: Fri, 23 Nov 2012 11:17:08 +0900
Subject: [PATCH] Fixing memory leak in string ports found by Lorenzo. In the
 future this will be simplified by dropping support for C string streams and
 never using malloc for buffers.

---
 sexp.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/sexp.c b/sexp.c
index 000e620c..d061e912 100644
--- a/sexp.c
+++ b/sexp.c
@@ -140,22 +140,16 @@ sexp sexp_finalize_port (sexp ctx, sexp self, sexp_sint_t n, sexp port) {
       }
     }
 #endif
-    if (sexp_port_stream(port) && ! sexp_port_no_closep(port)) {
+    if (sexp_port_stream(port) && ! sexp_port_no_closep(port))
       /* close the stream */
       fclose(sexp_port_stream(port));
       /* free the buffer if allocated */
-      if (sexp_port_buf(port)
-          && (sexp_oportp(port)
+    if (sexp_port_buf(port) && sexp_oportp(port)
 #if !SEXP_USE_STRING_STREAMS
-              || (sexp_iportp(port) && sexp_truep(sexp_port_cookie(port)))
+        && !sexp_port_customp(port)
 #endif
-              )
-#if SEXP_USE_STRING_STREAMS
-          && !sexp_port_customp(port)
-#endif
-          )
-        free(sexp_port_buf(port));
-    }
+        )
+      free(sexp_port_buf(port));
   }
   return res;
 }