[PATCH ustream] ustream-openssl: fix bio memory leak

Eneas U de Queiroz cotequeiroz at gmail.com
Wed Dec 9 11:06:45 EST 2020


Using the patch by Pan Chen as inspiration, this avoids a memory leak by
using a global BIO_METHOD pointer that doesn't ordinarily need to be
freed.

CC: Pan Chen <serial115200 at outlook.com>

Signed-off-by: Eneas U de Queiroz <cotequeiroz at gmail.com>

---
Run-tested with a WRT-3200ACM, running uclient_fetch and uhttpd.
I have not run it with valgrind or any other debugger.

diff --git a/ustream-io-openssl.c b/ustream-io-openssl.c
index 606ed4a..26b3ed5 100644
--- a/ustream-io-openssl.c
+++ b/ustream-io-openssl.c
@@ -116,20 +116,23 @@ static long s_ustream_ctrl(BIO *b, int cmd, long num, void *ptr)
 	};
 }
 
+static BIO_METHOD *methods_ustream = NULL;
+
 static BIO *ustream_bio_new(struct ustream *s)
 {
 	BIO *bio;
 
-	BIO_METHOD *methods_ustream;
-
-	methods_ustream = BIO_meth_new(100 | BIO_TYPE_SOURCE_SINK, "ustream");
-	BIO_meth_set_write(methods_ustream, s_ustream_write);
-	BIO_meth_set_read(methods_ustream, s_ustream_read);
-	BIO_meth_set_puts(methods_ustream, s_ustream_puts);
-	BIO_meth_set_gets(methods_ustream, s_ustream_gets);
-	BIO_meth_set_ctrl(methods_ustream, s_ustream_ctrl);
-	BIO_meth_set_create(methods_ustream, s_ustream_new);
-	BIO_meth_set_destroy(methods_ustream, s_ustream_free);
+	if (methods_ustream == NULL) {
+		methods_ustream = BIO_meth_new(100 | BIO_TYPE_SOURCE_SINK,
+					       "ustream");
+		BIO_meth_set_write(methods_ustream, s_ustream_write);
+		BIO_meth_set_read(methods_ustream, s_ustream_read);
+		BIO_meth_set_puts(methods_ustream, s_ustream_puts);
+		BIO_meth_set_gets(methods_ustream, s_ustream_gets);
+		BIO_meth_set_ctrl(methods_ustream, s_ustream_ctrl);
+		BIO_meth_set_create(methods_ustream, s_ustream_new);
+		BIO_meth_set_destroy(methods_ustream, s_ustream_free);
+	}
 	bio = BIO_new(methods_ustream);
 	BIO_set_data(bio, s);
 



More information about the openwrt-devel mailing list