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

Commit 61a07ba

Browse files
committed
Remove pgbench's hardwired limit on line length in custom script files.
pgbench formerly failed on lines longer than BUFSIZ, unexpectedly splitting them into multiple commands. Allow it to work with any length of input line. Sawada Masahiko
1 parent f1f21b2 commit 61a07ba

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

contrib/pgbench/pgbench.c

+48-2
Original file line numberDiff line numberDiff line change
@@ -2016,6 +2016,49 @@ process_commands(char *buf)
20162016
return my_commands;
20172017
}
20182018

2019+
/*
2020+
* Read a line from fd, and return it in a malloc'd buffer.
2021+
* Return NULL at EOF.
2022+
*
2023+
* The buffer will typically be larger than necessary, but we don't care
2024+
* in this program, because we'll free it as soon as we've parsed the line.
2025+
*/
2026+
static char *
2027+
read_line_from_file(FILE *fd)
2028+
{
2029+
char tmpbuf[BUFSIZ];
2030+
char *buf;
2031+
size_t buflen = BUFSIZ;
2032+
size_t used = 0;
2033+
2034+
buf = (char *) palloc(buflen);
2035+
buf[0] = '\0';
2036+
2037+
while (fgets(tmpbuf, BUFSIZ, fd) != NULL)
2038+
{
2039+
size_t thislen = strlen(tmpbuf);
2040+
2041+
/* Append tmpbuf to whatever we had already */
2042+
memcpy(buf + used, tmpbuf, thislen + 1);
2043+
used += thislen;
2044+
2045+
/* Done if we collected a newline */
2046+
if (thislen > 0 && tmpbuf[thislen - 1] == '\n')
2047+
break;
2048+
2049+
/* Else, enlarge buf to ensure we can append next bufferload */
2050+
buflen += BUFSIZ;
2051+
buf = (char *) pg_realloc(buf, buflen);
2052+
}
2053+
2054+
if (used > 0)
2055+
return buf;
2056+
2057+
/* Reached EOF */
2058+
free(buf);
2059+
return NULL;
2060+
}
2061+
20192062
static int
20202063
process_file(char *filename)
20212064
{
@@ -2024,7 +2067,7 @@ process_file(char *filename)
20242067
Command **my_commands;
20252068
FILE *fd;
20262069
int lineno;
2027-
char buf[BUFSIZ];
2070+
char *buf;
20282071
int alloc_num;
20292072

20302073
if (num_files >= MAX_FILES)
@@ -2046,11 +2089,14 @@ process_file(char *filename)
20462089

20472090
lineno = 0;
20482091

2049-
while (fgets(buf, sizeof(buf), fd) != NULL)
2092+
while ((buf = read_line_from_file(fd)) != NULL)
20502093
{
20512094
Command *command;
20522095

20532096
command = process_commands(buf);
2097+
2098+
free(buf);
2099+
20542100
if (command == NULL)
20552101
continue;
20562102

0 commit comments

Comments
 (0)