Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a69e6d9

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 8aa3e47 commit a69e6d9

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/interfaces/libpq/fe-connect.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -5783,18 +5783,26 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
57835783
break;
57845784

57855785
len = strlen(buf);
5786-
if (len == 0)
5787-
continue;
57885786

57895787
/* Remove trailing newline */
5790-
if (buf[len - 1] == '\n')
5791-
buf[len - 1] = 0;
5788+
if (len > 0 && buf[len - 1] == '\n')
5789+
{
5790+
buf[--len] = '\0';
5791+
/* Handle DOS-style line endings, too, even when not on Windows */
5792+
if (len > 0 && buf[len - 1] == '\r')
5793+
buf[--len] = '\0';
5794+
}
5795+
5796+
if (len == 0)
5797+
continue;
57925798

57935799
if ((t = pwdfMatchesString(t, hostname)) == NULL ||
57945800
(t = pwdfMatchesString(t, port)) == NULL ||
57955801
(t = pwdfMatchesString(t, dbname)) == NULL ||
57965802
(t = pwdfMatchesString(t, username)) == NULL)
57975803
continue;
5804+
5805+
/* Found a match. */
57985806
ret = strdup(t);
57995807
fclose(fp);
58005808

0 commit comments

Comments
 (0)