On Mon, 04 Dec 2023 15:46:29 +0200, Yavor Doganov wrote: > I guess I need to revisit my patch and find some way to fix this.
Here it is -- no memory leaks and I could not obtain crash or abort with the logs I've tested. Note that while my original patch introduced some leaks, it also fixes some in the original code. Tested the following plugins: dpkg, exim, httpd, procmail, sulog and syslog (yours and mine). Also compared the output between version -7 and git master + this new patch. Please test extensively and let me know if something is wrong. You may wish to apply (or not) two additional patches: 1 memory leak that I discovered with aptitude/Xorg logs and 1 uninitialized value (reported by valgrind as error).
>From 475cbc072cb08bd6a57befa6c3856f8458355b1b Mon Sep 17 00:00:00 2001 From: Yavor Doganov <ya...@gnu.org> Date: Wed, 6 Dec 2023 15:07:25 +0200 Subject: [PATCH] pcre2.patch: Fix some memory leaks --- debian/patches/pcre2.patch | 181 +++++++++++++++++++++++++------------ 1 file changed, 125 insertions(+), 56 deletions(-) diff --git a/debian/patches/pcre2.patch b/debian/patches/pcre2.patch index 6b60dc8..4014fc3 100644 --- a/debian/patches/pcre2.patch +++ b/debian/patches/pcre2.patch @@ -2,7 +2,7 @@ Description: Port to PCRE2. Bug-Debian: https://bugs.debian.org/1000044 Author: Yavor Doganov <ya...@gnu.org> Forwarded: no -Last-Update: 2023-12-03 +Last-Update: 2023-12-06 --- --- ccze.orig/configure.ac @@ -39,7 +39,7 @@ Last-Update: 2023-12-03 static char *words_bad[] = { "warn", "restart", "exit", "stop", "end", "shutting", "down", "close", -@@ -71,33 +71,31 @@ +@@ -71,34 +71,35 @@ void ccze_wordcolor_process_one (char *word, int slookup) { @@ -48,8 +48,9 @@ Last-Update: 2023-12-03 + size_t wlen, l; ccze_color_t col; - int match, printed = 0; +- char *pre = NULL, *post = NULL, *tmp, *lword; + int printed = 0; - char *pre = NULL, *post = NULL, *tmp, *lword; ++ char *pre = NULL, *post = NULL, *tmp = NULL, *lword; + pcre2_match_data *offsets; col = CCZE_COLOR_DEFAULT; @@ -63,10 +64,11 @@ Last-Update: 2023-12-03 { - pcre_get_substring (word, offsets, match, 1, (const char **)&pre); - pcre_get_substring (word, offsets, match, 2, (const char **)&tmp); -- free (word); + pcre2_substring_get_bynumber (offsets, 1, (unsigned char **)&pre, &l); + pcre2_substring_get_bynumber (offsets, 2, (unsigned char **)&tmp, &l); - word = tmp; + free (word); +- word = tmp; ++ word = strdup (tmp); } else pre = NULL; @@ -78,13 +80,16 @@ Last-Update: 2023-12-03 { - pcre_get_substring (word, offsets, match, 1, (const char **)&tmp); - pcre_get_substring (word, offsets, match, 2, (const char **)&post); -- free (word); ++ pcre2_substring_free (tmp); + pcre2_substring_get_bynumber (offsets, 1, (unsigned char **)&tmp, &l); + pcre2_substring_get_bynumber (offsets, 2, (unsigned char **)&post, &l); - word = tmp; + free (word); +- word = tmp; ++ word = strdup (tmp); } else -@@ -107,45 +105,45 @@ + post = NULL; +@@ -107,45 +108,45 @@ lword = _stolower (word); /** Host **/ @@ -144,20 +149,20 @@ Last-Update: 2023-12-03 { char *host, *ip; size_t hostlen, iplen; -@@ -206,9 +204,9 @@ - } +@@ -207,8 +208,10 @@ free (lword); -- free (word); + free (word); - free (post); - free (pre); ++ pcre2_substring_free (tmp); + pcre2_substring_free (post); + pcre2_substring_free (pre); + pcre2_match_data_free (offsets); } void -@@ -260,64 +258,67 @@ +@@ -260,64 +263,67 @@ void ccze_wordcolor_setup (void) { @@ -305,7 +310,8 @@ Last-Update: 2023-12-03 +ccze_apm_process (pcre2_match_data *offsets) { char *battery, *charge, *rate, *stuff1, *elapsed, *remain; - char *stuff2; +- char *stuff2; ++ char *stuff2, *ret; + size_t l; - pcre_get_substring (str, offsets, match, 1, (const char **)&battery); @@ -325,7 +331,7 @@ Last-Update: 2023-12-03 ccze_addstr (CCZE_COLOR_DEFAULT, "Battery:"); ccze_space (); -@@ -62,12 +63,12 @@ +@@ -62,48 +63,54 @@ ccze_addstr (CCZE_COLOR_DATE, remain); ccze_space (); @@ -335,16 +341,19 @@ Last-Update: 2023-12-03 - free (stuff1); - free (elapsed); - free (remain); ++ ret = strdup (stuff2); + pcre2_substring_free (battery); + pcre2_substring_free (charge); + pcre2_substring_free (rate); + pcre2_substring_free (stuff1); + pcre2_substring_free (elapsed); + pcre2_substring_free (remain); ++ pcre2_substring_free (stuff2); - return stuff2; +- return stuff2; ++ return ret; } -@@ -75,35 +76,39 @@ + static void ccze_apm_setup (void) { @@ -408,7 +417,8 @@ Last-Update: 2023-12-03 -ccze_distcc_process (const char *str, int *offsets, int match) +ccze_distcc_process (pcre2_match_data *offsets) { - char *pid, *func, *rest; +- char *pid, *func, *rest; ++ char *pid, *func, *rest, *ret; + size_t l; - pcre_get_substring (str, offsets, match, 1, (const char **)&pid); @@ -420,18 +430,21 @@ Last-Update: 2023-12-03 ccze_addstr (CCZE_COLOR_PROC, "distccd"); ccze_addstr (CCZE_COLOR_PIDB, "["); -@@ -53,8 +53,8 @@ +@@ -53,41 +53,44 @@ ccze_space (); } - free (pid); - free (func); ++ ret = strdup (rest); + pcre2_substring_free (pid); + pcre2_substring_free (func); ++ pcre2_substring_free (rest); - return rest; +- return rest; ++ return ret; } -@@ -62,32 +62,33 @@ + static void ccze_distcc_setup (void) { @@ -689,8 +702,9 @@ Last-Update: 2023-12-03 -ccze_exim_process (const char *str, int *offsets, int match) +ccze_exim_process (pcre2_match_data *offsets) { - char *date, *msg=NULL, *action=NULL, *uniqn=NULL, *msgfull; +- char *date, *msg=NULL, *action=NULL, *uniqn=NULL, *msgfull; - int match2, offsets2[99]; ++ char *date, *msg=NULL, *action=NULL, *uniqn=NULL, *ret=NULL, *msgfull; + pcre2_match_data *offsets2; + size_t l; ccze_color_t color = CCZE_COLOR_UNKNOWN; @@ -721,7 +735,7 @@ Last-Update: 2023-12-03 if (action[0] == '<') color = CCZE_COLOR_INCOMING; else if (action[1] == '>') -@@ -56,13 +57,13 @@ +@@ -56,16 +57,16 @@ else if (action[0] == '=' || action[0] == '*') color = CCZE_COLOR_ERROR; } @@ -740,18 +754,27 @@ Last-Update: 2023-12-03 + (unsigned char **)&msg, &l); } else - msg = strdup (msgfull); -@@ -82,47 +83,54 @@ +- msg = strdup (msgfull); ++ ret = strdup (msgfull); + + ccze_print_date (date); + ccze_space (); +@@ -82,47 +83,58 @@ ccze_space(); } +- return msg; ++ if (!ret) ++ ret = strdup (msg); ++ + pcre2_substring_free (date); + pcre2_substring_free (msgfull); + pcre2_substring_free (uniqn); + pcre2_substring_free (action); ++ pcre2_substring_free (msg); + pcre2_match_data_free (offsets2); + - return msg; ++ return ret; } static void @@ -829,7 +852,8 @@ Last-Update: 2023-12-03 -ccze_fetchmail_process (const char *str, int *offsets, int match) +ccze_fetchmail_process (pcre2_match_data *offsets) { - char *start, *addy, *current, *full, *rest; +- char *start, *addy, *current, *full, *rest; ++ char *start, *addy, *current, *full, *rest, *ret; + size_t l; - pcre_get_substring (str, offsets, match, 1, (const char **)&start); @@ -845,7 +869,7 @@ Last-Update: 2023-12-03 ccze_addstr (CCZE_COLOR_DEFAULT, start); ccze_space (); -@@ -51,10 +52,10 @@ +@@ -51,45 +52,51 @@ ccze_addstr (CCZE_COLOR_NUMBERS, full); ccze_space (); @@ -853,14 +877,17 @@ Last-Update: 2023-12-03 - free (addy); - free (current); - free (full); ++ ret = strdup (rest); + pcre2_substring_free (start); + pcre2_substring_free (addy); + pcre2_substring_free (current); + pcre2_substring_free (full); ++ pcre2_substring_free (rest); - return rest; +- return rest; ++ return ret; } -@@ -62,34 +63,38 @@ + static void ccze_fetchmail_setup (void) { @@ -1197,7 +1224,8 @@ Last-Update: 2023-12-03 +ccze_icecast_process (pcre2_match_data *offsets) { char *date = NULL, *admin = NULL, *threadno = NULL, *thread = NULL; - char *rest = NULL; +- char *rest = NULL; ++ char *rest = NULL, *ret; + size_t l; - pcre_get_substring (str, offsets, match, 1, (const char **)&date); @@ -1213,7 +1241,7 @@ Last-Update: 2023-12-03 ccze_addstr (CCZE_COLOR_DATE, date); ccze_space (); -@@ -65,29 +65,30 @@ +@@ -65,29 +65,32 @@ } ccze_space (); @@ -1221,12 +1249,15 @@ Last-Update: 2023-12-03 - free (admin); - free (threadno); - free (thread); ++ ret = strdup (rest); + pcre2_substring_free (date); + pcre2_substring_free (admin); + pcre2_substring_free (threadno); + pcre2_substring_free (thread); ++ pcre2_substring_free (rest); - return rest; +- return rest; ++ return ret; } static char * @@ -1258,7 +1289,7 @@ Last-Update: 2023-12-03 ccze_addstr (CCZE_COLOR_DATE, date); -@@ -121,15 +122,15 @@ +@@ -121,15 +124,15 @@ ccze_newline (); @@ -1283,7 +1314,7 @@ Last-Update: 2023-12-03 return NULL; } -@@ -137,51 +138,51 @@ +@@ -137,51 +140,51 @@ static void ccze_icecast_setup (void) { @@ -1467,7 +1498,7 @@ Last-Update: 2023-12-03 } --- ccze.orig/src/mod_php.c +++ ccze/src/mod_php.c -@@ -29,23 +29,23 @@ +@@ -29,56 +29,59 @@ static void ccze_php_shutdown (void); static int ccze_php_handle (const char *str, size_t length, char **rest); @@ -1479,7 +1510,8 @@ Last-Update: 2023-12-03 -ccze_php_process (const char *str, int *offsets, int match) +ccze_php_process (pcre2_match_data *offsets) { - char *date = NULL, *rest = NULL; +- char *date = NULL, *rest = NULL; ++ char *date = NULL, *rest = NULL, *ret; + size_t l; - pcre_get_substring (str, offsets, match, 1, (const char **)&date); @@ -1493,11 +1525,14 @@ Last-Update: 2023-12-03 ccze_space (); - free (date); ++ ret = strdup (rest); + pcre2_substring_free (date); ++ pcre2_substring_free (rest); - return rest; +- return rest; ++ return ret; } -@@ -53,32 +53,33 @@ + static void ccze_php_setup (void) { @@ -1998,7 +2033,7 @@ Last-Update: 2023-12-03 ccze_print_date (date); ccze_addstr (CCZE_COLOR_DEFAULT, espace); -@@ -135,63 +135,65 @@ +@@ -135,63 +135,67 @@ ccze_newline (); @@ -2036,7 +2071,8 @@ Last-Update: 2023-12-03 -ccze_squid_cache_log_process (const char *str, int *offsets, int match) +ccze_squid_cache_log_process (pcre2_match_data *offsets) { - char *date, *other; +- char *date, *other; ++ char *date, *other, *ret; + size_t l; - pcre_get_substring (str, offsets, match, 1, (const char **)&date); @@ -2048,8 +2084,11 @@ Last-Update: 2023-12-03 ccze_space(); - free (date); +- return other; ++ ret = strdup (other); + pcre2_substring_free (date); - return other; ++ pcre2_substring_free (other); ++ return ret; } static char * @@ -2100,7 +2139,7 @@ Last-Update: 2023-12-03 ccze_print_date (date); ccze_space(); -@@ -223,24 +225,24 @@ +@@ -223,24 +227,24 @@ ccze_newline (); @@ -2143,7 +2182,7 @@ Last-Update: 2023-12-03 return NULL; } -@@ -248,65 +250,62 @@ +@@ -248,65 +252,62 @@ static void ccze_squid_setup (void) { @@ -2420,7 +2459,7 @@ Last-Update: 2023-12-03 } --- ccze.orig/src/mod_syslog.c +++ ccze/src/mod_syslog.c -@@ -29,26 +29,26 @@ +@@ -29,26 +29,30 @@ static void ccze_syslog_shutdown (void); static int ccze_syslog_handle (const char *str, size_t length, char **rest); @@ -2434,6 +2473,7 @@ Last-Update: 2023-12-03 { char *date = NULL, *host = NULL, *send = NULL, *process = NULL; char *msg = NULL, *pid = NULL, *tmp = NULL, *toret; ++ int use_free = 0; + size_t l; - pcre_get_substring (str, offsets, match, 1, (const char **)&date); @@ -2445,7 +2485,11 @@ Last-Update: 2023-12-03 if ((strstr (send, "last message repeated") && strstr (send, "times")) || (strstr (send, "-- MARK --"))) - msg = strdup (send); +- msg = strdup (send); ++ { ++ msg = strdup (send); ++ use_free = 1; ++ } else { - pcre_get_substring (str, offsets, match, 4, (const char **)&process); @@ -2455,26 +2499,40 @@ Last-Update: 2023-12-03 } if (process) -@@ -87,12 +87,13 @@ +@@ -60,8 +64,8 @@ + + pid = strndup (&t[1], (size_t)(t2 - t - 1)); + tmp = strndup (process, (size_t)(t - process)); +- free (process); +- process = tmp; ++ pcre2_substring_free (process); ++ process = strdup (tmp); + } + } + +@@ -87,12 +91,17 @@ else toret = strdup (send); - free (date); - free (host); - free (send); -- free (process); -- free (msg); + pcre2_substring_free (date); + pcre2_substring_free (host); + pcre2_substring_free (send); -+ pcre2_substring_free (process); -+ pcre2_substring_free (msg); + free (process); +- free (msg); free (pid); + free (tmp); ++ ++ if (use_free) ++ free (msg); ++ else ++ pcre2_substring_free (msg); return toret; } -@@ -100,33 +101,34 @@ +@@ -100,33 +109,34 @@ static void ccze_syslog_setup (void) { @@ -2580,7 +2638,14 @@ Last-Update: 2023-12-03 } --- ccze.orig/src/mod_vsftpd.c +++ ccze/src/mod_vsftpd.c -@@ -26,19 +26,19 @@ +@@ -20,25 +20,26 @@ + */ + + #include <ccze.h> ++#include <string.h> + #include <stdlib.h> + + static void ccze_vsftpd_setup (void); static void ccze_vsftpd_shutdown (void); static int ccze_vsftpd_handle (const char *str, size_t length, char **rest); @@ -2592,7 +2657,8 @@ Last-Update: 2023-12-03 -ccze_vsftpd_log_process (const char *str, int *offsets, int match) +ccze_vsftpd_log_process (pcre2_match_data *offsets) { - char *date, *sspace, *pid, *user, *other; +- char *date, *sspace, *pid, *user, *other; ++ char *date, *sspace, *pid, *user, *other, *ret; + size_t l; - pcre_get_substring (str, offsets, match, 1, (const char **)&date); @@ -2608,7 +2674,7 @@ Last-Update: 2023-12-03 ccze_addstr (CCZE_COLOR_DATE, date); ccze_addstr (CCZE_COLOR_DEFAULT, sspace); -@@ -57,10 +57,10 @@ +@@ -57,45 +58,48 @@ ccze_space (); } @@ -2616,14 +2682,17 @@ Last-Update: 2023-12-03 - free (sspace); - free (pid); - free (user); ++ ret = strdup (other); + pcre2_substring_free (date); + pcre2_substring_free (sspace); + pcre2_substring_free (pid); + pcre2_substring_free (user); ++ pcre2_substring_free (other); - return other; +- return other; ++ return ret; } -@@ -68,34 +68,35 @@ + static void ccze_vsftpd_setup (void) { -- 2.43.0
Description: Fix a memory leak. Author: Yavor Doganov <ya...@gnu.org> Forwarded: no Last-Update: 2023-12-06 --- --- ccze.orig/src/ccze.c +++ ccze/src/ccze.c @@ -731,6 +731,9 @@ if (ccze_config.mode == CCZE_MODE_CURSES) refresh (); + + free (subject); + subject = NULL; } if (ccze_config.mode == CCZE_MODE_CURSES)
Description: Fix a possibly uninitialised value. Author: Yavor Doganov <ya...@gnu.org> Forwarded: no Last-Update: 2023-12-06 --- --- ccze.orig/src/ccze-color.c +++ ccze/src/ccze-color.c @@ -459,9 +459,11 @@ size_t len; struct stat stb; - stat (fn, &stb); - if (!S_ISREG (stb.st_mode)) - return; + if ((stat (fn, &stb)) == 0) + { + if (!S_ISREG (stb.st_mode)) + return; + } fp = fopen (fn, "r"); if (!fp)