Skip to content

Commit 0dc4019

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 6a9229d commit 0dc4019

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/backend/utils/adt/network.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,7 @@ inetnot(PG_FUNCTION_ARGS)
18661866
unsigned char *pip = ip_addr(ip);
18671867
unsigned char *pdst = ip_addr(dst);
18681868

1869-
while (nb-- > 0)
1869+
while (--nb >= 0)
18701870
pdst[nb] = ~pip[nb];
18711871
}
18721872
ip_bits(dst) = ip_bits(ip);
@@ -1898,7 +1898,7 @@ inetand(PG_FUNCTION_ARGS)
18981898
unsigned char *pip2 = ip_addr(ip2);
18991899
unsigned char *pdst = ip_addr(dst);
19001900

1901-
while (nb-- > 0)
1901+
while (--nb >= 0)
19021902
pdst[nb] = pip[nb] & pip2[nb];
19031903
}
19041904
ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
@@ -1930,7 +1930,7 @@ inetor(PG_FUNCTION_ARGS)
19301930
unsigned char *pip2 = ip_addr(ip2);
19311931
unsigned char *pdst = ip_addr(dst);
19321932

1933-
while (nb-- > 0)
1933+
while (--nb >= 0)
19341934
pdst[nb] = pip[nb] | pip2[nb];
19351935
}
19361936
ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
@@ -1955,7 +1955,7 @@ internal_inetpl(inet *ip, int64 addend)
19551955
unsigned char *pdst = ip_addr(dst);
19561956
int carry = 0;
19571957

1958-
while (nb-- > 0)
1958+
while (--nb >= 0)
19591959
{
19601960
carry = pip[nb] + (int) (addend & 0xFF) + carry;
19611961
pdst[nb] = (unsigned char) (carry & 0xFF);
@@ -2039,7 +2039,7 @@ inetmi(PG_FUNCTION_ARGS)
20392039
unsigned char *pip2 = ip_addr(ip2);
20402040
int carry = 1;
20412041

2042-
while (nb-- > 0)
2042+
while (--nb >= 0)
20432043
{
20442044
int lobyte;
20452045

0 commit comments

Comments
 (0)