Commit 31396f61c98ff955118f192f75bf02320c7a2abc

Authored by Jay Berkenbilt
1 parent fa15042c

Disallow --empty with --replace-input (fixes #728)

libqpdf/QPDFJob.cc
@@ -683,7 +683,13 @@ QPDFJob::getExitCode() const @@ -683,7 +683,13 @@ QPDFJob::getExitCode() const
683 void 683 void
684 QPDFJob::checkConfiguration() 684 QPDFJob::checkConfiguration()
685 { 685 {
  686 + // Do final checks for command-line consistency. (I always think
  687 + // this is called doFinalChecks, so I'm putting that in a
  688 + // comment.)
  689 +
686 if (m->replace_input) { 690 if (m->replace_input) {
  691 + // Check for --empty appears later after we have checked
  692 + // m->infilename.
687 if (m->outfilename) { 693 if (m->outfilename) {
688 usage("--replace-input may not be used when" 694 usage("--replace-input may not be used when"
689 " an output file is specified"); 695 " an output file is specified");
@@ -700,6 +706,8 @@ QPDFJob::checkConfiguration() @@ -700,6 +706,8 @@ QPDFJob::checkConfiguration()
700 } 706 }
701 if (m->infilename == nullptr) { 707 if (m->infilename == nullptr) {
702 usage("an input file name is required"); 708 usage("an input file name is required");
  709 + } else if (m->replace_input && (strlen(m->infilename.get()) == 0)) {
  710 + usage("--replace-input may not be used with --empty");
703 } else if ( 711 } else if (
704 m->require_outfile && (m->outfilename == nullptr) && 712 m->require_outfile && (m->outfilename == nullptr) &&
705 (!m->replace_input)) { 713 (!m->replace_input)) {
qpdf/qtest/arg-parsing.test
@@ -15,7 +15,7 @@ cleanup(); @@ -15,7 +15,7 @@ cleanup();
15 15
16 my $td = new TestDriver('arg-parsing'); 16 my $td = new TestDriver('arg-parsing');
17 17
18 -my $n_tests = 16; 18 +my $n_tests = 17;
19 19
20 $td->runtest("required argument", 20 $td->runtest("required argument",
21 {$td->COMMAND => "qpdf --password minimal.pdf"}, 21 {$td->COMMAND => "qpdf --password minimal.pdf"},
@@ -102,6 +102,11 @@ $td->runtest("ignore -- at top level", @@ -102,6 +102,11 @@ $td->runtest("ignore -- at top level",
102 {$td->REGEXP => ".*No syntax or stream encoding errors found.*", 102 {$td->REGEXP => ".*No syntax or stream encoding errors found.*",
103 $td->EXIT_STATUS => 0}, 103 $td->EXIT_STATUS => 0},
104 $td->NORMALIZE_NEWLINES); 104 $td->NORMALIZE_NEWLINES);
  105 +$td->runtest("empty and replace-input",
  106 + {$td->COMMAND => "qpdf --empty --replace-input"},
  107 + {$td->REGEXP => ".*--replace-input may not be used with --empty.*",
  108 + $td->EXIT_STATUS => 2},
  109 + $td->NORMALIZE_NEWLINES);
105 110
106 cleanup(); 111 cleanup();
107 $td->report($n_tests); 112 $td->report($n_tests);