]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
libpq: Fix "servicefile" after fallback to system service file
authorMichael Paquier <michael@paquier.xyz>
Thu, 4 Jun 2026 06:12:11 +0000 (15:12 +0900)
committerMichael Paquier <michael@paquier.xyz>
Thu, 4 Jun 2026 06:12:11 +0000 (15:12 +0900)
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 <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/59DD1652-4EB5-449B-8DE3-959F06561A96@gmail.com

src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/t/006_service.pl

index 4272d386e64a62c322f337cd0a062b6242589549..38422becc4802248df04bdde81e11bbdd4f24af7 100644 (file)
@@ -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)
        {
index 7462d21314db20f9664c384a1b42a2289977fff8..8e29880731f0f34fffe7b8cce2199e4058fce5f2 100644 (file)
@@ -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(
                '',