Skip to content

Commit 8c52b77

Browse files
committed
Fix failures to ignore \r when reading Windows-style newlines.
libpq failed to ignore Windows-style newlines in connection service files. This normally wasn't a problem on Windows itself, because fgets() would convert \r\n to just \n. But if libpq were running inside a program that changes the default fopen mode to binary, it would see the \r's and think they were data. In any case, it's project policy to ignore \r in text files unconditionally, because people sometimes try to use files with DOS-style newlines on Unix machines, where the C library won't hide that from us. Hence, adjust parseServiceFile() to ignore \r as well as \n at the end of the line. In HEAD, go a little further and make it ignore all trailing whitespace, to match what it's always done with leading whitespace. In HEAD, also run around and fix up everyplace where we have newline-chomping code to make all those places look consistent and uniformly drop \r. It is not clear whether any of those changes are fixing live bugs. Most of the non-cosmetic changes are in places that are reading popen output, and the jury is still out as to whether popen on Windows can return \r\n. (The Windows-specific code in pipe_read_line seems to think so, but our lack of support for this elsewhere suggests maybe it's not a problem in practice.) Hence, I desisted from applying those changes to back branches, except in run_ssl_passphrase_command() which is new enough and little-tested enough that we'd probably not have heard about any problems there. Tom Lane and Michael Paquier, per bug #15827 from Jorge Gustavo Rocha. Back-patch the parseServiceFile() change to all supported branches, and the run_ssl_passphrase_command() change to v11 where that was added. Discussion: https://postgr.es/m/15827-e6ba53a3a7ed543c@postgresql.org
1 parent 53fd0f0 commit 8c52b77

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,8 @@ parseServiceFile(const char *serviceFile,
40934093

40944094
while ((line = fgets(buf, sizeof(buf), f)) != NULL)
40954095
{
4096+
int len;
4097+
40964098
linenr++;
40974099

40984100
if (strlen(line) >= sizeof(buf) - 1)
@@ -4105,16 +4107,18 @@ parseServiceFile(const char *serviceFile,
41054107
return 2;
41064108
}
41074109

4108-
/* ignore EOL at end of line */
4109-
if (strlen(line) && line[strlen(line) - 1] == '\n')
4110-
line[strlen(line) - 1] = 0;
4110+
/* ignore EOL at end of line, including \r in case it's a DOS file */
4111+
len = strlen(line);
4112+
while (len > 0 && (line[len - 1] == '\n' ||
4113+
line[len - 1] == '\r'))
4114+
line[--len] = '\0';
41114115

41124116
/* ignore leading blanks */
41134117
while (*line && isspace((unsigned char) line[0]))
41144118
line++;
41154119

41164120
/* ignore comments and empty lines */
4117-
if (strlen(line) == 0 || line[0] == '#')
4121+
if (line[0] == '\0' || line[0] == '#')
41184122
continue;
41194123

41204124
/* Check for right groupname */

0 commit comments

Comments
 (0)