diff options
-rw-r--r-- | include/qpdf/QUtil.hh | 9 | ||||
-rw-r--r-- | libqpdf/QPDFArgParser.cc | 10 | ||||
-rw-r--r-- | libqpdf/QUtil.cc | 4 |
3 files changed, 14 insertions, 9 deletions
diff --git a/include/qpdf/QUtil.hh b/include/qpdf/QUtil.hh index 927fc1d8..b4cb1f6a 100644 --- a/include/qpdf/QUtil.hh +++ b/include/qpdf/QUtil.hh @@ -165,6 +165,15 @@ namespace QUtil QPDF_DLL std::unique_ptr<char[]> make_unique_cstr(std::string const&); + // Create a shared pointer to an array. From c++20, + // std::make_shared<T[]>(n) does this. + template <typename T> + std::shared_ptr<T> + make_shared_array(size_t n) + { + return std::shared_ptr<T>(new T[n], std::default_delete<T[]>()); + } + // 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 b1658fea..9ddb120c 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<char const*>( - new char const*[1 + this->m->new_argv.size()], - std::default_delete<char const*[]>()); + this->m->argv_ph = QUtil::make_shared_array<char const*>( + 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<char const*>( - new char const*[1 + this->m->bash_argv.size()], - std::default_delete<char const*[]>()); + this->m->bash_argv_ph = QUtil::make_shared_array<char const*>( + 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 503d79f9..1f179fd5 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -735,9 +735,7 @@ QUtil::copy_string(std::string const& str) std::shared_ptr<char> QUtil::make_shared_cstr(std::string const& str) { - auto result = std::shared_ptr<char>( - new char[str.length() + 1], - std::default_delete<char[]>()); + auto result = QUtil::make_shared_array<char>(str.length() + 1); // Use memcpy in case string contains nulls result.get()[str.length()] = '\0'; memcpy(result.get(), str.c_str(), str.length()); |