diff --git a/include/qpdf/QUtil.hh b/include/qpdf/QUtil.hh index 927fc1d..b4cb1f6 100644 --- a/include/qpdf/QUtil.hh +++ b/include/qpdf/QUtil.hh @@ -165,6 +165,15 @@ namespace QUtil QPDF_DLL std::unique_ptr make_unique_cstr(std::string const&); + // Create a shared pointer to an array. From c++20, + // std::make_shared(n) does this. + template + std::shared_ptr + make_shared_array(size_t n) + { + return std::shared_ptr(new T[n], std::default_delete()); + } + // Returns lower-case hex-encoded version of the string, treating // each character in the input string as unsigned. The output // string will be twice as long as the input string. diff --git a/libqpdf/QPDFArgParser.cc b/libqpdf/QPDFArgParser.cc index b1658fe..9ddb120 100644 --- a/libqpdf/QPDFArgParser.cc +++ b/libqpdf/QPDFArgParser.cc @@ -299,9 +299,8 @@ QPDFArgParser::handleArgFileArguments() QUtil::make_shared_cstr(this->m->argv[i])); } } - this->m->argv_ph = std::shared_ptr( - new char const*[1 + this->m->new_argv.size()], - std::default_delete()); + this->m->argv_ph = QUtil::make_shared_array( + 1 + this->m->new_argv.size()); for (size_t i = 0; i < this->m->new_argv.size(); ++i) { this->m->argv_ph.get()[i] = this->m->new_argv.at(i).get(); @@ -404,9 +403,8 @@ QPDFArgParser::handleBashArguments() } // Explicitly discard any non-space-terminated word. The "current // word" is handled specially. - this->m->bash_argv_ph = std::shared_ptr( - new char const*[1 + this->m->bash_argv.size()], - std::default_delete()); + this->m->bash_argv_ph = QUtil::make_shared_array( + 1 + this->m->bash_argv.size()); for (size_t i = 0; i < this->m->bash_argv.size(); ++i) { this->m->bash_argv_ph.get()[i] = this->m->bash_argv.at(i).get(); diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 503d79f..1f179fd 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -735,9 +735,7 @@ QUtil::copy_string(std::string const& str) std::shared_ptr QUtil::make_shared_cstr(std::string const& str) { - auto result = std::shared_ptr( - new char[str.length() + 1], - std::default_delete()); + auto result = QUtil::make_shared_array(str.length() + 1); // Use memcpy in case string contains nulls result.get()[str.length()] = '\0'; memcpy(result.get(), str.c_str(), str.length());