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).
This commit is contained in:
Tobias Brunner 2024-03-14 13:51:06 +01:00
parent 91f209b878
commit c035e4ca93

View File

@ -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);
}