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

Commit f65eced

Browse files
committed
pg_waldump: Fix invalid option handling
Previously, running pg_waldump with an invalid option (pg_waldump --foo) would print the help output and exit successfully. This was because it tried to process the option letter '?' as a normal option, but that letter is used by getopt() to report an invalid option. To fix, process help and version options separately, like we do everywhere else. Also add a basic test suite for pg_waldump and run the basic option handling tests, which would have caught this.
1 parent cd96389 commit f65eced

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

src/bin/pg_waldump/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
# Source files copied from src/backend/access/rmgrdesc/
33
/*desc.c
44
/xlogreader.c
5+
6+
# Generated by test suite
7+
/tmp_check/

src/bin/pg_waldump/Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@ uninstall:
3838

3939
clean distclean maintainer-clean:
4040
rm -f pg_waldump$(X) $(OBJS) $(RMGRDESCSOURCES) xlogreader.c
41+
rm -rf tmp_check
42+
43+
check:
44+
$(prove_check)
45+
46+
installcheck:
47+
$(prove_installcheck)

src/bin/pg_waldump/pg_waldump.c

+16-9
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ usage(void)
806806
printf(_(" -z, --stats[=record] show statistics instead of records\n"
807807
" (optionally, show per-record statistics)\n"));
808808
printf(_(" -?, --help show this help, then exit\n"));
809+
printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
809810
}
810811

811812
int
@@ -844,6 +845,20 @@ main(int argc, char **argv)
844845
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_waldump"));
845846
progname = get_progname(argv[0]);
846847

848+
if (argc > 1)
849+
{
850+
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
851+
{
852+
usage();
853+
exit(0);
854+
}
855+
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
856+
{
857+
puts("pg_waldump (PostgreSQL) " PG_VERSION);
858+
exit(0);
859+
}
860+
}
861+
847862
memset(&private, 0, sizeof(XLogDumpPrivate));
848863
memset(&config, 0, sizeof(XLogDumpConfig));
849864
memset(&stats, 0, sizeof(XLogDumpStats));
@@ -869,7 +884,7 @@ main(int argc, char **argv)
869884
goto bad_argument;
870885
}
871886

872-
while ((option = getopt_long(argc, argv, "be:?fn:p:r:s:t:Vx:z",
887+
while ((option = getopt_long(argc, argv, "be:fn:p:r:s:t:x:z",
873888
long_options, &optindex)) != -1)
874889
{
875890
switch (option)
@@ -889,10 +904,6 @@ main(int argc, char **argv)
889904
case 'f':
890905
config.follow = true;
891906
break;
892-
case '?':
893-
usage();
894-
exit(EXIT_SUCCESS);
895-
break;
896907
case 'n':
897908
if (sscanf(optarg, "%d", &config.stop_after_records) != 1)
898909
{
@@ -947,10 +958,6 @@ main(int argc, char **argv)
947958
goto bad_argument;
948959
}
949960
break;
950-
case 'V':
951-
puts("pg_waldump (PostgreSQL) " PG_VERSION);
952-
exit(EXIT_SUCCESS);
953-
break;
954961
case 'x':
955962
if (sscanf(optarg, "%u", &config.filter_by_xid) != 1)
956963
{

src/bin/pg_waldump/t/001_basic.pl

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use strict;
2+
use warnings;
3+
use TestLib;
4+
use Test::More tests => 8;
5+
6+
program_help_ok('pg_waldump');
7+
program_version_ok('pg_waldump');
8+
program_options_handling_ok('pg_waldump');

0 commit comments

Comments
 (0)