From 039eb4a253c7faed9a906a7f45f2c9f1f2a76002 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 23 Feb 2021 08:08:48 -0500 Subject: [PATCH] Fix input file = output file test for split pages --- ChangeLog | 6 ++++++ qpdf/qpdf.cc | 9 ++++++++- qpdf/qtest/qpdf.test | 12 +++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index ab4bb571..3a53753d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2021-02-23 Jay Berkenbilt + + * The test for the input and output files being the same wasn't + implemented correctly for --split-pages since the specified output + file is really a pattern, not the actual output file. + 2021-02-22 Jay Berkenbilt * From qpdf CLI, --pages and --split-pages will properly preserve diff --git a/qpdf/qpdf.cc b/qpdf/qpdf.cc index 75f046ca..ea52d6d3 100644 --- a/qpdf/qpdf.cc +++ b/qpdf/qpdf.cc @@ -3738,7 +3738,7 @@ ArgParser::doFinalChecks() } } - if (QUtil::same_file(o.infilename, o.outfilename)) + if ((! o.split_pages) && QUtil::same_file(o.infilename, o.outfilename)) { QTC::TC("qpdf", "qpdf same file error"); usage("input file and output file are the same;" @@ -6389,6 +6389,13 @@ static void do_split_pages(QPDF& pdf, Options& o, bool& warnings) QUtil::uint_to_string(last, QIntC::to_int(pageno_len)); } std::string outfile = before + page_range + after; + if (QUtil::same_file(o.infilename, outfile.c_str())) + { + std::cerr << whoami + << ": split pages would overwrite input file with " + << outfile << std::endl; + exit(EXIT_ERROR); + } QPDFWriter w(outpdf, outfile.c_str()); set_writer_options(outpdf, o, w); w.write(); diff --git a/qpdf/qtest/qpdf.test b/qpdf/qtest/qpdf.test index da98020a..7bfe21cd 100644 --- a/qpdf/qtest/qpdf.test +++ b/qpdf/qtest/qpdf.test @@ -1825,14 +1825,24 @@ $td->runtest("out of range in deleted object", show_ntests(); # ---------- $td->notify("--- Overwrite self ---"); -$n_tests += 1; +$n_tests += 3; copy("minimal.pdf", "a.pdf"); +copy("minimal.pdf", "split-out.pdf"); # Also tests @- for reading args from stdin $td->runtest("don't overwrite self", {$td->COMMAND => "(echo a.pdf; echo a.pdf) | qpdf \@-"}, {$td->REGEXP => "input file and output file are the same.*", $td->EXIT_STATUS => 2}); +$td->runtest("output is not really output for split", + {$td->COMMAND => "qpdf --split-pages split-out.pdf split-out.pdf"}, + {$td->STRING => "", $td->EXIT_STATUS => 0}); +$td->runtest("don't overwrite self (split)", + {$td->COMMAND => + "qpdf --split-pages split-out-1.pdf split-out.pdf"}, + {$td->REGEXP => + ".*split pages would overwrite.* split-out-1.pdf", + $td->EXIT_STATUS => 2}); show_ntests(); # ----------