Commit 5e872182 authored by Geoff Simmons's avatar Geoff Simmons

Consolidate code for range checking.

parent 9524c583
Pipeline #417 skipped
......@@ -83,6 +83,29 @@ struct vmod_dispatch_vcl {
static struct vrt_ctx dummy_ctx = { .magic = VRT_CTX_MAGIC };
static int
check_range(VRT_CTX, VCL_INT n, unsigned max,
struct vbitmap * const restrict bitmap,
const char * restrict const type, const char * restrict const name,
const char * restrict const method)
{
if (n < 0) {
VERR(ctx, "%s.%s(%ld): n must be >= 0", name, method, n);
return 0;
}
if (n >= max) {
VERR(ctx, "%s.%s(%ld): highest %s number is %d", name, method,
n, type, max - 1);
return 0;
}
if (!vbit_test(bitmap, n)) {
VERR(ctx, "%s.%s(%ld): %s %ld was not added", name, method, n,
type, n);
return 0;
}
return 1;
}
VCL_VOID
vmod_label__init(VRT_CTX, struct vmod_dispatch_label **labelp,
const char *vcl_name)
......@@ -193,20 +216,9 @@ vmod_label_go(VRT_CTX, struct vmod_dispatch_label *label, VCL_INT n)
n);
return;
}
if (n < 0) {
VERR(ctx, "%s.go(%ld): n must be >= 0", label->vcl_name, n);
return;
}
if (n >= label->nvcls) {
VERR(ctx, "%s.go(%ld): highest label number is %d",
label->vcl_name, n, label->nvcls - 1);
return;
}
if (!vbit_test(label->bitmap, n)) {
VERR(ctx, "%s.go(%ld): label %ld was not added",
label->vcl_name, n, n);
if (!check_range(ctx, n, label->nvcls, label->bitmap, "label",
label->vcl_name, "go"))
return;
}
VRT_vcl_select(ctx, label->vcl[n]);
VRT_handling(ctx, VCL_RET_VCL);
}
......@@ -217,20 +229,9 @@ vmod_label_string(VRT_CTX, struct vmod_dispatch_label *label, VCL_INT n)
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(label, VMOD_DISPATCH_LABEL_MAGIC);
if (n < 0) {
VERR(ctx, "%s.string(%ld): n must be >= 0", label->vcl_name, n);
return NULL;
}
if (n >= label->nvcls) {
VERR(ctx, "%s.string(%ld): highest label number is %d",
label->vcl_name, n, label->nvcls - 1);
if (!check_range(ctx, n, label->nvcls, label->bitmap, "label",
label->vcl_name, "string"))
return NULL;
}
if (!vbit_test(label->bitmap, n)) {
VERR(ctx, "%s.string(%ld): label %ld was not added",
label->vcl_name, n, n);
return NULL;
}
AN(label->string[n]);
return label->string[n];
}
......@@ -379,20 +380,9 @@ vmod_sub_call(VRT_CTX, struct vmod_dispatch_sub *sub, VCL_INT n)
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(sub, VMOD_DISPATCH_SUB_MAGIC);
if (n < 0) {
VERR(ctx, "%s.call(%ld): n must be >= 0", sub->vcl_name, n);
return;
}
if (n >= sub->nsubs) {
VERR(ctx, "%s.call(%ld): highest sub number is %d",
sub->vcl_name, n, sub->nsubs - 1);
return;
}
if (!vbit_test(sub->bitmap, n)) {
VERR(ctx, "%s.call(%ld): sub %ld was not added", sub->vcl_name,
n, n);
if (!check_range(ctx, n, sub->nsubs, sub->bitmap, "sub", sub->vcl_name,
"call"))
return;
}
AN(sub->func[n]);
sub->func[n](ctx);
}
......@@ -403,20 +393,9 @@ vmod_sub_string(VRT_CTX, struct vmod_dispatch_sub *sub, VCL_INT n)
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(sub, VMOD_DISPATCH_SUB_MAGIC);
if (n < 0) {
VERR(ctx, "%s.string(%ld): n must be >= 0", sub->vcl_name, n);
return NULL;
}
if (n >= sub->nsubs) {
VERR(ctx, "%s.string(%ld): highest sub number is %d",
sub->vcl_name, n, sub->nsubs - 1);
if (!check_range(ctx, n, sub->nsubs, sub->bitmap, "sub", sub->vcl_name,
"string"))
return NULL;
}
if (!vbit_test(sub->bitmap, n)) {
VERR(ctx, "%s.string(%ld): sub %ld was not added",
sub->vcl_name, n, n);
return NULL;
}
AN(sub->string[n]);
return sub->string[n];
}
......
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