Skip to content

Commit 5dfcbdd

Browse files
committed
Fix some portability bugs I'd introduced into inet/cidr code ---
shifting by the word width is not defined by ANSI C...
1 parent bbea364 commit 5dfcbdd

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/backend/utils/adt/network.c

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* is for IP V4 CIDR notation, but prepared for V6: just
44
* add the necessary bits where the comments indicate.
55
*
6-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.26 2000/11/10 20:13:25 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.27 2000/11/25 21:30:54 tgl Exp $
77
*
88
* Jon Postel RIP 16 Oct 1998
99
*/
@@ -418,7 +418,13 @@ network_broadcast(PG_FUNCTION_ARGS)
418418
/* It's an IP V4 address: */
419419
unsigned long mask = 0xffffffff;
420420

421-
mask >>= ip_bits(ip);
421+
/* Shifting by 32 or more bits does not yield portable results,
422+
* so don't try it.
423+
*/
424+
if (ip_bits(ip) < 32)
425+
mask >>= ip_bits(ip);
426+
else
427+
mask = 0;
422428

423429
ip_v4addr(dst) = htonl(ntohl(ip_v4addr(ip)) | mask);
424430
}
@@ -451,7 +457,13 @@ network_network(PG_FUNCTION_ARGS)
451457
/* It's an IP V4 address: */
452458
unsigned long mask = 0xffffffff;
453459

454-
mask <<= (32 - ip_bits(ip));
460+
/* Shifting by 32 or more bits does not yield portable results,
461+
* so don't try it.
462+
*/
463+
if (ip_bits(ip) > 0)
464+
mask <<= (32 - ip_bits(ip));
465+
else
466+
mask = 0;
455467

456468
ip_v4addr(dst) = htonl(ntohl(ip_v4addr(ip)) & mask);
457469
}
@@ -484,7 +496,13 @@ network_netmask(PG_FUNCTION_ARGS)
484496
/* It's an IP V4 address: */
485497
unsigned long mask = 0xffffffff;
486498

487-
mask <<= (32 - ip_bits(ip));
499+
/* Shifting by 32 or more bits does not yield portable results,
500+
* so don't try it.
501+
*/
502+
if (ip_bits(ip) > 0)
503+
mask <<= (32 - ip_bits(ip));
504+
else
505+
mask = 0;
488506

489507
ip_v4addr(dst) = htonl(mask);
490508

@@ -512,7 +530,13 @@ v4bitncmp(unsigned long a1, unsigned long a2, int bits)
512530
{
513531
unsigned long mask;
514532

515-
mask = (0xFFFFFFFFL << (32 - bits)) & 0xFFFFFFFFL;
533+
/* Shifting by 32 or more bits does not yield portable results,
534+
* so don't try it.
535+
*/
536+
if (bits > 0)
537+
mask = (0xFFFFFFFFL << (32 - bits)) & 0xFFFFFFFFL;
538+
else
539+
mask = 0;
516540
a1 = ntohl(a1);
517541
a2 = ntohl(a2);
518542
if ((a1 & mask) < (a2 & mask))
@@ -530,7 +554,13 @@ v4addressOK(unsigned long a1, int bits)
530554
{
531555
unsigned long mask;
532556

533-
mask = (0xFFFFFFFFL << (32 - bits)) & 0xFFFFFFFFL;
557+
/* Shifting by 32 or more bits does not yield portable results,
558+
* so don't try it.
559+
*/
560+
if (bits > 0)
561+
mask = (0xFFFFFFFFL << (32 - bits)) & 0xFFFFFFFFL;
562+
else
563+
mask = 0;
534564
a1 = ntohl(a1);
535565
if ((a1 & mask) == a1)
536566
return true;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy