Browse code

regex: use var to store strlen() values

- avoid doing it again in a few cases

Daniel-Constantin Mierla authored on 26/10/2020 12:06:53
Showing 1 changed files
... ...
@@ -246,6 +246,7 @@ static int load_pcres(int action)
246 246
 	int pcre_erroffset;
247 247
 	int num_pcres_tmp = 0;
248 248
 	pcre **pcres_tmp = NULL;
249
+	int llen;
249 250
 
250 251
 	/* Get the lock */
251 252
 	lock_get(reload_lock);
... ...
@@ -301,31 +302,37 @@ static int load_pcres(int action)
301 302
 			}
302 303
 			/* Start the regular expression with '(' */
303 304
 			patterns[i][0] = '(';
305
+			patterns[i][1] = '\0';
304 306
 			memset(line, 0, FILE_MAX_LINE);
305 307
 			continue;
306 308
 		}
307 309
 
310
+		llen = strlen(line);
308 311
 		/* Check if the patter size is too big (aprox) */
309
-		if (strlen(patterns[i]) + strlen(line) >= group_max_size - 4) {
312
+		if (strlen(patterns[i]) + llen >= group_max_size - 4) {
310 313
 			LM_ERR("pattern max file exceeded\n");
311 314
 			fclose(f);
312 315
 			goto err;
313 316
 		}
314 317
 
315 318
 		/* Append ')' at the end of the line */
316
-		if (line[strlen(line) - 1] == '\n') {
317
-			line[strlen(line)] = line[strlen(line) - 1];
318
-			line[strlen(line) - 2] = ')';
319
+		if (line[llen - 1] == '\n') {
320
+			line[llen - 1] = ')';
321
+			line[llen] = '\n';
322
+			line[llen + 1] = '\0';
319 323
 		} else {
320 324
 			/* This is the last char in the file and it's not \n */
321
-			line[strlen(line)] = ')';
325
+			line[llen] = ')';
326
+			line[llen + 1] = '\0';
322 327
 		}
323 328
 
324 329
 		/* Append '(' at the beginning of the line */
325
-		memcpy(patterns[i]+strlen(patterns[i]), "(", 1);
330
+		llen = strlen(patterns[i]);
331
+		memcpy(patterns[i]+llen, "(", 1);
332
+		llen++;
326 333
 
327
-		/* Append the line to the current pattern */
328
-		memcpy(patterns[i]+strlen(patterns[i]), line, strlen(line));
334
+		/* Append the line to the current pattern (including the ending 0) */
335
+		memcpy(patterns[i] + llen, line, strlen(line) + 1);
329 336
 
330 337
 		memset(line, 0, FILE_MAX_LINE);
331 338
 	}