Browse Source

try to fix bug with spurious "everything is broken" warning

svn:r5994
Nick Mathewson 19 years ago
parent
commit
2cc66125b8
2 changed files with 11 additions and 2 deletions
  1. 6 2
      src/common/util.c
  2. 5 0
      src/or/test.c

+ 6 - 2
src/common/util.c

@@ -1426,8 +1426,12 @@ int
 addr_mask_get_bits(uint32_t mask)
 addr_mask_get_bits(uint32_t mask)
 {
 {
   int i;
   int i;
+  if (mask == 0)
+    return 0;
+  if (mask == 0xFFFFFFFFu)
+    return 32;
   for (i=0; i<=32; ++i) {
   for (i=0; i<=32; ++i) {
-    if (mask == ~((1<<(32-i))-1)) {
+    if (mask == (uint32_t) ~((1u<<(32-i))-1)) {
       return i;
       return i;
     }
     }
   }
   }
@@ -1493,7 +1497,7 @@ parse_addr_and_port_range(const char *s, uint32_t *addr_out,
              "Bad number of mask bits on address range; rejecting.");
              "Bad number of mask bits on address range; rejecting.");
         goto err;
         goto err;
       }
       }
-      *mask_out = ~((1<<(32-bits))-1);
+      *mask_out = ~((1u<<(32-bits))-1);
     } else if (tor_inet_aton(mask, &in) != 0) {
     } else if (tor_inet_aton(mask, &in) != 0) {
       *mask_out = ntohl(in.s_addr);
       *mask_out = ntohl(in.s_addr);
     } else {
     } else {

+ 5 - 0
src/or/test.c

@@ -835,6 +835,11 @@ test_util(void)
   test_eq(u32, 0x7f000001u);
   test_eq(u32, 0x7f000001u);
   test_eq(u16, 0);
   test_eq(u16, 0);
   tor_free(cp);
   tor_free(cp);
+  test_eq(0, addr_mask_get_bits(0x0u));
+  test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
+  test_eq(16, addr_mask_get_bits(0xFFFF0000u));
+  test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
+  test_eq(1, addr_mask_get_bits(0x80000000u));
 
 
   /* Test tor_parse_long. */
   /* Test tor_parse_long. */
   test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
   test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));