[systemd-devel] [PATCH] fix in_addr_prefix_intersect for 32bits

Marc-Antoine Perennou Marc-Antoine at Perennou.com
Sun Jun 22 03:36:03 PDT 2014


shifting from a non fixed number of bits >= to the size of the type
leads to weird results, handle the special case of << 32 to fix it.

This was causing a test failure from test-socket-util:
Assertion 'in_addr_prefix_intersect(f, &ua, apl, &ub, bpl) == result' failed at
/var/tmp/paludis/build/sys-apps-systemd-scm/work/systemd-scm/src/test/test-socket-util.c:147, function
test_in_addr_prefix_intersect_one(). Aborting.

Minimal reproducer:

paludisbuild at Lou /tmp $ cat test.c
static void test(unsigned m) {
        unsigned nm = 0xFFFFFFFFUL << (32-m);
        printf("%u: %x\n", m, nm);
}

int main (void) {
        test(1);
        test(0);
        return 0;
}
paludisbuild at Lou /tmp $ gcc -m32 -std=gnu99 test.c -o test32
paludisbuild at Lou /tmp $ ./test32
1: 80000000
0: ffffffff
paludisbuild at Lou /tmp $ gcc -std=gnu99 test.c -o test64
paludisbuild at Lou /tmp $ ./test64
1: 80000000
0: 0
---
 src/shared/socket-util.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c
index f8c6795..6f49798 100644
--- a/src/shared/socket-util.c
+++ b/src/shared/socket-util.c
@@ -695,7 +695,7 @@ int in_addr_prefix_intersect(
                 uint32_t x, nm;
 
                 x = be32toh(a->in.s_addr ^ b->in.s_addr);
-                nm = 0xFFFFFFFFUL << (32 - m);
+                nm = (m == 0) ? 0 : 0xFFFFFFFFUL << (32 - m);
 
                 return (x & nm) == 0;
         }
-- 
2.0.0



More information about the systemd-devel mailing list