Commit aee01e64 authored by Geoff Simmons's avatar Geoff Simmons

Consolidate repetitive error-checking code.

parent 2270eafb
......@@ -74,6 +74,33 @@ VPFX(event)(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
NEEDLESS(return (0));
}
#define CHECK_ID(ctx, id, len, ret) \
do { \
if ((id) == NULL) { \
VRT_fail((ctx), "key id is NULL"); \
return ret; \
} \
len = strlen(id); \
if ((len) > 255) { \
VRT_fail((ctx), "key id \"%.80s...\" too long " \
"(length %zu > 255)", (id), (len)); \
return ret; \
} \
} while(0)
#define CHECK_KEY(ctx, key) \
do { \
if ((key) == NULL || (key)->blob == NULL) { \
VRT_fail((ctx), "key contents are empty"); \
return; \
} \
if ((key)->len != 16) { \
VRT_fail((ctx), "illegal key length %zu (must be 16)", \
(key)->len); \
return; \
} \
} while(0)
VCL_VOID
vmod_add_key(VRT_CTX, VCL_STRING id, VCL_BLOB key)
{
......@@ -81,24 +108,8 @@ vmod_add_key(VRT_CTX, VCL_STRING id, VCL_BLOB key)
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (id == NULL) {
VRT_fail(ctx, "key id is NULL");
return;
}
if (key == NULL || key->blob == NULL) {
VRT_fail(ctx, "key contents are empty");
return;
}
if (key->len != 16) {
VRT_fail(ctx, "illegal key length %zu (must be 16)", key->len);
return;
}
len = strlen(id);
if (len > 255) {
VRT_fail(ctx, "key id \"%.80s...\" too long (length %zu > 255)",
id, len);
return;
}
CHECK_ID(ctx, id, len,);
CHECK_KEY(ctx, key);
/* KEY_Add calls VRT_fail() on error. */
(void)KEY_Add(ctx, (uint8_t *)id, (uint8_t)len, key->blob);
......@@ -110,25 +121,8 @@ vmod_set_key(VRT_CTX, VCL_STRING id, VCL_BLOB key)
size_t len;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (id == NULL) {
VRT_fail(ctx, "key id is NULL");
return;
}
if (key == NULL || key->blob == NULL) {
VRT_fail(ctx, "key contents are empty");
return;
}
if (key->len != 16) {
VRT_fail(ctx, "illegal key length %zu (must be 16)", key->len);
return;
}
len = strlen(id);
if (len > 255) {
VRT_fail(ctx, "key id \"%.80s...\" too long (length %zu > 255)",
id, len);
return;
}
CHECK_ID(ctx, id, len,);
CHECK_KEY(ctx, key);
/* KEY_Set calls VRT_fail() on error. */
(void)KEY_Set(ctx, (uint8_t *)id, (uint8_t)len, key->blob);
......@@ -140,17 +134,7 @@ vmod_delete_key(VRT_CTX, VCL_STRING id)
size_t len;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (id == NULL) {
VRT_fail(ctx, "key id is NULL");
return;
}
len = strlen(id);
if (len > 255) {
VRT_fail(ctx, "key id \"%.80s...\" too long (length %zu > 255)",
id, len);
return;
}
CHECK_ID(ctx, id, len,);
/* KEY_Delete calls VRT_fail() on error. */
(void)KEY_Delete(ctx, (uint8_t *)id, (uint8_t)len);
......@@ -162,17 +146,7 @@ vmod_key_exists(VRT_CTX, VCL_STRING id)
size_t len;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (id == NULL) {
VRT_fail(ctx, "key id is NULL");
return (0);
}
len = strlen(id);
if (len > 255) {
VRT_fail(ctx, "key id \"%.80s...\" too long (length %zu > 255)",
id, len);
return (0);
}
CHECK_ID(ctx, id, len, 0);
return (KEY_Exists((uint8_t *)id, (uint8_t)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