Skip to content

Commit 46b6f3f

Browse files
committed
Allow DOS-style line endings in ~/.pgpass files.
On Windows, libc will mask \r\n line endings for us, since we read the password file in text mode. But that doesn't happen on Unix. People who share password files across both systems might have \r\n line endings in a file they use on Unix, so as a convenience, ignore trailing \r. Per gripe from Josh Berkus. In passing, put the existing check for empty line somewhere where it's actually useful, ie after stripping the newline not before. Vik Fearing, adjusted a bit by me Discussion: <0de37763-5843-b2cc-855e-5d0e5df25807@agliodbs.com>
1 parent 2cd311d commit 46b6f3f

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5753,18 +5753,26 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
57535753
break;
57545754

57555755
len = strlen(buf);
5756-
if (len == 0)
5757-
continue;
57585756

57595757
/* Remove trailing newline */
5760-
if (buf[len - 1] == '\n')
5761-
buf[len - 1] = 0;
5758+
if (len > 0 && buf[len - 1] == '\n')
5759+
{
5760+
buf[--len] = '\0';
5761+
/* Handle DOS-style line endings, too, even when not on Windows */
5762+
if (len > 0 && buf[len - 1] == '\r')
5763+
buf[--len] = '\0';
5764+
}
5765+
5766+
if (len == 0)
5767+
continue;
57625768

57635769
if ((t = pwdfMatchesString(t, hostname)) == NULL ||
57645770
(t = pwdfMatchesString(t, port)) == NULL ||
57655771
(t = pwdfMatchesString(t, dbname)) == NULL ||
57665772
(t = pwdfMatchesString(t, username)) == NULL)
57675773
continue;
5774+
5775+
/* Found a match. */
57685776
ret = strdup(t);
57695777
fclose(fp);
57705778

0 commit comments

Comments
 (0)