Commit d9e230bf4c8798afd204d99c2535d4e2672f70fb

Authored by m-holger
Committed by GitHub
2 parents 1d8dc489 985cdf91

Merge pull request #1342 from m-holger/i1329

CLI reject flags with parameters (fixes #1329)
ChangeLog
  1 +2024-02-01 M Holger <m.holger@qpdf.org>
  2 +
  3 + * Bug fix: in qpdf CLI / QPDFJob throw a QPDFUsage exception if a
  4 + parameter is given for a flag. Previously the parameter was ignored and
  5 + e.g. --encrypt=n was treated as --encrypt. Fixes #1329.
  6 +
1 2024-09-20 Chao Li <mslichao@outlook.com> 7 2024-09-20 Chao Li <mslichao@outlook.com>
2 8
3 * Add C API function qpdf_oh_free_buffer to release memory allocated 9 * Add C API function qpdf_oh_free_buffer to release memory allocated
libqpdf/QPDFArgParser.cc
@@ -9,6 +9,8 @@ @@ -9,6 +9,8 @@
9 #include <cstring> 9 #include <cstring>
10 #include <iostream> 10 #include <iostream>
11 11
  12 +using namespace std::literals;
  13 +
12 QPDFArgParser::Members::Members(int argc, char const* const argv[], char const* progname_env) : 14 QPDFArgParser::Members::Members(int argc, char const* const argv[], char const* progname_env) :
13 15
14 argc(argc), 16 argc(argc),
@@ -532,7 +534,13 @@ QPDFArgParser::parseArgs() @@ -532,7 +534,13 @@ QPDFArgParser::parseArgs()
532 } 534 }
533 usage(message); 535 usage(message);
534 } 536 }
  537 +
535 if (oe.bare_arg_handler) { 538 if (oe.bare_arg_handler) {
  539 + if (have_parameter) {
  540 + usage(
  541 + "--"s + arg_s + " does not take a parameter, but \"" + parameter +
  542 + "\" was given");
  543 + }
536 oe.bare_arg_handler(); 544 oe.bare_arg_handler();
537 } else if (oe.param_arg_handler) { 545 } else if (oe.param_arg_handler) {
538 oe.param_arg_handler(parameter); 546 oe.param_arg_handler(parameter);
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 = 25; 18 +my $n_tests = 26;
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"},
@@ -32,6 +32,11 @@ $td-&gt;runtest(&quot;required argument with choices&quot;, @@ -32,6 +32,11 @@ $td-&gt;runtest(&quot;required argument with choices&quot;,
32 {$td->REGEXP => "must be given as --decode-level=\\{.*all.*\\}", 32 {$td->REGEXP => "must be given as --decode-level=\\{.*all.*\\}",
33 $td->EXIT_STATUS => 2}, 33 $td->EXIT_STATUS => 2},
34 $td->NORMALIZE_NEWLINES); 34 $td->NORMALIZE_NEWLINES);
  35 +$td->runtest("flag with parameter",
  36 + {$td->COMMAND => "qpdf --qdf=false minimal.pdf"},
  37 + {$td->REGEXP => "--qdf does not take a parameter, but \"false\" was given",
  38 + $td->EXIT_STATUS => 2},
  39 + $td->NORMALIZE_NEWLINES);
35 copy("minimal.pdf", '@file.pdf'); 40 copy("minimal.pdf", '@file.pdf');
36 $td->runtest("\@file exists and file doesn't", 41 $td->runtest("\@file exists and file doesn't",
37 {$td->COMMAND => "qpdf --check \@file.pdf"}, 42 {$td->COMMAND => "qpdf --check \@file.pdf"},