BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: gorcom2012(at)gmail(dot)com
Subject: BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false
Date: 2025-05-07 08:48:44
Message-ID: 18914-6eac5aa27f44c628@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 18914
Logged by: Eugeny Goryachev
Email address: gorcom2012(at)gmail(dot)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++)

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message David Rowley 2025-05-07 10:34:16 Re: BUG #18914: REDUNDANT_COMPARISON.ALWAYS_FALSE Redundant comparison '0' > 'nb' (0 > {4, 16}) is always false
Previous Message M. BROUSSE 2025-05-07 07:00:43 Re: BUG #18911: Bug in the command INHERITS.