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