From 379d83a4a5c56ba46b96f985e2198a3f18391259 Mon Sep 17 00:00:00 2001 From: Michael Ortmann <41313082+michaelortmann@users.noreply.github.com> Date: Thu, 20 Feb 2025 04:14:55 +0100 Subject: [PATCH 1/3] Enhance memory usage regarding log table Add MIN() and MAX() to eggdrop.h --- src/chanprog.c | 8 ++++++++ src/eggdrop.h | 10 +++++++++- src/misc.c | 31 +++++++++++++++---------------- src/mod/server.mod/servmsg.c | 3 --- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/chanprog.c b/src/chanprog.c index eb5f3d1f6..cf63347bf 100644 --- a/src/chanprog.c +++ b/src/chanprog.c @@ -312,6 +312,14 @@ void tell_verbose_status(int idx) "Threaded DNS core is disabled.\n" #endif "Socket table: %d/%d\n", threaddata()->MAXSOCKS, max_socks); + int j = 0; + int k = max_logs * sizeof(log_t); + for (int i = 0; i < max_logs; i++) { + if (logs[i].filename) + j++; + k += logs[i].szlast_len; + } + dprintf(idx, "Log table: %d/%d %d bytes\n", j, max_logs, k); } /* Show all internal state variables diff --git a/src/eggdrop.h b/src/eggdrop.h index 393de019d..770104a0a 100644 --- a/src/eggdrop.h +++ b/src/eggdrop.h @@ -541,8 +541,9 @@ typedef struct { char *filename; unsigned int mask; /* what to send to this log */ char *chname; /* which channel */ - char szlast[LOGLINELEN]; /* for 'Last message repeated n times' + char *szlast; /* for 'Last message repeated n times' * stuff in misc.c/putlog() */ + int szlast_len; /* sizeof szlast */ int repeats; /* number of times szLast has been repeated */ unsigned int flags; /* other flags */ FILE *f; /* existing file */ @@ -734,6 +735,13 @@ enum { # define STRINGIFY1(x) #x #endif +#ifndef MIN + #define MIN(a,b) (((a)<(b))?(a):(b)) +#endif +#ifndef MAX + #define MAX(a,b) (((a)>(b))?(a):(b)) +#endif + #ifdef EGG_TDNS #include #define DTN_TYPE_HOSTBYIP 0 diff --git a/src/misc.c b/src/misc.c index 4f614ec74..45c33a246 100644 --- a/src/misc.c +++ b/src/misc.c @@ -95,20 +95,9 @@ void init_misc() if (max_logs < 1) max_logs = 1; - if (logs) - logs = nrealloc(logs, max_logs * sizeof(log_t)); - else - logs = nmalloc(max_logs * sizeof(log_t)); - for (; last < max_logs; last++) { - logs[last].filename = logs[last].chname = NULL; - logs[last].mask = 0; - logs[last].f = NULL; - /* Added by cybah */ - logs[last].szlast[0] = 0; - logs[last].repeats = 0; - /* Added by rtc */ - logs[last].flags = 0; - } + logs = nrealloc(logs, max_logs * sizeof(log_t)); + memset(logs + last, 0, (max_logs - last) * sizeof(log_t)); + last = max_logs; } @@ -587,7 +576,7 @@ void putlog (int type, char *chname, const char *format, ...) /* Check if this is the same as the last line added to * the log. */ - if (!strcasecmp(out + tsl, logs[i].szlast)) + if (logs[i].szlast && !strcasecmp(out + tsl, logs[i].szlast)) /* It is a repeat, so increment repeats */ logs[i].repeats++; else { @@ -607,7 +596,17 @@ void putlog (int type, char *chname, const char *format, ...) */ } fputs(out, logs[i].f); - strlcpy(logs[i].szlast, out + tsl, LOGLINEMAX); + size_t l = strlen(out + tsl); + if (l > logs[i].szlast_len) { + if (!logs[i].szlast_len) { + logs[i].szlast_len = MIN(MAX(l, 128), LOGLINELEN); + logs[i].szlast = nrealloc(logs[i].szlast, logs[i].szlast_len); + } else if (logs[i].szlast_len < LOGLINELEN) { + logs[i].szlast_len = MIN(MAX(l, logs[i].szlast_len << 1), LOGLINELEN); + logs[i].szlast = nrealloc(logs[i].szlast, logs[i].szlast_len); + } + } + strlcpy(logs[i].szlast, out + tsl, logs[i].szlast_len); } } } diff --git a/src/mod/server.mod/servmsg.c b/src/mod/server.mod/servmsg.c index f018d37c6..9c1f0f66a 100644 --- a/src/mod/server.mod/servmsg.c +++ b/src/mod/server.mod/servmsg.c @@ -1342,9 +1342,6 @@ static int tryauthenticate(char *from, char *msg) char *s; /* 400-byte chunk, see: https://ircv3.net/specs/extensions/sasl-3.1.html * base64 padding */ - #ifndef MAX - #define MAX(a,b) (((a)>(b))?(a):(b)) - #endif unsigned char dst[((MAX((sizeof src), 400) + 2) / 3) << 2] = ""; #ifdef HAVE_EVP_PKEY_GET1_EC_KEY int olen; From 29739c2372a99dc856335441c904c263b0572cf4 Mon Sep 17 00:00:00 2001 From: Michael Ortmann <41313082+michaelortmann@users.noreply.github.com> Date: Fri, 21 Feb 2025 02:06:35 +0100 Subject: [PATCH 2/3] Update expmem_misc() --- src/misc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/misc.c b/src/misc.c index 45c33a246..e9643dcae 100644 --- a/src/misc.c +++ b/src/misc.c @@ -86,6 +86,8 @@ int expmem_misc() for (item = current->first; item; item = item->next) tot += sizeof(struct help_list_t) + strlen(item->name) + 1; } + for (int i = 0; i < max_logs; i++) + tot += logs[i].szlast_len; return tot + (max_logs * sizeof(log_t)); } From 69e7ff36be9a21dd5eb677a627f9098e4c1ebb76 Mon Sep 17 00:00:00 2001 From: Michael Ortmann <41313082+michaelortmann@users.noreply.github.com> Date: Tue, 25 Feb 2025 07:52:21 +0100 Subject: [PATCH 3/3] More MAX cleanup, which is now in eggdrop.h --- src/mod/server.mod/sasl.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/mod/server.mod/sasl.c b/src/mod/server.mod/sasl.c index 234b93116..29aee9472 100644 --- a/src/mod/server.mod/sasl.c +++ b/src/mod/server.mod/sasl.c @@ -540,9 +540,6 @@ static int gotauthenticate(char *from, char *msg) char error_msg[1050]; /* snprintf() truncation should be tolerable */ int server_msg_plain_len; #endif - #ifndef MAX - #define MAX(a,b) (((a)>(b))?(a):(b)) - #endif char client_msg_b64[((MAX((sizeof client_msg_plain), 400) + 2) / 3) << 2] = ""; fixcolon(msg); /* Because Inspircd does its own thing */