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

Commit0b707d6

Browse files
committed
Be more careful about newline-chomping in pgbench.
process_backslash_command would drop the last character of the inputcommand on the assumption that it was a newline. Given a non newlineterminated input file, this could result in dropping the last characterof the command. Fix that by doing an actual test that we're removinga newline.While at it, allow for Windows newlines (\r\n), and suppress multiplenewlines if any. I do not think either of those cases really occur,since (a) we read script files in text mode and (b) the lexer stopswhen it hits a newline. But it's cheap enough and it provides astronger guarantee about what the result string looks like.This is just cosmetic, I think, since the possibly-overly-chompedline was only used for display not for further processing. Soit doesn't seem necessary to back-patch.Fabien Coelho, reviewed by Nikolay Shaplov, whacked around a bit by meDiscussion:https://postgr.es/m/alpine.DEB.2.20.1704171422500.4025@lancre
1 parentc23bb6b commit0b707d6

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

‎src/bin/pgbench/exprscan.l

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ expr_yyerror_more(yyscan_t yyscanner, const char *message, const char *more)
197197
interror_detection_offset =expr_scanner_offset(state) -1;
198198
YYSTYPElval;
199199
char *full_line;
200-
size_tl;
201200

202201
/*
203202
* While parsing an expression, we may not have collected the whole line
@@ -210,13 +209,11 @@ expr_yyerror_more(yyscan_t yyscanner, const char *message, const char *more)
210209
/* skip */ ;
211210
}
212211

212+
/* Extract the line, trimming trailing newline if any */
213213
full_line =expr_scanner_get_substring(state,
214214
expr_start_offset,
215-
expr_scanner_offset(state));
216-
/* Trim trailing newline if any */
217-
l =strlen(full_line);
218-
while (l >0 && full_line[l -1] =='\n')
219-
full_line[--l] ='\0';
215+
expr_scanner_offset(state),
216+
true);
220217

221218
syntax_error(expr_source, expr_lineno, full_line, expr_command,
222219
message, more, error_detection_offset - expr_start_offset);
@@ -341,19 +338,30 @@ expr_scanner_offset(PsqlScanState state)
341338

342339
/*
343340
* Get a malloc'd copy of the lexer input string from start_offset
344-
* to just before end_offset.
341+
* to just before end_offset. If chomp is true, drop any trailing
342+
* newline(s).
345343
*/
346344
char *
347345
expr_scanner_get_substring(PsqlScanState state,
348-
int start_offset,int end_offset)
346+
int start_offset,int end_offset,
347+
bool chomp)
349348
{
350349
char *result;
350+
constchar *scanptr = state->scanbuf + start_offset;
351351
intslen = end_offset - start_offset;
352352

353353
Assert(slen >=0);
354354
Assert(end_offset <=strlen(state->scanbuf));
355+
356+
if (chomp)
357+
{
358+
while (slen >0 &&
359+
(scanptr[slen -1] =='\n' || scanptr[slen -1] =='\r'))
360+
slen--;
361+
}
362+
355363
result = (char *)pg_malloc(slen +1);
356-
memcpy(result,state->scanbuf + start_offset, slen);
364+
memcpy(result,scanptr, slen);
357365
result[slen] ='\0';
358366

359367
return result;

‎src/bin/pgbench/pgbench.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,8 +3065,7 @@ process_backslash_command(PsqlScanState sstate, const char *source)
30653065
PQExpBufferDataword_buf;
30663066
intword_offset;
30673067
intoffsets[MAX_ARGS];/* offsets of argument words */
3068-
intstart_offset,
3069-
end_offset;
3068+
intstart_offset;
30703069
intlineno;
30713070
intj;
30723071

@@ -3120,13 +3119,11 @@ process_backslash_command(PsqlScanState sstate, const char *source)
31203119

31213120
my_command->expr=expr_parse_result;
31223121

3123-
/* Get location of the ending newline */
3124-
end_offset=expr_scanner_offset(sstate)-1;
3125-
3126-
/* Save line */
3122+
/* Save line, trimming any trailing newline */
31273123
my_command->line=expr_scanner_get_substring(sstate,
31283124
start_offset,
3129-
end_offset);
3125+
expr_scanner_offset(sstate),
3126+
true);
31303127

31313128
expr_scanner_finish(yyscanner);
31323129

@@ -3147,13 +3144,11 @@ process_backslash_command(PsqlScanState sstate, const char *source)
31473144
my_command->argc++;
31483145
}
31493146

3150-
/* Get location of the ending newline */
3151-
end_offset=expr_scanner_offset(sstate)-1;
3152-
3153-
/* Save line */
3147+
/* Save line, trimming any trailing newline */
31543148
my_command->line=expr_scanner_get_substring(sstate,
31553149
start_offset,
3156-
end_offset);
3150+
expr_scanner_offset(sstate),
3151+
true);
31573152

31583153
if (pg_strcasecmp(my_command->argv[0],"sleep")==0)
31593154
{

‎src/bin/pgbench/pgbench.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ extern yyscan_t expr_scanner_init(PsqlScanState state,
128128
externvoidexpr_scanner_finish(yyscan_tyyscanner);
129129
externintexpr_scanner_offset(PsqlScanStatestate);
130130
externchar*expr_scanner_get_substring(PsqlScanStatestate,
131-
intstart_offset,intend_offset);
131+
intstart_offset,intend_offset,
132+
boolchomp);
132133
externintexpr_scanner_get_lineno(PsqlScanStatestate,intoffset);
133134

134135
externvoidsyntax_error(constchar*source,intlineno,constchar*line,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp