From 3a4ccea2afbaf5605faca23b46ab77d0efc353e6 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 20 Jul 2019 11:11:39 +0100 Subject: [PATCH] [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)); -- 2.39.5