[netcf-devel] [PATCH] Fix netmask for prefix == 32

Laine Stump laine at laine.org
Wed Aug 20 03:17:06 UTC 2014


This resolves:

  https://bugzilla.redhat.com/show_bug.cgi?id=1116314

If an interface was defines with a network prefix of 32 (could be used
for a point-to-point link), instead of translating into a netmask of
255.255.255.255, it would end up being 0.0.0.0. This behavior was
caused by the function ipcalc_netmask() assuming that
~(0xffffffffu >> 32) would be 0xffffffffu, when in fact the result of
shifting right a 32 bit number by 32 positions is undefined. As it
happens, in the case of the code generated by gccc, the result of the
above is 0, when it should in fact be 0xffffffffu.

The safe way to deal with this is to simply special-case prefix == 32.
---
 src/xslt_ext.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/xslt_ext.c b/src/xslt_ext.c
index f35f19b..bf1ec9c 100644
--- a/src/xslt_ext.c
+++ b/src/xslt_ext.c
@@ -1,7 +1,7 @@
 /*
  * xslt_ext.c: XSLT extension functions needed by the stylesheets
  *
- * Copyright (C) 2009 Red Hat Inc.
+ * Copyright (C) 2009, 2014 Red Hat Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -43,6 +43,8 @@
 static void ipcalc_netmask(xmlXPathParserContextPtr ctxt, int nargs) {
     double raw_prefix;
     unsigned long prefix = 0;
+    struct in_addr netmask;
+    xmlChar netmask_str[16];
 
     if (nargs != 1) {
         xmlXPathSetArityError(ctxt);
@@ -69,10 +71,13 @@ static void ipcalc_netmask(xmlXPathParserContextPtr ctxt, int nargs) {
         goto error;
     }
 
-    struct in_addr netmask;
-    xmlChar netmask_str[16];
-
-    netmask.s_addr = htonl(~(0xffffffffu >> prefix));
+    /* We need to special-case 32, because the result of "x >> 32"
+     * is undefined when the number of bits in x's type is == 32.
+     */
+    if (prefix == 32)
+        netmask.s_addr = 0xffffffffu;
+    else
+        netmask.s_addr = htonl(~(0xffffffffu >> prefix));
 
     if (! inet_ntop(AF_INET, &netmask,
                     (char *) netmask_str, sizeof(netmask_str))) {
-- 
1.9.3



More information about the netcf-devel mailing list