diff mbox

[OpenWrt-Devel] firewall3: fix left shift on 64 bit systems in fw3_bitlen2netmask

Message ID 20150105145834.GA9004@gir
State Accepted
Delegated to: Jo-Philipp Wich
Headers show

Commit Message

Ulrich Weber Jan. 5, 2015, 2:58 p.m. UTC
otherwise 0.0.0.0/0 is set as 0.0.0.0/255.255.255.255 on x86_64

Signed-off-by: Ulrich Weber <uw@ocedo.com>
---
 utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Yousong Zhou Jan. 6, 2015, 12:54 p.m. UTC | #1
On 5 January 2015 at 22:58, Ulrich Weber <uweber.linux@gmail.com> wrote:
> otherwise 0.0.0.0/0 is set as 0.0.0.0/255.255.255.255 on x86_64
>
> Signed-off-by: Ulrich Weber <uw@ocedo.com>
> ---
>  utils.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/utils.c b/utils.c
> index 71a0f13..b2fbe02 100644
> --- a/utils.c
> +++ b/utils.c
> @@ -803,7 +803,7 @@ fw3_bitlen2netmask(int family, int bits, void *mask)
>                         return false;
>
>                 v4 = mask;
> -               v4->s_addr = htonl(~((1 << (32 - abs(bits))) - 1));
> +               v4->s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0;
>

Quite a catch!  But the culprit should be that shifting a 32-bit
integer left/right by not less than 32 is supposed to be undefined
behaviour in C.

>                 if (bits < 0)
>                         v4->s_addr = ~v4->s_addr;
> --
> 1.9.1
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel@lists.openwrt.org
> https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
diff mbox

Patch

diff --git a/utils.c b/utils.c
index 71a0f13..b2fbe02 100644
--- a/utils.c
+++ b/utils.c
@@ -803,7 +803,7 @@  fw3_bitlen2netmask(int family, int bits, void *mask)
 			return false;
 
 		v4 = mask;
-		v4->s_addr = htonl(~((1 << (32 - abs(bits))) - 1));
+		v4->s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0;
 
 		if (bits < 0)
 			v4->s_addr = ~v4->s_addr;