[sr-dev] git:5.3:675ac0d1: regex: use var to store strlen() values

Daniel-Constantin Mierla miconda at gmail.com
Fri Oct 30 14:50:59 CET 2020


Module: kamailio
Branch: 5.3
Commit: 675ac0d1afbefd56abe063a0408479b62d15a49e
URL: https://github.com/kamailio/kamailio/commit/675ac0d1afbefd56abe063a0408479b62d15a49e

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2020-10-30T14:47:36+01:00

regex: use var to store strlen() values

- avoid doing it again in a few cases

(cherry picked from commit 5d490d616132067a5e914302a24b964b73b229a2)
(cherry picked from commit bd2204b7151261981bc6872928b7434e2ac7df2e)

---

Modified: src/modules/regex/regex_mod.c

---

Diff:  https://github.com/kamailio/kamailio/commit/675ac0d1afbefd56abe063a0408479b62d15a49e.diff
Patch: https://github.com/kamailio/kamailio/commit/675ac0d1afbefd56abe063a0408479b62d15a49e.patch

---

diff --git a/src/modules/regex/regex_mod.c b/src/modules/regex/regex_mod.c
index 475c3542eb..2bc521040b 100644
--- a/src/modules/regex/regex_mod.c
+++ b/src/modules/regex/regex_mod.c
@@ -246,6 +246,7 @@ static int load_pcres(int action)
 	int pcre_erroffset;
 	int num_pcres_tmp = 0;
 	pcre **pcres_tmp = NULL;
+	int llen;
 
 	/* Get the lock */
 	lock_get(reload_lock);
@@ -301,31 +302,37 @@ static int load_pcres(int action)
 			}
 			/* Start the regular expression with '(' */
 			patterns[i][0] = '(';
+			patterns[i][1] = '\0';
 			memset(line, 0, FILE_MAX_LINE);
 			continue;
 		}
 
+		llen = strlen(line);
 		/* Check if the patter size is too big (aprox) */
-		if (strlen(patterns[i]) + strlen(line) >= group_max_size - 4) {
+		if (strlen(patterns[i]) + llen >= group_max_size - 4) {
 			LM_ERR("pattern max file exceeded\n");
 			fclose(f);
 			goto err;
 		}
 
 		/* Append ')' at the end of the line */
-		if (line[strlen(line) - 1] == '\n') {
-			line[strlen(line)] = line[strlen(line) - 1];
-			line[strlen(line) - 2] = ')';
+		if (line[llen - 1] == '\n') {
+			line[llen - 1] = ')';
+			line[llen] = '\n';
+			line[llen + 1] = '\0';
 		} else {
 			/* This is the last char in the file and it's not \n */
-			line[strlen(line)] = ')';
+			line[llen] = ')';
+			line[llen + 1] = '\0';
 		}
 
 		/* Append '(' at the beginning of the line */
-		memcpy(patterns[i]+strlen(patterns[i]), "(", 1);
+		llen = strlen(patterns[i]);
+		memcpy(patterns[i]+llen, "(", 1);
+		llen++;
 
-		/* Append the line to the current pattern */
-		memcpy(patterns[i]+strlen(patterns[i]), line, strlen(line));
+		/* Append the line to the current pattern (including the ending 0) */
+		memcpy(patterns[i] + llen, line, strlen(line) + 1);
 
 		memset(line, 0, FILE_MAX_LINE);
 	}




More information about the sr-dev mailing list