Commit 429725e1 authored by Geoff Simmons's avatar Geoff Simmons

reduce repetition in pcre2 error message reporting

parent c378fa6f
...@@ -110,14 +110,32 @@ errmsg(VRT_CTX, const char *fmt, ...) ...@@ -110,14 +110,32 @@ errmsg(VRT_CTX, const char *fmt, ...)
va_end(args); va_end(args);
} }
static inline int static void
get_pcre2_errmsg(struct ws * const restrict ws, int errcode, report_pcre2_err(VRT_CTX, int errcode, const char * const restrict msg,
char * restrict * const restrict buf) const char * const restrict post)
{ {
unsigned bytes = WS_Reserve(ws, 0); int ret;
*buf = WS_Front(ws);
return pcre2_get_error_message(errcode, (PCRE2_UCHAR *)*buf, uintptr_t snap = WS_Snapshot(ctx->ws);
unsigned bytes = WS_Reserve(ctx->ws, 0);
char *buf = WS_Front(ctx->ws);
ret = pcre2_get_error_message(errcode, (PCRE2_UCHAR *)buf,
(PCRE2_SIZE)bytes); (PCRE2_SIZE)bytes);
if (ret == PCRE2_ERROR_BADDATA) {
WS_ReleaseP(ctx->ws, buf);
VERR(ctx, "%s (unknown error code)%s", msg, post);
}
else if (ret == PCRE2_ERROR_NOMEMORY) {
WS_Release(ctx->ws, strlen(buf) + 1);
VERR(ctx, "%s: %s (truncated error message)%s", msg, buf,
post);
}
else {
WS_Release(ctx->ws, ret);
VERR(ctx, "%s: %s%s", msg, buf, post);
}
WS_Reset(ctx->ws, snap);
} }
/* Event function */ /* Event function */
...@@ -293,26 +311,17 @@ vmod_regex__init(VRT_CTX, struct vmod_pcre2_regex **regexp, ...@@ -293,26 +311,17 @@ vmod_regex__init(VRT_CTX, struct vmod_pcre2_regex **regexp,
code = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, code = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED,
options, &err_code, &err_offset, ccontext); options, &err_code, &err_offset, ccontext);
if (code == NULL) { if (code == NULL) {
char *buf; char *msg, *offset_msg;
uintptr_t snap = WS_Snapshot(ctx->ws); uintptr_t snap = WS_Snapshot(ctx->ws);
ret = get_pcre2_errmsg(ctx->ws, err_code, &buf);
if (ret == PCRE2_ERROR_BADDATA) { if ((msg = WS_Printf(ctx->ws,
WS_ReleaseP(ctx->ws, buf); "Cannot compile '%s' in %s constructor",
VERR(ctx, "Cannot compile '%s' in %s constructor " pattern, vcl_name)) == NULL)
"(unknown error code)", pattern, vcl_name); msg = "";
} if ((offset_msg = WS_Printf(ctx->ws, " at offset %zu",
else if (ret == PCRE2_ERROR_NOMEMORY) { err_offset)) == NULL)
WS_Release(ctx->ws, strlen(buf) + 1); offset_msg = "";
VERR(ctx, "Cannot compile '%s' in %s constructor: %s" report_pcre2_err(ctx, err_code, msg, offset_msg);
"(truncated error message) at offset %zu", pattern,
vcl_name, err_offset);
}
else {
WS_Release(ctx->ws, ret);
VERR(ctx, "Cannot compile '%s' in %s constructor: %s "
"at offset %zu", pattern, vcl_name, buf,
err_offset);
}
WS_Reset(ctx->ws, snap); WS_Reset(ctx->ws, snap);
pcre2_compile_context_free(ccontext); pcre2_compile_context_free(ccontext);
return; return;
...@@ -323,27 +332,14 @@ vmod_regex__init(VRT_CTX, struct vmod_pcre2_regex **regexp, ...@@ -323,27 +332,14 @@ vmod_regex__init(VRT_CTX, struct vmod_pcre2_regex **regexp,
options |= PCRE2_JIT_COMPLETE; options |= PCRE2_JIT_COMPLETE;
ret = pcre2_jit_compile(code, options); ret = pcre2_jit_compile(code, options);
if (ret != 0) { if (ret != 0) {
char *buf; char *msg;
uintptr_t snap = WS_Snapshot(ctx->ws); uintptr_t snap = WS_Snapshot(ctx->ws);
ret = get_pcre2_errmsg(ctx->ws, ret, &buf);
if (ret == PCRE2_ERROR_BADDATA) { if ((msg = WS_Printf(ctx->ws, "Cannot jit-compile '%s' "
WS_ReleaseP(ctx->ws, buf); "in %s constructor", pattern,
VERR(ctx, "Cannot jit-compile '%s' in %s " vcl_name)) == NULL)
"constructor (unknown error code)", msg = "";
pattern, vcl_name); report_pcre2_err(ctx, ret, msg, "");
}
else if (ret == PCRE2_ERROR_NOMEMORY) {
WS_Release(ctx->ws, strlen(buf) + 1);
VERR(ctx, "Cannot jit-compile '%s' in %s "
"constructor: %s (truncated error message)",
pattern, vcl_name);
}
else {
WS_Release(ctx->ws, ret);
VERR(ctx, "Cannot jit-compile '%s' in %s "
"constructor: %s ", pattern, vcl_name,
buf);
}
WS_Reset(ctx->ws, snap); WS_Reset(ctx->ws, snap);
pcre2_compile_context_free(ccontext); pcre2_compile_context_free(ccontext);
return; return;
...@@ -390,6 +386,8 @@ vmod_regex_match(VRT_CTX, struct vmod_pcre2_regex *regex, ...@@ -390,6 +386,8 @@ vmod_regex_match(VRT_CTX, struct vmod_pcre2_regex *regex,
struct vmod_priv *match_task = NULL; struct vmod_priv *match_task = NULL;
struct match_call *match_opts; struct match_call *match_opts;
int ret; int ret;
char *msg;
uintptr_t snap;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(regex, VMOD_PCRE2_REGEX_MAGIC); CHECK_OBJ_NOTNULL(regex, VMOD_PCRE2_REGEX_MAGIC);
...@@ -504,23 +502,11 @@ vmod_regex_match(VRT_CTX, struct vmod_pcre2_regex *regex, ...@@ -504,23 +502,11 @@ vmod_regex_match(VRT_CTX, struct vmod_pcre2_regex *regex,
if (ret >= 0) if (ret >= 0)
return 1; return 1;
char *buf; snap = WS_Snapshot(ctx->ws);
uintptr_t snap = WS_Snapshot(ctx->ws); if ((msg = WS_Printf(ctx->ws, "in %s.match()", regex->vcl_name))
ret = get_pcre2_errmsg(ctx->ws, ret, &buf); == NULL)
if (ret == PCRE2_ERROR_BADDATA) { msg = "";
WS_ReleaseP(ctx->ws, buf); report_pcre2_err(ctx, ret, msg, "");
VERR(ctx, "in %s.match() (unknown error code)",
regex->vcl_name);
}
else if (ret == PCRE2_ERROR_NOMEMORY) {
WS_Release(ctx->ws, strlen(buf) + 1);
VERR(ctx, "in %s.match(): %s (truncated error message)",
regex->vcl_name, buf);
}
else {
WS_Release(ctx->ws, ret);
VERR(ctx, "in %s.match(): %s ", regex->vcl_name, buf);
}
WS_Reset(ctx->ws, snap); WS_Reset(ctx->ws, snap);
return 0; return 0;
} }
......
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