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

Commit 2eb3bc5

Browse files
committed
Fix issues around .pgpass file.
This commit fixes the following two issues around .pgpass file. (1) If the length of a line in .pgpass file was larger than 319B, libpq silently treated each 319B in the line as a separate setting line. (2) The document explains that a line beginning with # is treated as a comment in .pgpass. But there was no code doing such special handling. Whether a line begins with # or not, libpq just checked that the first token in the line match with the host. For (1), this commit makes libpq warn if the length of a line is larger than 319B, and throw away the remaining part beginning from 320B position. For (2), this commit changes libpq so that it treats any lines beginning with # as comments. Author: Fujii Masao Reviewed-by: Hamid Akhtar Discussion: https://postgr.es/m/c0f0c01c-fa74-9749-2084-b73882fd5465@oss.nttdata.com
1 parent fbcf087 commit 2eb3bc5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/interfaces/libpq/fe-connect.c

+33
Original file line numberDiff line numberDiff line change
@@ -6949,6 +6949,7 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
69496949
{
69506950
FILE *fp;
69516951
struct stat stat_buf;
6952+
int line_number = 0;
69526953

69536954
#define LINELEN NAMEDATALEN*5
69546955
char buf[LINELEN];
@@ -7014,10 +7015,42 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
70147015
*p1,
70157016
*p2;
70167017
int len;
7018+
int buflen;
70177019

70187020
if (fgets(buf, sizeof(buf), fp) == NULL)
70197021
break;
70207022

7023+
line_number++;
7024+
buflen = strlen(buf);
7025+
if (buflen >= sizeof(buf) - 1 && buf[buflen - 1] != '\n')
7026+
{
7027+
char rest[LINELEN];
7028+
int restlen;
7029+
7030+
/*
7031+
* Warn if this password setting line is too long, because it's
7032+
* unexpectedly truncated.
7033+
*/
7034+
if (buf[0] != '#')
7035+
fprintf(stderr,
7036+
libpq_gettext("WARNING: line %d too long in password file \"%s\"\n"),
7037+
line_number, pgpassfile);
7038+
7039+
/* eat rest of the line */
7040+
while (!feof(fp) && !ferror(fp))
7041+
{
7042+
if (fgets(rest, sizeof(rest), fp) == NULL)
7043+
break;
7044+
restlen = strlen(rest);
7045+
if (restlen < sizeof(rest) - 1 || rest[restlen - 1] == '\n')
7046+
break;
7047+
}
7048+
}
7049+
7050+
/* ignore comments */
7051+
if (buf[0] == '#')
7052+
continue;
7053+
70217054
/* strip trailing newline and carriage return */
70227055
len = pg_strip_crlf(buf);
70237056

0 commit comments

Comments
 (0)