pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19039: UNREACHABLE_CODE: Remove unreachable code in network_send - replace with assertion
2+ messages / 2 participants
[nested] [flat]

* BUG #19039: UNREACHABLE_CODE: Remove unreachable code in network_send - replace with assertion
@ 2025-09-02 07:06  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 2+ messages in thread

From: PG Bug reporting form @ 2025-09-02 07:06 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: gorcom2012@gmail.com

The following bug has been logged on the website:

Bug reference:      19039
Logged by:          Eugeny Goryachev
Email address:      gorcom2012@gmail.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++)



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #19039: UNREACHABLE_CODE: Remove unreachable code in network_send - replace with assertion
@ 2025-09-02 11:54  Daniel Gustafsson <daniel@yesql.se>
  parent: PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 0 replies; 2+ messages in thread

From: Daniel Gustafsson @ 2025-09-02 11:54 UTC (permalink / raw)
  To: gorcom2012@gmail.com; pgsql-bugs@lists.postgresql.org

> On 2 Sep 2025, at 09:06, PG Bug reporting form <noreply@postgresql.org> wrote:

> if (nb < 0)
>    nb = 0;
> 
> is unreachable because the ip_addrsize(addr) function can only return one of
> two possible values:

Thanks for the report, this has already been fixed in f27eb0325b7b.

--
Daniel Gustafsson






^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-09-02 11:54 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-09-02 07:06 BUG #19039: UNREACHABLE_CODE: Remove unreachable code in network_send - replace with assertion PG Bug reporting form <noreply@postgresql.org>
2025-09-02 11:54 ` 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