Commit df2f5c6a360bd7512d2280aa9cb582bc0aa622bd
1 parent
cfaae47d
Add QUtil::make_shared_array to help with PointerHolder transition
Showing
3 changed files
with
14 additions
and
9 deletions
include/qpdf/QUtil.hh
| @@ -165,6 +165,15 @@ namespace QUtil | @@ -165,6 +165,15 @@ namespace QUtil | ||
| 165 | QPDF_DLL | 165 | QPDF_DLL |
| 166 | std::unique_ptr<char[]> make_unique_cstr(std::string const&); | 166 | std::unique_ptr<char[]> make_unique_cstr(std::string const&); |
| 167 | 167 | ||
| 168 | + // Create a shared pointer to an array. From c++20, | ||
| 169 | + // std::make_shared<T[]>(n) does this. | ||
| 170 | + template <typename T> | ||
| 171 | + std::shared_ptr<T> | ||
| 172 | + make_shared_array(size_t n) | ||
| 173 | + { | ||
| 174 | + return std::shared_ptr<T>(new T[n], std::default_delete<T[]>()); | ||
| 175 | + } | ||
| 176 | + | ||
| 168 | // Returns lower-case hex-encoded version of the string, treating | 177 | // Returns lower-case hex-encoded version of the string, treating |
| 169 | // each character in the input string as unsigned. The output | 178 | // each character in the input string as unsigned. The output |
| 170 | // string will be twice as long as the input string. | 179 | // string will be twice as long as the input string. |
libqpdf/QPDFArgParser.cc
| @@ -299,9 +299,8 @@ QPDFArgParser::handleArgFileArguments() | @@ -299,9 +299,8 @@ QPDFArgParser::handleArgFileArguments() | ||
| 299 | QUtil::make_shared_cstr(this->m->argv[i])); | 299 | QUtil::make_shared_cstr(this->m->argv[i])); |
| 300 | } | 300 | } |
| 301 | } | 301 | } |
| 302 | - this->m->argv_ph = std::shared_ptr<char const*>( | ||
| 303 | - new char const*[1 + this->m->new_argv.size()], | ||
| 304 | - std::default_delete<char const*[]>()); | 302 | + this->m->argv_ph = QUtil::make_shared_array<char const*>( |
| 303 | + 1 + this->m->new_argv.size()); | ||
| 305 | for (size_t i = 0; i < this->m->new_argv.size(); ++i) | 304 | for (size_t i = 0; i < this->m->new_argv.size(); ++i) |
| 306 | { | 305 | { |
| 307 | this->m->argv_ph.get()[i] = this->m->new_argv.at(i).get(); | 306 | this->m->argv_ph.get()[i] = this->m->new_argv.at(i).get(); |
| @@ -404,9 +403,8 @@ QPDFArgParser::handleBashArguments() | @@ -404,9 +403,8 @@ QPDFArgParser::handleBashArguments() | ||
| 404 | } | 403 | } |
| 405 | // Explicitly discard any non-space-terminated word. The "current | 404 | // Explicitly discard any non-space-terminated word. The "current |
| 406 | // word" is handled specially. | 405 | // word" is handled specially. |
| 407 | - this->m->bash_argv_ph = std::shared_ptr<char const*>( | ||
| 408 | - new char const*[1 + this->m->bash_argv.size()], | ||
| 409 | - std::default_delete<char const*[]>()); | 406 | + this->m->bash_argv_ph = QUtil::make_shared_array<char const*>( |
| 407 | + 1 + this->m->bash_argv.size()); | ||
| 410 | for (size_t i = 0; i < this->m->bash_argv.size(); ++i) | 408 | for (size_t i = 0; i < this->m->bash_argv.size(); ++i) |
| 411 | { | 409 | { |
| 412 | this->m->bash_argv_ph.get()[i] = this->m->bash_argv.at(i).get(); | 410 | this->m->bash_argv_ph.get()[i] = this->m->bash_argv.at(i).get(); |
libqpdf/QUtil.cc
| @@ -735,9 +735,7 @@ QUtil::copy_string(std::string const& str) | @@ -735,9 +735,7 @@ QUtil::copy_string(std::string const& str) | ||
| 735 | std::shared_ptr<char> | 735 | std::shared_ptr<char> |
| 736 | QUtil::make_shared_cstr(std::string const& str) | 736 | QUtil::make_shared_cstr(std::string const& str) |
| 737 | { | 737 | { |
| 738 | - auto result = std::shared_ptr<char>( | ||
| 739 | - new char[str.length() + 1], | ||
| 740 | - std::default_delete<char[]>()); | 738 | + auto result = QUtil::make_shared_array<char>(str.length() + 1); |
| 741 | // Use memcpy in case string contains nulls | 739 | // Use memcpy in case string contains nulls |
| 742 | result.get()[str.length()] = '\0'; | 740 | result.get()[str.length()] = '\0'; |
| 743 | memcpy(result.get(), str.c_str(), str.length()); | 741 | memcpy(result.get(), str.c_str(), str.length()); |