[netcf-devel] [PATCH] Fix netmask for prefix == 32
Eric Blake
eblake at redhat.com
Wed Aug 20 03:26:56 UTC 2014
On 08/19/2014 09:17 PM, Laine Stump wrote:
> 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
s/gccc/gcc/ ?
> 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(-)
>
ACK
>
> - 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));
Another way of writing this might be:
netmask.s_addr = htonl(~0U << (32 - prefix))
(defined for prefix from 1 to 32, but not for prefix 0, whereas your
code is). But it's not worth the rewrite.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 539 bytes
Desc: OpenPGP digital signature
URL: <https://lists.fedorahosted.org/pipermail/netcf-devel/attachments/20140819/b8d7883a/attachment.sig>
More information about the netcf-devel
mailing list