From 76c4f78b5cfd786b90069f7256252229444fdecd Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 22 Jan 2022 17:33:53 -0500 Subject: Add QUtil::make_shared_cstr Replace most of the calls to QUtil::copy_string with this instead. --- libqpdf/QPDFWriter.cc | 5 ++--- libqpdf/QPDF_encryption.cc | 6 +++--- libqpdf/QUtil.cc | 14 +++++++++++++- 3 files changed, 18 insertions(+), 7 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index e7fb9ad6..c0ee68d1 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -1915,9 +1915,8 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level, } else { - PointerHolder tmp_ph = - PointerHolder(true, QUtil::copy_string(val)); - char* tmp = tmp_ph.getPointer(); + auto tmp_ph = QUtil::make_shared_cstr(val); + char* tmp = tmp_ph.get(); size_t vlen = val.length(); RC4 rc4(QUtil::unsigned_char_pointer(this->m->cur_data_key), QIntC::to_int(this->m->cur_data_key.length())); diff --git a/libqpdf/QPDF_encryption.cc b/libqpdf/QPDF_encryption.cc index 9607a598..d5289ad3 100644 --- a/libqpdf/QPDF_encryption.cc +++ b/libqpdf/QPDF_encryption.cc @@ -1211,10 +1211,10 @@ QPDF::decryptString(std::string& str, int objid, int generation) size_t vlen = str.length(); // Using PointerHolder guarantees that tmp will // be freed even if rc4.process throws an exception. - PointerHolder tmp(true, QUtil::copy_string(str)); + auto tmp = QUtil::make_shared_cstr(str); RC4 rc4(QUtil::unsigned_char_pointer(key), toI(key.length())); - rc4.process(QUtil::unsigned_char_pointer(tmp.getPointer()), vlen); - str = std::string(tmp.getPointer(), vlen); + rc4.process(QUtil::unsigned_char_pointer(tmp.get()), vlen); + str = std::string(tmp.get(), vlen); } } catch (QPDFExc&) diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index c71e7923..cfd1bb1e 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -731,6 +731,18 @@ QUtil::copy_string(std::string const& str) return result; } +std::shared_ptr +QUtil::make_shared_cstr(std::string const& str) +{ + auto result = std::shared_ptr( + new char[str.length() + 1], + std::default_delete()); + // Use memcpy in case string contains nulls + result.get()[str.length()] = '\0'; + memcpy(result.get(), str.c_str(), str.length()); + return result; +} + std::string QUtil::hex_encode(std::string const& input) { @@ -2625,7 +2637,7 @@ QUtil::call_main_from_wmain(int argc, wchar_t* argv[], QIntC::to_uchar(codepoint & 0xff))); } std::string utf8 = QUtil::utf16_to_utf8(utf16); - utf8_argv.push_back(std::shared_ptr(QUtil::copy_string(utf8.c_str()), std::default_delete())); + utf8_argv.push_back(QUtil::make_shared_cstr(utf8)); } auto utf8_argv_sp = std::shared_ptr(new char*[1+utf8_argv.size()], std::default_delete()); -- cgit v1.2.3-54-g00ecf