BUG #19039: UNREACHABLE_CODE: Remove unreachable code in network_send - replace with assertion

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 #19039: UNREACHABLE_CODE: Remove unreachable code in network_send - replace with assertion
Date: 2025-09-02 07:06:18
Message-ID: 19039-9fe9888501b7a12f@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: 19039
Logged by: Eugeny Goryachev
Email address: gorcom2012(at)gmail(dot)com
PostgreSQL version: 17.4
Operating system: Ubuntu
Description:

I found unreachable code in the network_send function in
src/backend/utils/adt/network.c.
The current check:

if (nb < 0)
nb = 0;

is unreachable because the ip_addrsize(addr) function can only return one of
two possible values:

- 4 for IPv4 addresses (AF_INET);
- 16 for IPv6 addresses (AF_INET6).

These are the only address families supported by PostgreSQL's inet datatype,
and the network input functions validate all addresses before they reach
this code path.

Replace the unreachable defensive code with an assertion that documents this
invariant:

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 zhouenbing 2025-09-02 09:25:31 empty,query_id, pg_stat_activity
Previous Message Richard Guo 2025-09-02 06:49:51 Re: BUG #19037: Planner fails on estimating array length with "no relation entry" error