From b0ebd5737e903b48060a93fc29908b62b1bfd74e Mon Sep 17 00:00:00 2001 From: Kuang-che Wu Date: Tue, 26 Oct 2021 15:45:06 +0800 Subject: [PATCH] fix StrStream memory leak ISclose() doesn't free memory if a stream's function pointer, close, is NULL. However, str_stream's close pointer is always NULL and thus leaks memory. --- istream.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/istream.c b/istream.c index 374eace..5e432c9 100644 --- a/istream.c +++ b/istream.c @@ -185,12 +185,16 @@ int ISclose(InputStream stream) { MySignalHandler(*prevtrap) (); - if (stream == NULL || stream->base.close == NULL || - stream->base.type & IST_UNCLOSE) - return -1; - prevtrap = mySignal(SIGINT, SIG_IGN); - stream->base.close (stream->base.handle); - mySignal(SIGINT, prevtrap); + if (stream == NULL) + return -1; + if (stream->base.close != NULL) { + if (stream->base.type & IST_UNCLOSE) { + return -1; + } + prevtrap = mySignal(SIGINT, SIG_IGN); + stream->base.close (stream->base.handle); + mySignal(SIGINT, prevtrap); + } xfree(stream->base.stream.buf); xfree(stream); return 0;