Skip to content

Commit

Permalink
Fix calls to regexec.
Browse files Browse the repository at this point in the history
If we don't need sub matching then never initialize nmatch and pmatch as
we will never look at its info anyway.
  • Loading branch information
Marco van Wieringen committed Oct 19, 2015
1 parent 1f7da46 commit 07f8fed
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/console/console.c
Expand Up @@ -459,7 +459,7 @@ void init_items()
static void match_kw(regex_t *preg, const char *what, int len, POOLMEM **buf)
{
int rc, size;
int nmatch=20;
int nmatch = 20;
regmatch_t pmatch[20];

if (len <= 0) {
Expand Down
4 changes: 1 addition & 3 deletions src/dird/dird.c
Expand Up @@ -1242,8 +1242,6 @@ static void cleanup_old_files()
POOLMEM *basename = get_pool_memory(PM_MESSAGE);
regex_t preg1;
char prbuf[500];
const int nmatch = 30;
regmatch_t pmatch[nmatch];
berrno be;

/* Exclude spaces and look for .mail or .restore.xx.bsr files */
Expand Down Expand Up @@ -1290,7 +1288,7 @@ static void cleanup_old_files()
}

/* Unlink files that match regexes */
if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) {
if (regexec(&preg1, result->d_name, 0, NULL, 0) == 0) {
pm_strcpy(cleanup, basename);
pm_strcat(cleanup, result->d_name);
Dmsg1(100, "Unlink: %s\n", cleanup);
Expand Down
4 changes: 1 addition & 3 deletions src/dird/migrate.c
Expand Up @@ -1131,15 +1131,13 @@ static bool regex_find_jobids(JCR *jcr, idpkt *ids,
}
/* Now apply the regex to the names and remove any item not matched */
foreach_dlist(item, item_chain) {
const int nmatch = 30;
regmatch_t pmatch[nmatch];
if (last_item) {
Dmsg1(dbglevel, "Remove item %s\n", last_item->item);
free(last_item->item);
item_chain->remove(last_item);
}
Dmsg1(dbglevel, "get name Item=%s\n", item->item);
rc = regexec(&preg, item->item, nmatch, pmatch, 0);
rc = regexec(&preg, item->item, 0, NULL, 0);
if (rc == 0) {
last_item = NULL; /* keep this one */
} else {
Expand Down
12 changes: 3 additions & 9 deletions src/findlib/find.c
Expand Up @@ -404,9 +404,7 @@ bool accept_file(FF_PKT *ff)
}
if (S_ISDIR(ff->statp.st_mode)) {
for (k = 0; k < fo->regexdir.size(); k++) {
const int nmatch = 30;
regmatch_t pmatch[nmatch];
if (regexec((regex_t *)fo->regexdir.get(k), ff->fname, nmatch, pmatch, 0) == 0) {
if (regexec((regex_t *)fo->regexdir.get(k), ff->fname, 0, NULL, 0) == 0) {
if (ff->flags & FO_EXCLUDE) {
return false; /* reject file */
}
Expand All @@ -415,9 +413,7 @@ bool accept_file(FF_PKT *ff)
}
} else {
for (k = 0; k < fo->regexfile.size(); k++) {
const int nmatch = 30;
regmatch_t pmatch[nmatch];
if (regexec((regex_t *)fo->regexfile.get(k), ff->fname, nmatch, pmatch, 0) == 0) {
if (regexec((regex_t *)fo->regexfile.get(k), ff->fname, 0, NULL, 0) == 0) {
if (ff->flags & FO_EXCLUDE) {
return false; /* reject file */
}
Expand All @@ -426,9 +422,7 @@ bool accept_file(FF_PKT *ff)
}
}
for (k = 0; k < fo->regex.size(); k++) {
const int nmatch = 30;
regmatch_t pmatch[nmatch];
if (regexec((regex_t *)fo->regex.get(k), ff->fname, nmatch, pmatch, 0) == 0) {
if (regexec((regex_t *)fo->regex.get(k), ff->fname, 0, NULL, 0) == 0) {
if (ff->flags & FO_EXCLUDE) {
return false; /* reject file */
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/bregex.c
Expand Up @@ -1488,7 +1488,7 @@ void re_registers_to_regmatch(regexp_registers_t old_regs,
}
}

int regexec(regex_t * preg, const char *string, size_t nmatch,
int regexec(regex_t *preg, const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags)
{
int status;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/bsys.c
Expand Up @@ -49,8 +49,6 @@ int safer_unlink(const char *pathname, const char *regx)
int rc;
regex_t preg1;
char prbuf[500];
const int nmatch = 30;
regmatch_t pmatch[nmatch];
int rtn;

/* Name must start with working directory */
Expand All @@ -68,8 +66,10 @@ int safer_unlink(const char *pathname, const char *regx)
return ENOENT;
}

/* Unlink files that match regexes */
if (regexec(&preg1, pathname, nmatch, pmatch, 0) == 0) {
/*
* Unlink files that match regexes
*/
if (regexec(&preg1, pathname, 0, NULL, 0) == 0) {
Dmsg1(100, "safe_unlink unlinking: %s\n", pathname);
rtn = unlink(pathname);
} else {
Expand Down
7 changes: 3 additions & 4 deletions src/lib/var.c
Expand Up @@ -1054,10 +1054,9 @@ op_search_and_replace(
/* no (more) matching */
tokenbuf_append(&tmp, p, mydata.end - p);
break;
}
else if ( multiline
&& (p + pmatch[0].rm_so) == mydata.end
&& (pmatch[0].rm_eo - pmatch[0].rm_so) == 0) {
} else if (multiline &&
(p + pmatch[0].rm_so) == mydata.end &&
(pmatch[0].rm_eo - pmatch[0].rm_so) == 0) {
/* special case: found empty pattern (usually /^/ or /$/ only)
in multi-line at end of data (after the last newline) */
tokenbuf_append(&tmp, p, mydata.end - p);
Expand Down
4 changes: 1 addition & 3 deletions src/stored/stored.c
Expand Up @@ -500,8 +500,6 @@ static void cleanup_old_files()
POOLMEM *basename = get_pool_memory(PM_MESSAGE);
regex_t preg1;
char prbuf[500];
const int nmatch = 30;
regmatch_t pmatch[nmatch];
berrno be;

/* Look for .spool files but don't allow spaces */
Expand Down Expand Up @@ -547,7 +545,7 @@ static void cleanup_old_files()
}

/* Unlink files that match regex */
if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) {
if (regexec(&preg1, result->d_name, 0, NULL, 0) == 0) {
pm_strcpy(cleanup, basename);
pm_strcat(cleanup, result->d_name);
Dmsg1(500, "Unlink: %s\n", cleanup);
Expand Down
4 changes: 1 addition & 3 deletions src/tools/bregex.c
Expand Up @@ -147,11 +147,9 @@ int main(int argc, char *const *argv)
}
lineno = 0;
while (fgets(data, sizeof(data)-1, fd)) {
const int nmatch = 30;
regmatch_t pmatch[nmatch];
strip_trailing_newline(data);
lineno++;
rc = regexec(&preg, data, nmatch, pmatch, 0);
rc = regexec(&preg, data, 0, NULL, 0);
if ((match_only && rc == 0) || (!match_only && rc != 0)) {
if (no_linenos) {
printf("%s\n", data);
Expand Down

0 comments on commit 07f8fed

Please sign in to comment.