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

Commit0d1ca2a

Browse files
committed
Fix unportable code in SockAddr_cidr_mask: you can't assume that
shifting left by full word width gives zero. Per bug report fromTyson Thomson.
1 parentc22b4fa commit0d1ca2a

File tree

1 file changed

+22
-19
lines changed
  • src/backend/libpq

1 file changed

+22
-19
lines changed

‎src/backend/libpq/ip.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/libpq/ip.c,v 1.29 2004/09/27 23:24:30 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/libpq/ip.c,v 1.30 2004/11/08 01:54:40 tgl Exp $
1212
*
1313
* This file and the IPV6 implementation were initially provided by
1414
* Nigel Kukard <nkukard@lbsd.net>, Linux Based Systems Design
@@ -340,37 +340,40 @@ SockAddr_cidr_mask(struct sockaddr_storage * mask, char *numbits, int family)
340340
{
341341
longbits;
342342
char*endptr;
343-
structsockaddr_inmask4;
344-
345-
#ifdefHAVE_IPV6
346-
structsockaddr_in6mask6;
347-
#endif
348343

349344
bits=strtol(numbits,&endptr,10);
350345

351346
if (*numbits=='\0'||*endptr!='\0')
352347
return-1;
353348

354-
if ((bits<0)|| (family==AF_INET&&bits>32)
355-
#ifdefHAVE_IPV6
356-
|| (family==AF_INET6&&bits>128)
357-
#endif
358-
)
359-
return-1;
360-
361349
switch (family)
362350
{
363351
caseAF_INET:
364-
mask4.sin_addr.s_addr=
365-
htonl((0xffffffffUL << (32-bits))
366-
&0xffffffffUL);
367-
memcpy(mask,&mask4,sizeof(mask4));
368-
break;
352+
{
353+
structsockaddr_inmask4;
354+
longmaskl;
355+
356+
if (bits<0||bits>32)
357+
return-1;
358+
/* avoid "x << 32", which is not portable */
359+
if (bits>0)
360+
maskl= (0xffffffffUL << (32- (int)bits))
361+
&0xffffffffUL;
362+
else
363+
maskl=0;
364+
mask4.sin_addr.s_addr=htonl(maskl);
365+
memcpy(mask,&mask4,sizeof(mask4));
366+
break;
367+
}
368+
369369
#ifdefHAVE_IPV6
370370
caseAF_INET6:
371371
{
372+
structsockaddr_in6mask6;
372373
inti;
373374

375+
if (bits<0||bits>128)
376+
return-1;
374377
for (i=0;i<16;i++)
375378
{
376379
if (bits <=0)
@@ -380,7 +383,7 @@ SockAddr_cidr_mask(struct sockaddr_storage * mask, char *numbits, int family)
380383
else
381384
{
382385
mask6.sin6_addr.s6_addr[i]=
383-
(0xff << (8-bits))&0xff;
386+
(0xff << (8-(int)bits))&0xff;
384387
}
385388
bits-=8;
386389
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp