]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
smp: Make code that encodes identities more readable
authorTobias Brunner <tobias@strongswan.org>
Thu, 14 Mar 2024 12:51:06 +0000 (13:51 +0100)
committerTobias Brunner <tobias@strongswan.org>
Thu, 14 Mar 2024 12:51:06 +0000 (13:51 +0100)
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).

src/libcharon/plugins/smp/smp.c

index 950055b44786923eeed647b6f31313ce7a9c1232..6ca9f13997e123b988d3c3be4b1cef18909fe858 100644 (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);
 }