aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2000-11-25 21:30:54 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2000-11-25 21:30:54 +0000
commit5dfcbdde702c85019bcc2c4e384e082d87d42c16 (patch)
tree576a2c79c0579744d93203e5f0132ebd9b2e3438
parentbbea3643a3a6425f92d0db9ff16c7f73a31a466c (diff)
downloadpostgresql-5dfcbdde702c85019bcc2c4e384e082d87d42c16.tar.gz
postgresql-5dfcbdde702c85019bcc2c4e384e082d87d42c16.zip
Fix some portability bugs I'd introduced into inet/cidr code ---
shifting by the word width is not defined by ANSI C...
-rw-r--r--src/backend/utils/adt/network.c42
1 files changed, 36 insertions, 6 deletions
diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c
index 52f4e8ecd2f..75527fda026 100644
--- a/src/backend/utils/adt/network.c
+++ b/src/backend/utils/adt/network.c
@@ -3,7 +3,7 @@
* is for IP V4 CIDR notation, but prepared for V6: just
* add the necessary bits where the comments indicate.
*
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.26 2000/11/10 20:13:25 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.27 2000/11/25 21:30:54 tgl Exp $
*
* Jon Postel RIP 16 Oct 1998
*/
@@ -418,7 +418,13 @@ network_broadcast(PG_FUNCTION_ARGS)
/* It's an IP V4 address: */
unsigned long mask = 0xffffffff;
- mask >>= ip_bits(ip);
+ /* Shifting by 32 or more bits does not yield portable results,
+ * so don't try it.
+ */
+ if (ip_bits(ip) < 32)
+ mask >>= ip_bits(ip);
+ else
+ mask = 0;
ip_v4addr(dst) = htonl(ntohl(ip_v4addr(ip)) | mask);
}
@@ -451,7 +457,13 @@ network_network(PG_FUNCTION_ARGS)
/* It's an IP V4 address: */
unsigned long mask = 0xffffffff;
- mask <<= (32 - ip_bits(ip));
+ /* Shifting by 32 or more bits does not yield portable results,
+ * so don't try it.
+ */
+ if (ip_bits(ip) > 0)
+ mask <<= (32 - ip_bits(ip));
+ else
+ mask = 0;
ip_v4addr(dst) = htonl(ntohl(ip_v4addr(ip)) & mask);
}
@@ -484,7 +496,13 @@ network_netmask(PG_FUNCTION_ARGS)
/* It's an IP V4 address: */
unsigned long mask = 0xffffffff;
- mask <<= (32 - ip_bits(ip));
+ /* Shifting by 32 or more bits does not yield portable results,
+ * so don't try it.
+ */
+ if (ip_bits(ip) > 0)
+ mask <<= (32 - ip_bits(ip));
+ else
+ mask = 0;
ip_v4addr(dst) = htonl(mask);
@@ -512,7 +530,13 @@ v4bitncmp(unsigned long a1, unsigned long a2, int bits)
{
unsigned long mask;
- mask = (0xFFFFFFFFL << (32 - bits)) & 0xFFFFFFFFL;
+ /* Shifting by 32 or more bits does not yield portable results,
+ * so don't try it.
+ */
+ if (bits > 0)
+ mask = (0xFFFFFFFFL << (32 - bits)) & 0xFFFFFFFFL;
+ else
+ mask = 0;
a1 = ntohl(a1);
a2 = ntohl(a2);
if ((a1 & mask) < (a2 & mask))
@@ -530,7 +554,13 @@ v4addressOK(unsigned long a1, int bits)
{
unsigned long mask;
- mask = (0xFFFFFFFFL << (32 - bits)) & 0xFFFFFFFFL;
+ /* Shifting by 32 or more bits does not yield portable results,
+ * so don't try it.
+ */
+ if (bits > 0)
+ mask = (0xFFFFFFFFL << (32 - bits)) & 0xFFFFFFFFL;
+ else
+ mask = 0;
a1 = ntohl(a1);
if ((a1 & mask) == a1)
return true;