postgres.git / summary / log / commit / refs

commit    de2d7b602b43245fc7fccf08b8f04a2cf8e72fca
Author:   Fujii Masao <fujii@postgresql.org>
Date:     Sat Nov 21 14:16:26 2026 +0000

    Fix libpq crashes on malformed target_session_attrs query results
    
    Applications using libpq could crash during connection establishment
    when a server returned an unexpected result for an internal query used
    to check target_session_attrs.
    
    This happened because libpq checked that the result contained one row,
    but did not check the column count before reading the first column.
    A result with one row and no columns caused PQgetvalue() to return NULL,
    which was then passed to strncmp().
    
    Fix this by requiring exactly one column in the results of SHOW
    transaction_read_only and SELECT pg_catalog.pg_is_in_recovery().
    Malformed results now follow the existing error path, rejecting the
    connection attempt to that host and trying the next host, if any.
    
    Backpatch to all supported versions.
    
    Author: s1ko <s1ko@riseup.net>
    Reviewed-by: Chao Li <li.evan.chao@gmail.com>
    Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
    Discussion: https://postgr.es/m/f1743511-5fab-482e-94ec-1198ce18723e@riseup.net
    Backpatch-through: 15


src/interfaces/libpq/fe-connect.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index fce5aef25e9..032b4e47be2 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -3753,7 +3753,7 @@ keep_going: /* We will come back to here until there is res = PQgetResult(conn); if (res && PQresultStatus(res) == PGRES_TUPLES_OK && - PQntuples(res) == 1) + PQntuples(res) == 1 && PQnfields(res) == 1) { char *val = PQgetvalue(res, 0, 0); @@ -3820,7 +3820,7 @@ keep_going: /* We will come back to here until there is res = PQgetResult(conn); if (res && PQresultStatus(res) == PGRES_TUPLES_OK && - PQntuples(res) == 1) + PQntuples(res) == 1 && PQnfields(res) == 1) { char *val = PQgetvalue(res, 0, 0); [parent: e9d3d12c79c9]