Skip to content

Commit dde1a35

Browse files
committed
libpq_pipeline: Must strdup(optarg) to avoid crash
I forgot to strdup() when processing argv[]. Apparently many platforms hide this mistake from users, but in those that don't you may get a program crash. Repair. Per buildfarm member drongo, which is the only one in all the buildfarm manifesting a problem here. While at it, move "numrows" processing out of the line of special cases, and make it getopt's -r instead. (A similar thing could be done to 'conninfo', but current use of the program doesn't warrant spending time on that -- nowhere else we use conninfo in so simplistic a manner.) Discussion: https://postgr.es/m/20210401124850.GA19247@alvherre.pgsql
1 parent f82de5c commit dde1a35

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/test/modules/libpq_pipeline/libpq_pipeline.c

+16-16
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ test_pipelined_insert(PGconn *conn, int n_rows)
725725
{
726726
snprintf(insert_param_0, MAXINTLEN, "%d", rows_to_send);
727727
snprintf(insert_param_1, MAXINT8LEN, "%lld",
728-
(long long) rows_to_send);
728+
(1L << 62) + (long long) rows_to_send);
729729

730730
if (PQsendQueryPrepared(conn, "my_insert",
731731
2, insert_params, NULL, NULL, 0) == 1)
@@ -1227,9 +1227,10 @@ usage(const char *progname)
12271227
fprintf(stderr, "%s tests libpq's pipeline mode.\n\n", progname);
12281228
fprintf(stderr, "Usage:\n");
12291229
fprintf(stderr, " %s [OPTION] tests\n", progname);
1230-
fprintf(stderr, " %s [OPTION] TESTNAME [CONNINFO [NUMBER_OF_ROWS]\n", progname);
1230+
fprintf(stderr, " %s [OPTION] TESTNAME [CONNINFO]\n", progname);
12311231
fprintf(stderr, "\nOptions:\n");
12321232
fprintf(stderr, " -t TRACEFILE generate a libpq trace to TRACEFILE\n");
1233+
fprintf(stderr, " -r NUMROWS use NUMROWS as the test size\n");
12331234
}
12341235

12351236
static void
@@ -1256,19 +1257,29 @@ main(int argc, char **argv)
12561257
PGresult *res;
12571258
int c;
12581259

1259-
while ((c = getopt(argc, argv, "t:")) != -1)
1260+
while ((c = getopt(argc, argv, "t:r:")) != -1)
12601261
{
12611262
switch (c)
12621263
{
12631264
case 't': /* trace file */
12641265
tracefile = pg_strdup(optarg);
12651266
break;
1267+
case 'r': /* numrows */
1268+
errno = 0;
1269+
numrows = strtol(optarg, NULL, 10);
1270+
if (errno != 0 || numrows <= 0)
1271+
{
1272+
fprintf(stderr, "couldn't parse \"%s\" as a positive integer\n",
1273+
optarg);
1274+
exit(1);
1275+
}
1276+
break;
12661277
}
12671278
}
12681279

12691280
if (optind < argc)
12701281
{
1271-
testname = argv[optind];
1282+
testname = pg_strdup(argv[optind]);
12721283
optind++;
12731284
}
12741285
else
@@ -1285,18 +1296,7 @@ main(int argc, char **argv)
12851296

12861297
if (optind < argc)
12871298
{
1288-
conninfo = argv[optind];
1289-
optind++;
1290-
}
1291-
if (optind < argc)
1292-
{
1293-
errno = 0;
1294-
numrows = strtol(argv[optind], NULL, 10);
1295-
if (errno != 0 || numrows <= 0)
1296-
{
1297-
fprintf(stderr, "couldn't parse \"%s\" as a positive integer\n", argv[optind]);
1298-
exit(1);
1299-
}
1299+
conninfo = pg_strdup(argv[optind]);
13001300
optind++;
13011301
}
13021302

src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
for my $testname (@tests)
2424
{
25-
my @extraargs = ();
25+
my @extraargs = ('-r', $numrows);
2626
my $cmptrace = grep(/^$testname$/,
2727
qw(simple_pipeline multi_pipelines prepared singlerow
2828
pipeline_abort transaction disallowed_in_pipeline)) > 0;
@@ -38,8 +38,7 @@
3838
$node->command_ok(
3939
[
4040
'libpq_pipeline', @extraargs,
41-
$testname, $node->connstr('postgres'),
42-
$numrows
41+
$testname, $node->connstr('postgres')
4342
],
4443
"libpq_pipeline $testname");
4544

0 commit comments

Comments
 (0)