pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false
3+ messages / 3 participants
[nested] [flat]
* BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false
@ 2025-05-07 08:48 PG Bug reporting form <noreply@postgresql.org>
2025-05-07 10:34 ` Re: BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false David Rowley <dgrowleyml@gmail.com>
2025-05-07 10:35 ` Re: BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false Daniel Gustafsson <daniel@yesql.se>
0 siblings, 2 replies; 3+ messages in thread
From: PG Bug reporting form @ 2025-05-07 08:48 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: gorcom2012@gmail.com
The following bug has been logged on the website:
Bug reference: 18914
Logged by: Eugeny Goryachev
Email address: gorcom2012@gmail.com
PostgreSQL version: 17.4
Operating system: Ubuntu
Description:
REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4,
16}) is always false at network.c:282.
The function 'network_send()' in src/backend/utils/adt/network.c contains a
redundant comparison:
if (nb < 0)
nb = 0;
Since 'nb' is set via 'ip_addrsize(addr)', which returns either 4 or 16 (for
IPv4 and IPv6 addresses respectively), this condition can never be true. As
such, the check adds no runtime value and may trigger warnings from static
analyzers about unreachable code.
To address this, I've replaced the redundant check with an 'Assert(nb >= 0)'
to document the expected invariant while maintaining defensive programming
style.
This change retains safety by ensuring that unexpected negative values would
be caught during development (in debug builds), while eliminating the
unreachable code that confuses static analysis tools.
Patch:
diff --git a/src/backend/utils/adt/network.c
b/src/backend/utils/adt/network.c
index 640fc37dc83..3f83fbe85df 100644
--- a/src/backend/utils/adt/network.c
+++ b/src/backend/utils/adt/network.c
@@ -279,8 +279,7 @@ network_send(inet *addr, bool is_cidr)
pq_sendbyte(&buf, ip_bits(addr));
pq_sendbyte(&buf, is_cidr);
nb = ip_addrsize(addr);
- if (nb < 0)
- nb = 0;
+ Assert(nb >= 0);
pq_sendbyte(&buf, nb);
addrptr = (char *) ip_addr(addr);
for (i = 0; i < nb; i++)
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false
2025-05-07 08:48 BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false PG Bug reporting form <noreply@postgresql.org>
@ 2025-05-07 10:34 ` David Rowley <dgrowleyml@gmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: David Rowley @ 2025-05-07 10:34 UTC (permalink / raw)
To: gorcom2012@gmail.com; pgsql-bugs@lists.postgresql.org
On Wed, 7 May 2025 at 21:28, PG Bug reporting form
<noreply@postgresql.org> wrote:
> @@ -279,8 +279,7 @@ network_send(inet *addr, bool is_cidr)
> pq_sendbyte(&buf, ip_bits(addr));
> pq_sendbyte(&buf, is_cidr);
> nb = ip_addrsize(addr);
> - if (nb < 0)
> - nb = 0;
> + Assert(nb >= 0);
This has already been fixed in master by [1]. Since there's no actual
misbehaviour, there was no backpatch.
David
[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=f27eb0325b7b2cff3b880fa66991369384952...
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false
2025-05-07 08:48 BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false PG Bug reporting form <noreply@postgresql.org>
@ 2025-05-07 10:35 ` Daniel Gustafsson <daniel@yesql.se>
1 sibling, 0 replies; 3+ messages in thread
From: Daniel Gustafsson @ 2025-05-07 10:35 UTC (permalink / raw)
To: gorcom2012@gmail.com; pgsql-bugs@lists.postgresql.org
> On 7 May 2025, at 10:48, PG Bug reporting form <noreply@postgresql.org> wrote:
>
> The following bug has been logged on the website:
>
> Bug reference: 18914
> Logged by: Eugeny Goryachev
> Email address: gorcom2012@gmail.com
> PostgreSQL version: 17.4
> Operating system: Ubuntu
> Description:
>
> REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4,
> 16}) is always false at network.c:282.
> The function 'network_send()' in src/backend/utils/adt/network.c contains a
> redundant comparison:
> if (nb < 0)
> nb = 0;
> Since 'nb' is set via 'ip_addrsize(addr)', which returns either 4 or 16 (for
> IPv4 and IPv6 addresses respectively), this condition can never be true.
This was already addressed in commit f27eb0325b7b2c.
If you are going to run a static analyzer you should make sure to run it
against HEAD as small things like this one aren't backported.
--
Daniel Gustafsson
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2025-05-07 10:35 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-05-07 08:48 BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false PG Bug reporting form <noreply@postgresql.org>
2025-05-07 10:34 ` David Rowley <dgrowleyml@gmail.com>
2025-05-07 10:35 ` Daniel Gustafsson <daniel@yesql.se>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox