From: Michael Paquier Date: Thu, 4 Jun 2026 06:12:11 +0000 (+0900) Subject: libpq: Fix "servicefile" after fallback to system service file X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=90354030b8fceecef9a1908fdefcccf0e02823fb;p=thirdparty%2Fpostgresql.git libpq: Fix "servicefile" after fallback to system service file When "servicefile" is specified in a connection string but the requested service is not found in this file, libpq falls back to a default file, pg_service.conf in PGSYSCONFDIR. The "servicefile" option was missing an update in this case, causing an incorrect value to be reported, impacting SERVICEFILE in psql, for example. An extra update of the option is added in this commit, based on the actual file where the service name has been found. A test is added to cover this problem. Oversight in 092f3c63efc6. Author: Chao Li Discussion: https://postgr.es/m/59DD1652-4EB5-449B-8DE3-959F06561A96@gmail.com --- diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 4272d386e64..38422becc48 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -6050,6 +6050,18 @@ next_file: if (status != 0) return status; + /* Update servicefile to the file that actually supplied the service */ + if (group_found && service_fname != NULL && + conninfo_storeval(options, "servicefile", serviceFile, + errorMessage, false, false) == NULL) + { + /* + * conninfo_storeval already set an error message, that could be only + * an OOM. + */ + return 3; + } + last_file: if (!group_found) { diff --git a/src/interfaces/libpq/t/006_service.pl b/src/interfaces/libpq/t/006_service.pl index 7462d21314d..8e29880731f 100644 --- a/src/interfaces/libpq/t/006_service.pl +++ b/src/interfaces/libpq/t/006_service.pl @@ -143,6 +143,14 @@ local $ENV{PGSERVICEFILE} = "$srvfile_empty"; sql => "SELECT 'connect2_3'", expected_stdout => qr/connect2_3/); + my $srvfile_empty_win_cared = $srvfile_empty; + $srvfile_empty_win_cared =~ s/\\/\\\\/g; + $dummy_node->connect_ok( + q{service=my_srv servicefile='} . $srvfile_empty_win_cared . q{'}, + 'SERVICEFILE updated when service is found in default pg_service.conf', + sql => '\echo :SERVICEFILE', + expected_stdout => qr/^\Q$srvfile_default\E$/); + local $ENV{PGSERVICE} = 'undefined-service'; $dummy_node->connect_fails( '',