Skip to content

Commit

Permalink
regex: fix tained values reported by coverity
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Oct 21, 2016
1 parent c22ddb1 commit abfecfc
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions modules/regex/regex_mod.c
Expand Up @@ -258,6 +258,7 @@ static void destroy(void)
static int load_pcres(int action)
{
int i, j;
int len, plen;
FILE *f;
char line[FILE_MAX_LINE];
char **patterns = NULL;
Expand Down Expand Up @@ -327,28 +328,37 @@ static int load_pcres(int action)
memset(line, '\0', FILE_MAX_LINE);
continue;
}
len = strlen(line);

/* Check if the patter size is too big (aprox) */
if (strlen(patterns[i]) + strlen(line) >= group_max_size - 2) {
if (strlen(patterns[i]) + len >= group_max_size - 2) {
LM_ERR("pattern max file exceeded\n");
fclose(f);
goto err;
}
if (len >= FILE_MAX_LINE - 1) {
LM_ERR("cannot add group termination\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[len - 1] == '\n') {
line[len] = line[len - 1];
line[len - 1] = ')';
} else {
/* This is the last char in the file and it's not \n */
line[strlen(line)] = ')';
line[len] = ')';
}
len++;
plen = strlen(patterns[i]);

/* Append '(' at the beginning of the line */
memcpy(patterns[i]+strlen(patterns[i]), "(", 1);
memcpy(patterns[i]+plen, "(", 1);
plen++;

/* Append the line to the current pattern */
memcpy(patterns[i]+strlen(patterns[i]), line, strlen(line));
memcpy(patterns[i]+plen, line, len);

memset(line, '\0', FILE_MAX_LINE);
}
Expand All @@ -358,29 +368,31 @@ static int load_pcres(int action)

/* Fix the patterns */
for (i=0; i < num_pcres_tmp; i++) {
plen = strlen(patterns[i]);

/* Convert empty groups in unmatcheable regular expression ^$ */
if (strlen(patterns[i]) == 1) {
if (plen == 1) {
patterns[i][0] = '^';
patterns[i][1] = '$';
patterns[i][2] = '\0';
continue;
}

/* Delete possible '\n' at the end of the pattern */
if (patterns[i][strlen(patterns[i])-1] == '\n') {
patterns[i][strlen(patterns[i])-1] = '\0';
if (patterns[i][plen-1] == '\n') {
patterns[i][plen-1] = '\0';
plen--;
}

/* Replace '\n' with '|' (except at the end of the pattern) */
for (j=0; j < strlen(patterns[i]); j++) {
if (patterns[i][j] == '\n' && j != strlen(patterns[i])-1) {
for (j=0; j < plen; j++) {
if (patterns[i][j] == '\n' && j != plen-1) {
patterns[i][j] = '|';
}
}

/* Add ')' at the end of the pattern */
patterns[i][strlen(patterns[i])] = ')';
patterns[i][plen] = ')';
}

/* Log the group patterns */
Expand Down

0 comments on commit abfecfc

Please sign in to comment.