[sr-dev] git:5.0:5f8dc56f: regex: free unused tmp pkg memory for reloaded patterns

Daniel-Constantin Mierla miconda at gmail.com
Thu Nov 2 08:48:29 CET 2017


Module: kamailio
Branch: 5.0
Commit: 5f8dc56f3cb47d1e804fbc3d7f5321ce979d61d9
URL: https://github.com/kamailio/kamailio/commit/5f8dc56f3cb47d1e804fbc3d7f5321ce979d61d9

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2017-11-02T08:48:02+01:00

regex: free unused tmp pkg memory for reloaded patterns

- use memset instead of for loop to init allocated memory to 0
- few bits of code formatting

(cherry picked from commit 5940e759a754644d29e4145eea648ede45b4b356)

---

Modified: src/modules/regex/regex_mod.c

---

Diff:  https://github.com/kamailio/kamailio/commit/5f8dc56f3cb47d1e804fbc3d7f5321ce979d61d9.diff
Patch: https://github.com/kamailio/kamailio/commit/5f8dc56f3cb47d1e804fbc3d7f5321ce979d61d9.patch

---

diff --git a/src/modules/regex/regex_mod.c b/src/modules/regex/regex_mod.c
index e0c8761a75..dc953661ce 100644
--- a/src/modules/regex/regex_mod.c
+++ b/src/modules/regex/regex_mod.c
@@ -270,17 +270,17 @@ static int load_pcres(int action)
 			fclose(f);
 			goto err;
 		}
-		memset(patterns[i], '\0', group_max_size);
+		memset(patterns[i], 0, group_max_size);
 	}
 
 	/* Read the file and extract the patterns */
-	memset(line, '\0', FILE_MAX_LINE);
+	memset(line, 0, FILE_MAX_LINE);
 	i = -1;
 	while (fgets(line, FILE_MAX_LINE, f) != NULL) {
 
 		/* Ignore comments and lines starting by space, tab, CR, LF */
 		if(isspace(line[0]) || line[0]=='#') {
-			memset(line, '\0', FILE_MAX_LINE);
+			memset(line, 0, FILE_MAX_LINE);
 			continue;
 		}
 
@@ -302,7 +302,7 @@ static int load_pcres(int action)
 			}
 			/* Start the regular expression with '(' */
 			patterns[i][0] = '(';
-			memset(line, '\0', FILE_MAX_LINE);
+			memset(line, 0, FILE_MAX_LINE);
 			continue;
 		}
 
@@ -328,7 +328,7 @@ static int load_pcres(int action)
 		/* Append the line to the current pattern */
 		memcpy(patterns[i]+strlen(patterns[i]), line, strlen(line));
 
-		memset(line, '\0', FILE_MAX_LINE);
+		memset(line, 0, FILE_MAX_LINE);
 	}
 	num_pcres_tmp = i + 1;
 
@@ -369,7 +369,8 @@ static int load_pcres(int action)
 	/* Log the group patterns */
 	LM_INFO("num groups = %d\n", num_pcres_tmp);
 	for (i=0; i < num_pcres_tmp; i++) {
-		LM_INFO("<group[%d]>%s</group[%d]> (size = %i)\n", i, patterns[i], i, (int)strlen(patterns[i]));
+		LM_INFO("<group[%d]>%s</group[%d]> (size = %i)\n", i, patterns[i],
+				i, (int)strlen(patterns[i]));
 	}
 
 	/* Temporal pointer of pcres */
@@ -384,14 +385,17 @@ static int load_pcres(int action)
 	/* Compile the patters */
 	for (i=0; i<num_pcres_tmp; i++) {
 
-		pcre_tmp = pcre_compile(patterns[i], pcre_options, &pcre_error, &pcre_erroffset, NULL);
+		pcre_tmp = pcre_compile(patterns[i], pcre_options, &pcre_error,
+				&pcre_erroffset, NULL);
 		if (pcre_tmp == NULL) {
-			LM_ERR("pcre_tmp compilation of '%s' failed at offset %d: %s\n", patterns[i], pcre_erroffset, pcre_error);
+			LM_ERR("pcre_tmp compilation of '%s' failed at offset %d: %s\n",
+					patterns[i], pcre_erroffset, pcre_error);
 			goto err;
 		}
 		pcre_rc = pcre_fullinfo(pcre_tmp, NULL, PCRE_INFO_SIZE, &pcre_size);
 		if (pcre_rc) {
-			printf("pcre_fullinfo on compiled pattern[%i] yielded error: %d\n", i, pcre_rc);
+			printf("pcre_fullinfo on compiled pattern[%i] yielded error: %d\n",
+					i, pcre_rc);
 			goto err;
 		}
 
@@ -419,9 +423,7 @@ static int load_pcres(int action)
 		LM_ERR("no more memory for pcres\n");
 		goto err;
 	}
-	for (i=0; i<num_pcres_tmp; i++) {
-		pcres[i] = NULL;
-	}
+	memset(pcres, 0, sizeof(pcre *) * num_pcres_tmp);
 	for (i=0; i<num_pcres_tmp; i++) {
 		pcre_rc = pcre_fullinfo(pcres_tmp[i], NULL, PCRE_INFO_SIZE, &pcre_size);
 		if ((pcres[i] = shm_malloc(pcre_size)) == 0) {
@@ -438,6 +440,10 @@ static int load_pcres(int action)
 		pkg_free(pcres_tmp[i]);
 	}
 	pkg_free(pcres_tmp);
+	/* Free allocated slots for unused patterns */
+	for (i = num_pcres_tmp; i < max_groups; i++) {
+		pkg_free(patterns[i]);
+	}
 	pkg_free(patterns);
 	lock_release(reload_lock);
 
@@ -692,4 +698,3 @@ static int regex_init_rpc(void)
 	}
 	return 0;
 }
-




More information about the sr-dev mailing list