commit 3a4ccea: [Minor] Do not use alloca for huge allocations

Vsevolod Stakhov vsevolod at highsecure.ru
Sun Jul 21 08:56:03 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-07-20 11:11:39 +0100
URL: https://github.com/rspamd/rspamd/commit/3a4ccea2afbaf5605faca23b46ab77d0efc353e6

[Minor] Do not use alloca for huge allocations

---
 src/lua/lua_tcp.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/lua/lua_tcp.c b/src/lua/lua_tcp.c
index 371e72d5c..d5d497599 100644
--- a/src/lua/lua_tcp.c
+++ b/src/lua/lua_tcp.c
@@ -789,7 +789,14 @@ lua_tcp_write_helper (struct lua_tcp_cbdata *cbd)
 	niov = wh->iovlen;
 	remain = wh->pos;
 	/* We know that niov is small enough for that */
-	cur_iov = alloca (niov * sizeof (struct iovec));
+
+	if (niov < 1024) {
+		cur_iov = g_alloca (niov * sizeof (struct iovec));
+	}
+	else {
+		cur_iov = g_malloc0 (niov * sizeof (struct iovec));
+	}
+
 	memcpy (cur_iov, wh->iov, niov * sizeof (struct iovec));
 
 	for (i = 0; i < wh->iovlen && remain > 0; i++) {
@@ -822,6 +829,10 @@ lua_tcp_write_helper (struct lua_tcp_cbdata *cbd)
 		r = sendmsg (cbd->fd, &msg, flags);
 	}
 
+	if (niov >= 1024) {
+		g_free (cur_iov);
+	}
+
 	if (r == -1) {
 		lua_tcp_push_error (cbd, FALSE, "IO write error while trying to write %d "
 				"bytes: %s", (gint)remain, strerror (errno));


More information about the Commits mailing list