Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1e5a5d0

Browse files
committed
Simplify some long-obsolete code in hba.c's next_token().
next_token() oddly set its buffer space consumption limit to one beforethe last char position in the buffer, not the last as you'd expect.The reason is there was once an ugly kluge to mark keywords by appendinga newline to them, potentially requiring one more byte. Commite5e2fc8removed that kluge, but failed to notice that the length limit could beincreased.Also, remove some vestigial handling of newline characters in the buffer.That was left over from when this function read the file directly usinggetc(). Commit7f49a67 changed it to read from a buffer, from whichtokenize_file had already removed the only possible occurrence of newline,but did not simplify this function in consequence.Also, ensure that we don't return with *lineptr set to someplace past theterminating '\0'; that would be catastrophic if a caller were to ask foranother token from the same line. This is just latent since no callersactually do call again after a "false" return; but considering that it wasactually costing us extra code to do it wrong, we might as well make itbulletproof.Noted while reviewing pg_hba_file_rules patch.
1 parentde16ab7 commit1e5a5d0

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

‎src/backend/libpq/hba.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -197,42 +197,32 @@ next_token(char **lineptr, char *buf, int bufsz,
197197
{
198198
intc;
199199
char*start_buf=buf;
200-
char*end_buf=buf+ (bufsz-2);
200+
char*end_buf=buf+ (bufsz-1);
201201
boolin_quote= false;
202202
boolwas_quote= false;
203203
boolsaw_quote= false;
204204

205-
/* end_buf reserves two bytes to ensure we can append \n and \0 */
206205
Assert(end_buf>start_buf);
207206

208207
*initial_quote= false;
209208
*terminating_comma= false;
210209

211-
/* Move overinitial whitespace and commas */
210+
/* Move overany whitespace and commas preceding the next token */
212211
while ((c= (*(*lineptr)++))!='\0'&& (pg_isblank(c)||c==','))
213212
;
214213

215-
if (c=='\0'||c=='\n')
216-
{
217-
*buf='\0';
218-
return false;
219-
}
220-
221214
/*
222-
* Build a token in buf of next characters up toEOF,EOL, unquoted comma,
223-
*orunquoted whitespace.
215+
* Build a token in buf of next characters up to EOL, unquoted comma, or
216+
* unquoted whitespace.
224217
*/
225-
while (c!='\0'&&c!='\n'&&
218+
while (c!='\0'&&
226219
(!pg_isblank(c)||in_quote))
227220
{
228221
/* skip comments to EOL */
229222
if (c=='#'&& !in_quote)
230223
{
231-
while ((c= (*(*lineptr)++))!='\0'&&c!='\n')
224+
while ((c= (*(*lineptr)++))!='\0')
232225
;
233-
/* If only comment, consume EOL too; return EOL */
234-
if (c!='\0'&&buf==start_buf)
235-
(*lineptr)++;
236226
break;
237227
}
238228

@@ -245,12 +235,14 @@ next_token(char **lineptr, char *buf, int bufsz,
245235
start_buf)));
246236
*err_msg="authentication file token too long";
247237
/* Discard remainder of line */
248-
while ((c= (*(*lineptr)++))!='\0'&&c!='\n')
238+
while ((c= (*(*lineptr)++))!='\0')
249239
;
240+
/* Un-eat the '\0', in case we're called again */
241+
(*lineptr)--;
250242
return false;
251243
}
252244

253-
/* we do not pass backthe comma in the token */
245+
/* we do not pass backa terminating comma in the token */
254246
if (c==','&& !in_quote)
255247
{
256248
*terminating_comma= true;
@@ -278,8 +270,8 @@ next_token(char **lineptr, char *buf, int bufsz,
278270
}
279271

280272
/*
281-
*Put backthe char right after the token (critical in case it isEOL,
282-
*since we need to detectend-of-line at next call).
273+
*Un-eatthe char right after the token (critical in case it is'\0',
274+
*else next call will read pastend of string).
283275
*/
284276
(*lineptr)--;
285277

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp