Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit52d39d5

Browse files
committed
Sorting for the inet data type randomly returns the wrong result
when you have networks with the same prefix, but different netmasks.This is due to the fact that occassionally there is random(uninitialized?)data in the extra bits past the point where the netmask cares aboutthem.ie (real data from a real live database): 10.0/10 == 00001010.00100000.00100000.00011000 10.0/11 == 00001010.00000000.00000000.00000000 ^ Bad data, normally never seenThe v4bitncmp() function was only taking one bit length argument soit would determine that the networks were different, even thoughthey really aren't (and the netmask test wouldn't be used). ThisONLY happens if the tuple with the longer bit length is used as theip_bits() for the v4bitncmp call AND there happens to be junk datain place in the shorter tuple. Odd and random, but I saw it happena couple times so...Ryan Mooney
1 parent5a19781 commit52d39d5

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

‎src/backend/utils/adt/network.c

Lines changed: 14 additions & 11 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-
*$Id: network.c,v 1.18 2000/02/21 18:49:54 tgl Exp $
6+
*$Id: network.c,v 1.19 2000/03/07 23:01:43 momjian Exp $
77
*Jon Postel RIP 16 Oct 1998
88
*/
99

@@ -18,7 +18,7 @@
1818
#include"postgres.h"
1919
#include"utils/builtins.h"
2020

21-
staticintv4bitncmp(unsignedinta1,unsignedinta2,intbits);
21+
staticintv4bitncmp(unsignedinta1,unsignedinta2,intbits1,intbits2);
2222

2323
/*
2424
*Access macros.Add IPV6 support.
@@ -137,7 +137,7 @@ network_lt(inet *a1, inet *a2)
137137
return FALSE;
138138
if ((ip_family(a1)==AF_INET)&& (ip_family(a2)==AF_INET))
139139
{
140-
intorder=v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a2));
140+
intorder=v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a1),ip_bits(a2));
141141

142142
return ((order<0)|| ((order==0)&& (ip_bits(a1)<ip_bits(a2))));
143143
}
@@ -166,7 +166,7 @@ network_eq(inet *a1, inet *a2)
166166
if ((ip_family(a1)==AF_INET)&& (ip_family(a2)==AF_INET))
167167
{
168168
return ((ip_bits(a1)==ip_bits(a2))
169-
&& (v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a1))==0));
169+
&& (v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a1),ip_bits(a2))==0));
170170
}
171171
else
172172
{
@@ -192,7 +192,7 @@ network_gt(inet *a1, inet *a2)
192192
return FALSE;
193193
if ((ip_family(a1)==AF_INET)&& (ip_family(a2)==AF_INET))
194194
{
195-
intorder=v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a2));
195+
intorder=v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a1),ip_bits(a2));
196196

197197
return ((order>0)|| ((order==0)&& (ip_bits(a1)>ip_bits(a2))));
198198
}
@@ -222,7 +222,7 @@ network_sub(inet *a1, inet *a2)
222222
if ((ip_family(a1)==AF_INET)&& (ip_family(a2)==AF_INET))
223223
{
224224
return ((ip_bits(a1)>ip_bits(a2))
225-
&& (v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a2))==0));
225+
&& (v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a1),ip_bits(a2))==0));
226226
}
227227
else
228228
{
@@ -242,7 +242,7 @@ network_subeq(inet *a1, inet *a2)
242242
if ((ip_family(a1)==AF_INET)&& (ip_family(a2)==AF_INET))
243243
{
244244
return ((ip_bits(a1) >=ip_bits(a2))
245-
&& (v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a2))==0));
245+
&& (v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a1),ip_bits(a2))==0));
246246
}
247247
else
248248
{
@@ -262,7 +262,7 @@ network_sup(inet *a1, inet *a2)
262262
if ((ip_family(a1)==AF_INET)&& (ip_family(a2)==AF_INET))
263263
{
264264
return ((ip_bits(a1)<ip_bits(a2))
265-
&& (v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a1))==0));
265+
&& (v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a1),ip_bits(a2))==0));
266266
}
267267
else
268268
{
@@ -282,7 +282,7 @@ network_supeq(inet *a1, inet *a2)
282282
if ((ip_family(a1)==AF_INET)&& (ip_family(a2)==AF_INET))
283283
{
284284
return ((ip_bits(a1) <=ip_bits(a2))
285-
&& (v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a1))==0));
285+
&& (v4bitncmp(ip_v4addr(a1),ip_v4addr(a2),ip_bits(a1),ip_bits(a2))==0));
286286
}
287287
else
288288
{
@@ -476,13 +476,16 @@ network_netmask(inet *ip)
476476
*/
477477

478478
staticint
479-
v4bitncmp(unsignedinta1,unsignedinta2,intbits)
479+
v4bitncmp(unsignedinta1,unsignedinta2,intbits1,intbits2)
480480
{
481481
unsigned longmask=0;
482-
inti;
482+
inti,bits;
483+
484+
bits=(bits1<bits2) ?bits1 :bits2;
483485

484486
for (i=0;i<bits;i++)
485487
mask= (mask >>1) |0x80000000;
488+
486489
a1=ntohl(a1);
487490
a2=ntohl(a2);
488491
if ((a1&mask)< (a2&mask))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp