Commit 039eb4a253c7faed9a906a7f45f2c9f1f2a76002

Authored by Jay Berkenbilt
1 parent be3a8c0e

Fix input file = output file test for split pages

ChangeLog
  1 +2021-02-23 Jay Berkenbilt <ejb@ql.org>
  2 +
  3 + * The test for the input and output files being the same wasn't
  4 + implemented correctly for --split-pages since the specified output
  5 + file is really a pattern, not the actual output file.
  6 +
1 2021-02-22 Jay Berkenbilt <ejb@ql.org> 7 2021-02-22 Jay Berkenbilt <ejb@ql.org>
2 8
3 * From qpdf CLI, --pages and --split-pages will properly preserve 9 * From qpdf CLI, --pages and --split-pages will properly preserve
qpdf/qpdf.cc
@@ -3738,7 +3738,7 @@ ArgParser::doFinalChecks() @@ -3738,7 +3738,7 @@ ArgParser::doFinalChecks()
3738 } 3738 }
3739 } 3739 }
3740 3740
3741 - if (QUtil::same_file(o.infilename, o.outfilename)) 3741 + if ((! o.split_pages) && QUtil::same_file(o.infilename, o.outfilename))
3742 { 3742 {
3743 QTC::TC("qpdf", "qpdf same file error"); 3743 QTC::TC("qpdf", "qpdf same file error");
3744 usage("input file and output file are the same;" 3744 usage("input file and output file are the same;"
@@ -6389,6 +6389,13 @@ static void do_split_pages(QPDF&amp; pdf, Options&amp; o, bool&amp; warnings) @@ -6389,6 +6389,13 @@ static void do_split_pages(QPDF&amp; pdf, Options&amp; o, bool&amp; warnings)
6389 QUtil::uint_to_string(last, QIntC::to_int(pageno_len)); 6389 QUtil::uint_to_string(last, QIntC::to_int(pageno_len));
6390 } 6390 }
6391 std::string outfile = before + page_range + after; 6391 std::string outfile = before + page_range + after;
  6392 + if (QUtil::same_file(o.infilename, outfile.c_str()))
  6393 + {
  6394 + std::cerr << whoami
  6395 + << ": split pages would overwrite input file with "
  6396 + << outfile << std::endl;
  6397 + exit(EXIT_ERROR);
  6398 + }
6392 QPDFWriter w(outpdf, outfile.c_str()); 6399 QPDFWriter w(outpdf, outfile.c_str());
6393 set_writer_options(outpdf, o, w); 6400 set_writer_options(outpdf, o, w);
6394 w.write(); 6401 w.write();
qpdf/qtest/qpdf.test
@@ -1825,14 +1825,24 @@ $td-&gt;runtest(&quot;out of range in deleted object&quot;, @@ -1825,14 +1825,24 @@ $td-&gt;runtest(&quot;out of range in deleted object&quot;,
1825 show_ntests(); 1825 show_ntests();
1826 # ---------- 1826 # ----------
1827 $td->notify("--- Overwrite self ---"); 1827 $td->notify("--- Overwrite self ---");
1828 -$n_tests += 1; 1828 +$n_tests += 3;
1829 1829
1830 copy("minimal.pdf", "a.pdf"); 1830 copy("minimal.pdf", "a.pdf");
  1831 +copy("minimal.pdf", "split-out.pdf");
1831 # Also tests @- for reading args from stdin 1832 # Also tests @- for reading args from stdin
1832 $td->runtest("don't overwrite self", 1833 $td->runtest("don't overwrite self",
1833 {$td->COMMAND => "(echo a.pdf; echo a.pdf) | qpdf \@-"}, 1834 {$td->COMMAND => "(echo a.pdf; echo a.pdf) | qpdf \@-"},
1834 {$td->REGEXP => "input file and output file are the same.*", 1835 {$td->REGEXP => "input file and output file are the same.*",
1835 $td->EXIT_STATUS => 2}); 1836 $td->EXIT_STATUS => 2});
  1837 +$td->runtest("output is not really output for split",
  1838 + {$td->COMMAND => "qpdf --split-pages split-out.pdf split-out.pdf"},
  1839 + {$td->STRING => "", $td->EXIT_STATUS => 0});
  1840 +$td->runtest("don't overwrite self (split)",
  1841 + {$td->COMMAND =>
  1842 + "qpdf --split-pages split-out-1.pdf split-out.pdf"},
  1843 + {$td->REGEXP =>
  1844 + ".*split pages would overwrite.* split-out-1.pdf",
  1845 + $td->EXIT_STATUS => 2});
1836 1846
1837 show_ntests(); 1847 show_ntests();
1838 # ---------- 1848 # ----------