Skip to content

Commit a7a9273

Browse files
committed
Work around spurious compiler warning in inet operators
gcc 12+ has complaints like the following: ../../../../../pgsql/src/backend/utils/adt/network.c: In function 'inetnot': ../../../../../pgsql/src/backend/utils/adt/network.c:1893:34: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=] 1893 | pdst[nb] = ~pip[nb]; | ~~~~~~~~~^~~~~~~~~~ ../../../../../pgsql/src/include/utils/inet.h:27:23: note: at offset -1 into destination object 'ipaddr' of size 16 27 | unsigned char ipaddr[16]; /* up to 128 bits of address */ | ^~~~~~ ../../../../../pgsql/src/include/utils/inet.h:27:23: note: at offset -1 into destination object 'ipaddr' of size 16 This is due to a compiler bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104986 It has been a year since the bug has been reported without getting fixed. As the warnings are verbose and use of gcc 12 is becoming more common, it seems worth working around the bug. Particularly because a simple reformulation of the loop condition fixes the issue and isn't any less readable. Author: Tom Lane <tgl@sss.pgh.pa.us> Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/144536.1648326206@sss.pgh.pa.us Backpatch: 11-
1 parent 00fc4b3 commit a7a9273

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/backend/utils/adt/network.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,7 @@ inetnot(PG_FUNCTION_ARGS)
18891889
unsigned char *pip = ip_addr(ip);
18901890
unsigned char *pdst = ip_addr(dst);
18911891

1892-
while (nb-- > 0)
1892+
while (--nb >= 0)
18931893
pdst[nb] = ~pip[nb];
18941894
}
18951895
ip_bits(dst) = ip_bits(ip);
@@ -1921,7 +1921,7 @@ inetand(PG_FUNCTION_ARGS)
19211921
unsigned char *pip2 = ip_addr(ip2);
19221922
unsigned char *pdst = ip_addr(dst);
19231923

1924-
while (nb-- > 0)
1924+
while (--nb >= 0)
19251925
pdst[nb] = pip[nb] & pip2[nb];
19261926
}
19271927
ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
@@ -1953,7 +1953,7 @@ inetor(PG_FUNCTION_ARGS)
19531953
unsigned char *pip2 = ip_addr(ip2);
19541954
unsigned char *pdst = ip_addr(dst);
19551955

1956-
while (nb-- > 0)
1956+
while (--nb >= 0)
19571957
pdst[nb] = pip[nb] | pip2[nb];
19581958
}
19591959
ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
@@ -1978,7 +1978,7 @@ internal_inetpl(inet *ip, int64 addend)
19781978
unsigned char *pdst = ip_addr(dst);
19791979
int carry = 0;
19801980

1981-
while (nb-- > 0)
1981+
while (--nb >= 0)
19821982
{
19831983
carry = pip[nb] + (int) (addend & 0xFF) + carry;
19841984
pdst[nb] = (unsigned char) (carry & 0xFF);
@@ -2062,7 +2062,7 @@ inetmi(PG_FUNCTION_ARGS)
20622062
unsigned char *pip2 = ip_addr(ip2);
20632063
int carry = 1;
20642064

2065-
while (nb-- > 0)
2065+
while (--nb >= 0)
20662066
{
20672067
int lobyte;
20682068

0 commit comments

Comments
 (0)