From c79051e5871a61034f37eb26f9199c1c3380bc61 Mon Sep 17 00:00:00 2001 From: Teemu Ollakka Date: Tue, 3 Mar 2020 16:50:48 +0200 Subject: MDEV-22271 Excessive stack memory usage due to WSREP_LOG - Made WSREP_LOG a function and moved the body out of header. - Reduced the stack allocated buffer size and implemented reprint into dynamically allocated buffer if stack buffer is not large enough to hold the message. --- sql/wsrep_mysqld.cc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'sql/wsrep_mysqld.cc') diff --git a/sql/wsrep_mysqld.cc b/sql/wsrep_mysqld.cc index 7665c5f1c6b..90896b2319e 100644 --- a/sql/wsrep_mysqld.cc +++ b/sql/wsrep_mysqld.cc @@ -267,6 +267,45 @@ char* wsrep_cluster_capabilities = NULL; wsp::Config_state *wsrep_config_state; +void WSREP_LOG(void (*fun)(const char* fmt, ...), const char* fmt, ...) +{ + /* Allocate short buffer from stack. If the vsnprintf() return value + indicates that the message was truncated, a new buffer will be allocated + dynamically and the message will be reprinted. */ + char msg[128] = {'\0'}; + va_list arglist; + va_start(arglist, fmt); + int n= vsnprintf(msg, sizeof(msg) - 1, fmt, arglist); + va_end(arglist); + if (n < 0) + { + sql_print_warning("WSREP: Printing message failed"); + } + else if (n < (int)sizeof(msg)) + { + fun("WSREP: %s", msg); + } + else + { + size_t dynbuf_size= std::max(n, 4096); + char* dynbuf= (char*) my_malloc(dynbuf_size, MYF(0)); + if (dynbuf) + { + va_start(arglist, fmt); + (void)vsnprintf(&dynbuf[0], dynbuf_size - 1, fmt, arglist); + va_end(arglist); + dynbuf[dynbuf_size - 1] = '\0'; + fun("WSREP: %s", &dynbuf[0]); + my_free(dynbuf); + } + else + { + /* Memory allocation for vector failed, print truncated message. */ + fun("WSREP: %s", msg); + } + } +} + wsrep_uuid_t local_uuid = WSREP_UUID_UNDEFINED; wsrep_seqno_t local_seqno = WSREP_SEQNO_UNDEFINED; -- cgit v1.2.1