enum: Return boolean result for enum_from_name() lookup

Handling the result for enum_from_name() is difficult, as checking for
negative return values requires a cast if the enum type is unsigned. The new
signature clearly differentiates lookup result from lookup value.

Further, this actually allows to convert real -1 enum values, which could not
be distinguished from "not-found" and the -1 return value.

This also fixes several clang warnings where enums are unsigned.
This commit is contained in:
Martin Willi
2014-05-16 15:42:07 +02:00
parent 9ee8b3b41f
commit 064fe9c963
28 changed files with 102 additions and 85 deletions
+1 -2
View File
@@ -43,8 +43,7 @@ int main(int argc, char *argv[])
limit = atoi(argv[2]); limit = atoi(argv[2]);
} }
alg = enum_from_name(hash_algorithm_short_names, argv[1]); if (!enum_from_name(hash_algorithm_short_names, argv[1], &alg))
if (alg == -1)
{ {
fprintf(stderr, "unknown hash algorthm: %s\n", argv[1]); fprintf(stderr, "unknown hash algorthm: %s\n", argv[1]);
return 1; return 1;
+2 -3
View File
@@ -460,10 +460,9 @@ static void add_ts(private_cmd_connection_t *this,
*/ */
static void set_profile(private_cmd_connection_t *this, char *name) static void set_profile(private_cmd_connection_t *this, char *name)
{ {
int profile; profile_t profile;
profile = enum_from_name(profile_names, name); if (!enum_from_name(profile_names, name, &profile))
if (profile == -1)
{ {
DBG1(DBG_CFG, "unknown connection profile: %s", name); DBG1(DBG_CFG, "unknown connection profile: %s", name);
exit(1); exit(1);
+1 -2
View File
@@ -73,8 +73,7 @@ METHOD(listener_t, message, bool,
type = atoi(this->type); type = atoi(this->type);
if (!type) if (!type)
{ {
type = enum_from_name(notify_type_names, this->type); if (!enum_from_name(notify_type_names, this->type, &type))
if (type == -1)
{ {
DBG1(DBG_CFG, "unknown notify: '%s', skipped", this->type); DBG1(DBG_CFG, "unknown notify: '%s', skipped", this->type);
return TRUE; return TRUE;
+1 -2
View File
@@ -77,8 +77,7 @@ METHOD(listener_t, message, bool,
type = atoi(this->type); type = atoi(this->type);
if (!type) if (!type)
{ {
type = enum_from_name(payload_type_short_names, this->type); if (!enum_from_name(payload_type_short_names, this->type, &type))
if (type == -1)
{ {
DBG1(DBG_CFG, "unknown payload: '%s', skipped", this->type); DBG1(DBG_CFG, "unknown payload: '%s', skipped", this->type);
return TRUE; return TRUE;
+1 -2
View File
@@ -79,8 +79,7 @@ static linked_list_t* load_proposals(private_custom_proposal_t *this,
type = strtoul(key, &end, 10); type = strtoul(key, &end, 10);
if (end == key || errno) if (end == key || errno)
{ {
type = enum_from_name(transform_type_names, key); if (!enum_from_name(transform_type_names, key, &type))
if (type == -1)
{ {
DBG1(DBG_CFG, "unknown transform: '%s', skipped", key); DBG1(DBG_CFG, "unknown transform: '%s', skipped", key);
continue; continue;
+1 -2
View File
@@ -65,8 +65,7 @@ METHOD(listener_t, message, bool,
type = atoi(name); type = atoi(name);
if (!type) if (!type)
{ {
type = enum_from_name(payload_type_short_names, name); if (!enum_from_name(payload_type_short_names, name, &type))
if (type == -1)
{ {
DBG1(DBG_CFG, "invalid payload name '%s'", name); DBG1(DBG_CFG, "invalid payload name '%s'", name);
break; break;
+1 -2
View File
@@ -63,8 +63,7 @@ METHOD(listener_t, message, bool,
type = atoi(this->type); type = atoi(this->type);
if (!type) if (!type)
{ {
type = enum_from_name(payload_type_short_names, this->type); if (!enum_from_name(payload_type_short_names, this->type, &type))
if (type == -1)
{ {
DBG1(DBG_CFG, "unknown payload: '%s', skipped", this->type); DBG1(DBG_CFG, "unknown payload: '%s', skipped", this->type);
return TRUE; return TRUE;
+1 -2
View File
@@ -181,8 +181,7 @@ METHOD(listener_t, message, bool,
type = atoi(name); type = atoi(name);
if (!type) if (!type)
{ {
type = enum_from_name(payload_type_short_names, name); if (!enum_from_name(payload_type_short_names, name, &type))
if (type == -1)
{ {
DBG1(DBG_CFG, "invalid payload name '%s'", name); DBG1(DBG_CFG, "invalid payload name '%s'", name);
break; break;
+1 -2
View File
@@ -68,8 +68,7 @@ METHOD(listener_t, ike_updown, bool,
type = atoi(this->type); type = atoi(this->type);
if (!type) if (!type)
{ {
type = enum_from_name(notify_type_names, this->type); if (!enum_from_name(notify_type_names, this->type, &type))
if (type == -1)
{ {
DBG1(DBG_CFG, "unknown notify: '%s', skipped", this->type); DBG1(DBG_CFG, "unknown notify: '%s', skipped", this->type);
return TRUE; return TRUE;
+1 -2
View File
@@ -69,8 +69,7 @@ METHOD(listener_t, message, bool,
order = enumerator_create_token(this->order, ", ", " "); order = enumerator_create_token(this->order, ", ", " ");
while (order->enumerate(order, &name)) while (order->enumerate(order, &name))
{ {
type = enum_from_name(payload_type_short_names, name); if (enum_from_name(payload_type_short_names, name, &type))
if (type != -1)
{ {
enumerator = list->create_enumerator(list); enumerator = list->create_enumerator(list);
while (enumerator->enumerate(enumerator, &payload)) while (enumerator->enumerate(enumerator, &payload))
@@ -202,6 +202,7 @@ METHOD(coupling_validator_t, destroy, void,
coupling_validator_t *coupling_validator_create() coupling_validator_t *coupling_validator_create()
{ {
private_coupling_validator_t *this; private_coupling_validator_t *this;
hash_algorithm_t alg;
char *path, *hash; char *path, *hash;
INIT(this, INIT(this,
@@ -219,8 +220,13 @@ coupling_validator_t *coupling_validator_create()
hash = lib->settings->get_str(lib->settings, hash = lib->settings->get_str(lib->settings,
"%s.plugins.coupling.hash", "sha1", lib->ns); "%s.plugins.coupling.hash", "sha1", lib->ns);
this->hasher = lib->crypto->create_hasher(lib->crypto, if (!enum_from_name(hash_algorithm_short_names, hash, &alg))
enum_from_name(hash_algorithm_short_names, hash)); {
DBG1(DBG_CFG, "unknown coupling hash algorithm: %s", hash);
destroy(this);
return NULL;
}
this->hasher = lib->crypto->create_hasher(lib->crypto, alg);
if (!this->hasher) if (!this->hasher)
{ {
DBG1(DBG_CFG, "unsupported coupling hash algorithm: %s", hash); DBG1(DBG_CFG, "unsupported coupling hash algorithm: %s", hash);
@@ -362,8 +362,7 @@ static linked_list_t* parse_selector(char *selector)
vendor = atoi(token); vendor = atoi(token);
token = pos; token = pos;
} }
type = enum_from_name(radius_attribute_type_names, token); if (!enum_from_name(radius_attribute_type_names, token, &type))
if (type == -1)
{ {
type = atoi(token); type = atoi(token);
} }
@@ -465,7 +465,6 @@ load_tester_creds_t *load_tester_creds_create()
.private = load_issuer_key(), .private = load_issuer_key(),
.ca = load_issuer_cert(), .ca = load_issuer_cert(),
.cas = linked_list_create(), .cas = linked_list_create(),
.digest = enum_from_name(hash_algorithm_short_names, digest),
.psk = shared_key_create(SHARED_IKE, .psk = shared_key_create(SHARED_IKE,
chunk_clone(chunk_create(psk, strlen(psk)))), chunk_clone(chunk_create(psk, strlen(psk)))),
.pwd = shared_key_create(SHARED_EAP, .pwd = shared_key_create(SHARED_EAP,
@@ -477,7 +476,7 @@ load_tester_creds_t *load_tester_creds_create()
this->cas->insert_last(this->cas, this->ca->get_ref(this->ca)); this->cas->insert_last(this->cas, this->ca->get_ref(this->ca));
} }
if (this->digest == -1) if (!enum_from_name(hash_algorithm_short_names, digest, &this->digest))
{ {
DBG1(DBG_CFG, "invalid load-tester digest: '%s', using sha1", digest); DBG1(DBG_CFG, "invalid load-tester digest: '%s', using sha1", digest);
this->digest = HASH_SHA1; this->digest = HASH_SHA1;
@@ -487,4 +486,3 @@ load_tester_creds_t *load_tester_creds_create()
return &this->public; return &this->public;
} }
+1 -2
View File
@@ -584,8 +584,7 @@ static void stroke_loglevel(private_stroke_socket_t *this,
} }
else else
{ {
group = enum_from_name(debug_names, msg->loglevel.type); if (!enum_from_name(debug_names, msg->loglevel.type, &group))
if ((int)group < 0)
{ {
fprintf(out, "unknown type '%s'!\n", msg->loglevel.type); fprintf(out, "unknown type '%s'!\n", msg->loglevel.type);
return; return;
+1 -2
View File
@@ -729,8 +729,7 @@ CALLBACK(list_certs, vici_message_t*,
char *str; char *str;
str = request->get_str(request, "ANY", "type"); str = request->get_str(request, "ANY", "type");
type = enum_from_name(certificate_type_names, str); if (!enum_from_name(certificate_type_names, str, &type))
if (type == -1)
{ {
b = vici_builder_create(); b = vici_builder_create();
return b->finalize(b); return b->finalize(b);
+30 -21
View File
@@ -120,41 +120,50 @@ END_TEST
*/ */
static struct { static struct {
bool found;
int val; int val;
char *str; char *str;
} enum_tests_cont[] = { } enum_tests_cont[] = {
{CONT1, "CONT1"}, {TRUE, CONT1, "CONT1"},
{CONT2, "CONT2"}, {TRUE, CONT2, "CONT2"},
{CONT2, "CoNt2"}, {TRUE, CONT2, "CoNt2"},
{CONT3, "CONT3"}, {TRUE, CONT3, "CONT3"},
{CONT4, "CONT4"}, {TRUE, CONT4, "CONT4"},
{CONT5, "CONT5"}, {TRUE, CONT5, "CONT5"},
{-1, "asdf"}, {FALSE, 0, "asdf"},
{-1, ""}, {FALSE, 0, ""},
{-1, NULL}, {FALSE, 0, NULL},
}, enum_tests_split[] = { }, enum_tests_split[] = {
{SPLIT1, "SPLIT1"}, {TRUE, SPLIT1, "SPLIT1"},
{SPLIT1, "split1"}, {TRUE, SPLIT1, "split1"},
{SPLIT2, "SPLIT2"}, {TRUE, SPLIT2, "SPLIT2"},
{SPLIT2, "SpLiT2"}, {TRUE, SPLIT2, "SpLiT2"},
{SPLIT3, "SPLIT3"}, {TRUE, SPLIT3, "SPLIT3"},
{SPLIT4, "SPLIT4"}, {TRUE, SPLIT4, "SPLIT4"},
{SPLIT5, "SPLIT5"}, {TRUE, SPLIT5, "SPLIT5"},
{-1, "asdf"}, {FALSE, 0, "asdf"},
{-1, ""}, {FALSE, 0, ""},
{-1, NULL}, {FALSE, 0, NULL},
}; };
START_TEST(test_enum_from_name_cont) START_TEST(test_enum_from_name_cont)
{ {
int val = enum_from_name(test_enum_cont_names, enum_tests_cont[_i].str); int val = 0;
bool found;
found = enum_from_name(test_enum_cont_names, enum_tests_cont[_i].str, &val);
ck_assert(enum_tests_cont[_i].found == found);
ck_assert_int_eq(val, enum_tests_cont[_i].val); ck_assert_int_eq(val, enum_tests_cont[_i].val);
} }
END_TEST END_TEST
START_TEST(test_enum_from_name_split) START_TEST(test_enum_from_name_split)
{ {
int val = enum_from_name(test_enum_split_names, enum_tests_split[_i].str); int val = 0;
bool found;
found = enum_from_name(test_enum_split_names, enum_tests_split[_i].str, &val);
ck_assert(enum_tests_split[_i].found == found);
ck_assert_int_eq(val, enum_tests_split[_i].val); ck_assert_int_eq(val, enum_tests_split[_i].val);
} }
END_TEST END_TEST
+4 -3
View File
@@ -40,7 +40,7 @@ char *enum_to_name(enum_name_t *e, int val)
/** /**
* See header. * See header.
*/ */
int enum_from_name(enum_name_t *e, char *name) bool enum_from_name_as_int(enum_name_t *e, const char *name, int *val)
{ {
do do
{ {
@@ -50,12 +50,13 @@ int enum_from_name(enum_name_t *e, char *name)
{ {
if (name && strcaseeq(name, e->names[i])) if (name && strcaseeq(name, e->names[i]))
{ {
return e->first + i; *val = e->first + i;
return TRUE;
} }
} }
} }
while ((e = e->next)); while ((e = e->next));
return -1; return FALSE;
} }
/** /**
+23 -2
View File
@@ -120,9 +120,30 @@ char *enum_to_name(enum_name_t *e, int val);
* *
* @param e enum names for this enum value * @param e enum names for this enum value
* @param name name to get enum value for * @param name name to get enum value for
* @return enum value, -1 if not found * @þaram valp variable sized pointer receiving value
* @return TRUE if enum name found, FALSE otherwise
*/ */
int enum_from_name(enum_name_t *e, char *name); #define enum_from_name(e, name, valp) ({ \
int _val; \
int _found = enum_from_name_as_int(e, name, &_val); \
if (_found) \
{ \
*(valp) = _val; \
} \
_found; })
/**
* Convert a enum string back to its enum value, integer pointer variant.
*
* This variant takes integer pointer only, use enum_from_name() to pass
* enum type pointers for the result.
*
* @param e enum names for this enum value
* @param name name to get enum value for
* @þaram val integer pointer receiving value
* @return TRUE if enum name found, FALSE otherwise
*/
bool enum_from_name_as_int(enum_name_t *e, const char *name, int *val);
/** /**
* printf hook function for enum_names_t. * printf hook function for enum_names_t.
+2 -2
View File
@@ -959,8 +959,8 @@ static void filter_specific_config_suites(private_tls_crypto_t *this,
enumerator = enumerator_create_token(config, ",", " "); enumerator = enumerator_create_token(config, ",", " ");
while (enumerator->enumerate(enumerator, &token)) while (enumerator->enumerate(enumerator, &token))
{ {
suite = enum_from_name(tls_cipher_suite_names, token); if (enum_from_name(tls_cipher_suite_names, token, &suite) &&
if (suite == suites[i].suite) suite == suites[i].suite)
{ {
suites[remaining++] = suites[i]; suites[remaining++] = suites[i];
break; break;
@@ -432,7 +432,7 @@ METHOD(imv_manager_t, destroy, void,
imv_manager_t* tnc_imv_manager_create(void) imv_manager_t* tnc_imv_manager_create(void)
{ {
private_tnc_imv_manager_t *this; private_tnc_imv_manager_t *this;
recommendation_policy_t policy; char *polname;
INIT(this, INIT(this,
.public = { .public = {
@@ -458,11 +458,12 @@ imv_manager_t* tnc_imv_manager_create(void)
.next_imv_id = 1, .next_imv_id = 1,
); );
policy = enum_from_name(recommendation_policy_names, polname = lib->settings->get_str(lib->settings,
lib->settings->get_str(lib->settings, "%s.plugins.tnc-imv.recommendation_policy", "default", lib->ns);
"%s.plugins.tnc-imv.recommendation_policy", if (!enum_from_name(recommendation_policy_names, polname, &this->policy))
"default", lib->ns)); {
this->policy = (policy != -1) ? policy : RECOMMENDATION_POLICY_DEFAULT; this->policy = RECOMMENDATION_POLICY_DEFAULT;
}
DBG1(DBG_TNC, "TNC recommendation policy is '%N'", DBG1(DBG_TNC, "TNC recommendation policy is '%N'",
recommendation_policy_names, this->policy); recommendation_policy_names, this->policy);
@@ -128,9 +128,8 @@ tnccs_msg_t *tnccs_error_msg_create_from_node(xmlNodePtr node)
error_type_name = xmlGetProp(node, "type"); error_type_name = xmlGetProp(node, "type");
if (error_type_name) if (error_type_name)
{ {
this->error_type = enum_from_name(tnccs_error_type_names, if (!enum_from_name(tnccs_error_type_names, error_type_name,
error_type_name); &this->error_type))
if (this->error_type == -1)
{ {
this->error_type = TNCCS_ERROR_OTHER; this->error_type = TNCCS_ERROR_OTHER;
} }
@@ -41,7 +41,7 @@ tnccs_msg_t* tnccs_msg_create_from_node(xmlNodePtr node, linked_list_t *errors)
char *error_msg, buf[BUF_LEN]; char *error_msg, buf[BUF_LEN];
tnccs_error_type_t error_type = TNCCS_ERROR_MALFORMED_BATCH; tnccs_error_type_t error_type = TNCCS_ERROR_MALFORMED_BATCH;
tnccs_msg_t *msg; tnccs_msg_t *msg;
tnccs_msg_type_t type = IMC_IMV_MSG; tnccs_msg_type_t type = IMC_IMV_MSG, nametype;
if (streq((char*)node->name, "IMC-IMV-Message")) if (streq((char*)node->name, "IMC-IMV-Message"))
{ {
@@ -103,7 +103,8 @@ tnccs_msg_t* tnccs_msg_create_from_node(xmlNodePtr node, linked_list_t *errors)
error_msg = "node is not in the TNCCS message namespace"; error_msg = "node is not in the TNCCS message namespace";
goto fatal; goto fatal;
} }
if (type != enum_from_name(tnccs_msg_type_names, (char*)cur->name)) if (!enum_from_name(tnccs_msg_type_names, cur->name, &nametype) ||
type != nametype)
{ {
error_msg = buf; error_msg = buf;
snprintf(buf, BUF_LEN, "expected '%N' node but was '%s'", snprintf(buf, BUF_LEN, "expected '%N' node but was '%s'",
@@ -137,4 +138,3 @@ fatal:
errors->insert_last(errors, msg); errors->insert_last(errors, msg);
return NULL; return NULL;
} }
+1 -2
View File
@@ -53,8 +53,7 @@ static int acert()
case 'h': case 'h':
goto usage; goto usage;
case 'g': case 'g':
digest = enum_from_name(hash_algorithm_short_names, arg); if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
if (digest == -1)
{ {
error = "invalid --digest type"; error = "invalid --digest type";
goto usage; goto usage;
+1 -2
View File
@@ -106,8 +106,7 @@ static int issue()
} }
continue; continue;
case 'g': case 'g':
digest = enum_from_name(hash_algorithm_short_names, arg); if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
if (digest == -1)
{ {
error = "invalid --digest type"; error = "invalid --digest type";
goto usage; goto usage;
+1 -2
View File
@@ -64,8 +64,7 @@ static int req()
} }
continue; continue;
case 'g': case 'g':
digest = enum_from_name(hash_algorithm_short_names, arg); if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
if (digest == -1)
{ {
error = "invalid --digest type"; error = "invalid --digest type";
goto usage; goto usage;
+1 -2
View File
@@ -95,8 +95,7 @@ static int self()
} }
continue; continue;
case 'g': case 'g':
digest = enum_from_name(hash_algorithm_short_names, arg); if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
if (digest == -1)
{ {
error = "invalid --digest type"; error = "invalid --digest type";
goto usage; goto usage;
+1 -2
View File
@@ -142,8 +142,7 @@ static int sign_crl()
case 'h': case 'h':
goto usage; goto usage;
case 'g': case 'g':
digest = enum_from_name(hash_algorithm_short_names, arg); if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
if (digest == -1)
{ {
error = "invalid --digest type"; error = "invalid --digest type";
goto usage; goto usage;
+3 -3
View File
@@ -556,10 +556,10 @@ CALLBACK(list_cb, void,
bool has_privkey; bool has_privkey;
buf = vici_find(res, &len, "data"); buf = vici_find(res, &len, "data");
type = enum_from_name(certificate_type_names,
vici_find_str(res, "ANY", "type"));
has_privkey = streq(vici_find_str(res, "no", "has_privkey"), "yes"); has_privkey = streq(vici_find_str(res, "no", "has_privkey"), "yes");
if (type != -1 && type != CERT_ANY && buf) if (enum_from_name(certificate_type_names,
vici_find_str(res, "ANY", "type"), &type) &&
type != CERT_ANY && buf)
{ {
cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, type, cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, type,
BUILD_BLOB_ASN1_DER, chunk_create(buf, len), BUILD_BLOB_ASN1_DER, chunk_create(buf, len),