[Olsr-dev] Strange shift behaviour (ipcalc.c)

Markus Kittenberger (spam-protected)
Sun Nov 30 03:32:24 CET 2008


On Sat, Nov 29, 2008 at 10:14 PM, Markus Kittenberger <
(spam-protected)> wrote:

> i ended in ipcalc.c
>
> 121 if(olsr_cnf->ip_version == AF_INET) {
> 122  uint32_t netmask = ntohl(~0 << (32 - net->prefix_len));
> 123  rv = (ipaddr->v4.s_addr & netmask) == (net->prefix.v4.s_addr &
> netmask);
> 124 }
>
as you can`t shift a 32 bit wide type by 32 (which is of no big usage anyway
(-;)
.
e.g. if you code like following you end in an compiler warning
.
netmask = netmask << 32;
.
warning: left shift count >= width of type
.
therefore a patch like following is really required for ipcalc.c
.
--- ../../olsrd-54b2ca51a0b1/src/ipcalc.c 2008-11-28 10:00:21.000000000
+0100
+++ ipcalc.c 2008-11-30 03:30:09.000000000 +0100
@@ -119,8 +119,12 @@
 {
  int rv;
  if(olsr_cnf->ip_version == AF_INET) {
- uint32_t netmask = ntohl(~0 << (32 - net->prefix_len));
- rv = (ipaddr->v4.s_addr & netmask) == (net->prefix.v4.s_addr & netmask);
+ if (net->prefix_len < 1) {
+ rv = true;
+ } else {
+ uint32_t netmask = ~0 << (32 - net->prefix_len);
+ rv = (ipaddr->v4.s_addr & netmask) == (net->prefix.v4.s_addr & netmask);
+ }
  } else {
  /* IPv6 */
  uint32_t netmask;
.
.
Markus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.olsr.org/pipermail/olsr-dev/attachments/20081130/43590f4e/attachment.html>


More information about the Olsr-dev mailing list