attr: Don't shift the 32-bit netmask by 32
authorTobias Brunner <tobias@strongswan.org>
Wed, 9 Apr 2014 15:09:55 +0000 (17:09 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 9 Apr 2014 15:09:55 +0000 (17:09 +0200)
This is undefined behavior as per the C99 standard (sentence 1185):

 "If the value of the right operand is negative or is greater or equal
  to the width of the promoted left operand, the behavior is undefined."

Apparently shifts may be done modulo the width on some platforms so
a shift by 32 would not shift at all.

src/libhydra/plugins/attr/attr_provider.c

index a27fd57..c1788df 100644 (file)
@@ -242,10 +242,13 @@ static void load_entries(private_attr_provider_t *this)
                                {
                                        if (family == AF_INET)
                                        {       /* IPv4 attributes contain a subnet mask */
-                                               u_int32_t netmask;
+                                               u_int32_t netmask = 0;
 
-                                               mask = 32 - mask;
-                                               netmask = htonl((0xFFFFFFFF >> mask) << mask);
+                                               if (mask)
+                                               {       /* shifting u_int32_t by 32 or more is undefined */
+                                                       mask = 32 - mask;
+                                                       netmask = htonl((0xFFFFFFFF >> mask) << mask);
+                                               }
                                                data = chunk_cat("cc", host->get_address(host),
                                                                                 chunk_from_thing(netmask));
                                        }