From: Tobias Brunner Date: Thu, 14 Mar 2024 12:51:06 +0000 (+0100) Subject: smp: Make code that encodes identities more readable X-Git-Tag: 5.9.14~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c035e4ca9300fd952a7ff932f53b204894966513;p=thirdparty%2Fstrongswan.git smp: Make code that encodes identities more readable In particular for static code analyzers. The previous nesting of case statements inside of a while loop that's inside a switch statement and a wrapping block with declaration was quite weird and Coverity didn't like it (it figured that `type` was uninitialized even when it assumed that get_type() returned a known type for which a case statement existed). --- diff --git a/src/libcharon/plugins/smp/smp.c b/src/libcharon/plugins/smp/smp.c index 950055b447..6ca9f13997 100644 --- a/src/libcharon/plugins/smp/smp.c +++ b/src/libcharon/plugins/smp/smp.c @@ -76,45 +76,46 @@ static void write_bool(xmlTextWriterPtr writer, char *element, bool val) */ static void write_id(xmlTextWriterPtr writer, char *element, identification_t *id) { + char *type = NULL; + xmlTextWriterStartElement(writer, element); switch (id->get_type(id)) { - { - char *type; - while (TRUE) - { - case ID_ANY: - type = "any"; - break; - case ID_IPV4_ADDR: - type = "ipv4"; - break; - case ID_IPV6_ADDR: - type = "ipv6"; - break; - case ID_FQDN: - type = "fqdn"; - break; - case ID_RFC822_ADDR: - type = "email"; - break; - case ID_DER_ASN1_DN: - type = "asn1dn"; - break; - case ID_DER_ASN1_GN: - type = "asn1gn"; - break; - } - xmlTextWriterWriteAttribute(writer, "type", type); - xmlTextWriterWriteFormatString(writer, "%Y", id); + case ID_ANY: + type = "any"; + break; + case ID_IPV4_ADDR: + type = "ipv4"; + break; + case ID_IPV6_ADDR: + type = "ipv6"; + break; + case ID_FQDN: + type = "fqdn"; + break; + case ID_RFC822_ADDR: + type = "email"; + break; + case ID_DER_ASN1_DN: + type = "asn1dn"; + break; + case ID_DER_ASN1_GN: + type = "asn1gn"; break; - } default: - /* TODO: base64 keyid */ - xmlTextWriterWriteAttribute(writer, "type", "keyid"); break; } + if (type) + { + xmlTextWriterWriteAttribute(writer, "type", type); + xmlTextWriterWriteFormatString(writer, "%Y", id); + } + else + { + /* TODO: base64 keyid */ + xmlTextWriterWriteAttribute(writer, "type", "keyid"); + } xmlTextWriterEndElement(writer); }