Skip to content

Commit

Permalink
Fix back-reference parsing in mutt_replacelist_add
Browse files Browse the repository at this point in the history
  • Loading branch information
gahr authored Nov 10, 2021
1 parent b680fe0 commit f5e40b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
18 changes: 12 additions & 6 deletions mutt/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Expand Down Expand Up @@ -313,13 +314,18 @@ int mutt_replacelist_add(struct ReplaceList *rl, const char *pat,
{
if (*p == '%')
{
int n = 0;
if (mutt_str_atoi(++p, &n) < 0)
char *end = NULL;
errno = 0;
int n = strtol(++p, &end, 10);
if (errno != 0)
{
mutt_debug(LL_DEBUG2, "Invalid match number in replacelist: '%s'\n", p);
if (n > np->nmatch)
}
else if (n > np->nmatch)
{
np->nmatch = n;
while (*p && isdigit((int) *p))
p++;
}
p = end;
}
else
p++;
Expand Down Expand Up @@ -463,9 +469,9 @@ void mutt_replacelist_free(struct ReplaceList *rl)
/**
* mutt_replacelist_match - Does a string match a pattern?
* @param rl ReplaceList of patterns
* @param str String to check
* @param buf Buffer to save match
* @param buflen Buffer length
* @param str String to check
* @retval true String matches a patterh in the ReplaceList
*
* Match a string against the patterns defined by the 'spam' command and output
Expand Down
24 changes: 24 additions & 0 deletions test/regex/mutt_replacelist_match.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define TEST_NO_MAIN
#include "config.h"
#include "acutest.h"
#include "test_common.h"
#include "mutt/lib.h"

void test_mutt_replacelist_match(void)
Expand All @@ -44,4 +45,27 @@ void test_mutt_replacelist_match(void)
char buf[32] = { 0 };
TEST_CHECK(!mutt_replacelist_match(&replacelist, buf, sizeof(buf), NULL));
}

{
struct ReplaceList replacelist = STAILQ_HEAD_INITIALIZER(replacelist);
mutt_replacelist_add(&replacelist, "foo-([^-]+)-bar", "foo [%0] bar", NULL);
char buf[32] = { 0 };
TEST_CHECK(mutt_replacelist_match(&replacelist, buf, sizeof(buf), "foo-1234-bar"));
TEST_CHECK_STR_EQ("foo [foo-1234-bar] bar", buf);
}

{
struct ReplaceList replacelist = STAILQ_HEAD_INITIALIZER(replacelist);
mutt_replacelist_add(&replacelist, "foo-([^-]+)-bar", "foo [%1] bar", NULL);
char buf[32] = { 0 };
TEST_CHECK(mutt_replacelist_match(&replacelist, buf, sizeof(buf), "foo-1234-bar"));
TEST_CHECK_STR_EQ("foo [1234] bar", buf);
}

{
struct ReplaceList replacelist = STAILQ_HEAD_INITIALIZER(replacelist);
mutt_replacelist_add(&replacelist, "foo-([^-]+)-bar", "foo [%2] bar", NULL);
char buf[32] = { 0 };
TEST_CHECK(!mutt_replacelist_match(&replacelist, buf, sizeof(buf), "foo-1234-bar"));
}
}

0 comments on commit f5e40b0

Please sign in to comment.