Commit 025f5654 authored by Geoff Simmons's avatar Geoff Simmons

compute the length of the subject string only once in match()

parent 54295ce5
......@@ -151,7 +151,7 @@ VCL_BOOL
vmod_regex_match(const struct vrt_ctx *ctx, struct vmod_re2_regex *re,
VCL_STRING subject)
{
int match = 0, ngroups = 0;
int match = 0, ngroups = 0, len;
const char *err;
char *text = (void *) subject;
void *group = NULL;
......@@ -165,9 +165,10 @@ vmod_regex_match(const struct vrt_ctx *ctx, struct vmod_re2_regex *re,
AZ(pthread_setspecific(re->matchk, match_failed));
len = strlen(subject);
if (! re->never_capture) {
ngroups = re->ngroups + 1;
if ((text = WS_Copy(ctx->ws, subject, -1)) == NULL) {
if ((text = WS_Copy(ctx->ws, subject, len + 1)) == NULL) {
VERR(ctx, ERR_PREFIX "insufficient workspace to copy "
"subject", re->vcl_name, subject);
return 0;
......@@ -181,7 +182,7 @@ vmod_regex_match(const struct vrt_ctx *ctx, struct vmod_re2_regex *re,
}
}
if ((err = vre2_match(re->vre2, text, &match, ngroups, group))
if ((err = vre2_match(re->vre2, text, len, &match, ngroups, group))
!= NULL) {
VERR(ctx, ERR_PREFIX "%s", re->vcl_name, subject, err);
WS_Reset(ctx->ws, text);
......
......@@ -56,10 +56,10 @@ vre2::~vre2() {
}
inline bool
vre2::match(const char *subject, const int ngroups, StringPiece* groups) const
vre2::match(const char *subject, const size_t len, const int ngroups,
StringPiece* groups) const
{
return re_->Match(subject, 0, strlen(subject), RE2::UNANCHORED,
groups, ngroups);
return re_->Match(subject, 0, len, RE2::UNANCHORED, groups, ngroups);
}
inline const int
......@@ -106,12 +106,12 @@ vre2_matchsz(void)
}
const char *
vre2_match(vre2 *vre2, const char * const subject, int * const match,
const int ngroups, void * const group)
vre2_match(vre2 *vre2, const char * const subject, const size_t len,
int * const match, const int ngroups, void * const group)
{
try {
StringPiece* g = reinterpret_cast<StringPiece *>(group);
*match = vre2->match(subject, ngroups, g);
*match = vre2->match(subject, len, ngroups, g);
return NULL;
}
CATCHALL
......
......@@ -43,7 +43,8 @@ private:
public:
vre2(const char *pattern, RE2::Options * const opt);
virtual ~vre2();
bool match(const char *subject, int ngroups, StringPiece* groups) const;
bool match(const char *subject, size_t len, int ngroups,
StringPiece* groups) const;
const int ngroups() const;
};
#else
......@@ -65,7 +66,7 @@ extern "C" {
const size_t vre2_matchsz(void);
const char *vre2_ngroups(vre2 *vre2, int * const ngroups);
const char *vre2_match(vre2 *vre2, const char * const subject,
int * const match, int ngroups,
size_t len, int * const match, int ngroups,
void * const group);
const char *vre2_capture(void *group, int refnum,
const char ** const capture, int * const len);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment