Skip to content

Commit 2ac5988

Browse files
committed
Fix misparsing of non-newline-terminated pg_hba.conf files.
This back-patches the v10-cycle commit 1e5a5d0 into 9.3 - 9.6. I had noticed at the time that that was fixing a bug, namely that next_token() might advance *lineptr past the line-terminating '\0', but given the lack of field complaints I too easily convinced myself that the problem was only latent. It's not, because tokenize_file() decides whether there's more on the line using "strlen(lineptr)". The bug is indeed latent on a newline-terminated line, because then the newline-stripping bit in tokenize_file() means we'll have two or more consecutive '\0's in the buffer, masking the fact that we accidentally advanced over the first one. But the last line in the file might not be null-terminated, allowing the loop to see and process garbage, as reported by Mark Jones in bug #14859. The bug doesn't exist in <= 9.2; there next_token() is reading directly from a file, and termination of the outer loop relies on an feof() test not a buffer pointer check. Probably commit 7f49a67 can be blamed for this bug, but I didn't track it down exactly. Commit 1e5a5d0 does a bit more than the minimum needed to fix the bug, but I felt the rest of it was good cleanup, so applying it all. Discussion: https://postgr.es/m/20171017141814.8203.27280@wrigleys.postgresql.org
1 parent aa1e9b3 commit 2ac5988

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

src/backend/libpq/hba.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,42 +141,32 @@ next_token(char **lineptr, char *buf, int bufsz, bool *initial_quote,
141141
{
142142
int c;
143143
char *start_buf = buf;
144-
char *end_buf = buf + (bufsz - 2);
144+
char *end_buf = buf + (bufsz - 1);
145145
bool in_quote = false;
146146
bool was_quote = false;
147147
bool saw_quote = false;
148148

149-
/* end_buf reserves two bytes to ensure we can append \n and \0 */
150149
Assert(end_buf > start_buf);
151150

152151
*initial_quote = false;
153152
*terminating_comma = false;
154153

155-
/* Move over initial whitespace and commas */
154+
/* Move over any whitespace and commas preceding the next token */
156155
while ((c = (*(*lineptr)++)) != '\0' && (pg_isblank(c) || c == ','))
157156
;
158157

159-
if (c == '\0' || c == '\n')
160-
{
161-
*buf = '\0';
162-
return false;
163-
}
164-
165158
/*
166-
* Build a token in buf of next characters up to EOF, EOL, unquoted comma,
167-
* or unquoted whitespace.
159+
* Build a token in buf of next characters up to EOL, unquoted comma, or
160+
* unquoted whitespace.
168161
*/
169-
while (c != '\0' && c != '\n' &&
162+
while (c != '\0' &&
170163
(!pg_isblank(c) || in_quote))
171164
{
172165
/* skip comments to EOL */
173166
if (c == '#' && !in_quote)
174167
{
175-
while ((c = (*(*lineptr)++)) != '\0' && c != '\n')
168+
while ((c = (*(*lineptr)++)) != '\0')
176169
;
177-
/* If only comment, consume EOL too; return EOL */
178-
if (c != '\0' && buf == start_buf)
179-
(*lineptr)++;
180170
break;
181171
}
182172

@@ -188,12 +178,12 @@ next_token(char **lineptr, char *buf, int bufsz, bool *initial_quote,
188178
errmsg("authentication file token too long, skipping: \"%s\"",
189179
start_buf)));
190180
/* Discard remainder of line */
191-
while ((c = (*(*lineptr)++)) != '\0' && c != '\n')
181+
while ((c = (*(*lineptr)++)) != '\0')
192182
;
193183
break;
194184
}
195185

196-
/* we do not pass back the comma in the token */
186+
/* we do not pass back a terminating comma in the token */
197187
if (c == ',' && !in_quote)
198188
{
199189
*terminating_comma = true;
@@ -221,8 +211,8 @@ next_token(char **lineptr, char *buf, int bufsz, bool *initial_quote,
221211
}
222212

223213
/*
224-
* Put back the char right after the token (critical in case it is EOL,
225-
* since we need to detect end-of-line at next call).
214+
* Un-eat the char right after the token (critical in case it is '\0',
215+
* else next call will read past end of string).
226216
*/
227217
(*lineptr)--;
228218

0 commit comments

Comments
 (0)