aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-02-05 14:15:07 +0100
committerJay Berkenbilt <ejb@ql.org>2022-02-05 17:24:56 +0100
commit5f3f78822b5d43e9b02082da5268d186ba7101c0 (patch)
treec7d2407df01e4c622371ad290f9681a5ae99a476 /libqpdf
parent88c3d556d552a94aa42eaa2fa43667952e123dc8 (diff)
downloadqpdf-5f3f78822b5d43e9b02082da5268d186ba7101c0.tar.zst
Improve use of std::unique_ptr
* Use unique_ptr in place of shared_ptr in some cases * unique_ptr for arrays does not require a custom deleter * use std::make_unique (c++14) where possible
Diffstat (limited to 'libqpdf')
-rw-r--r--libqpdf/AES_PDF_native.cc8
-rw-r--r--libqpdf/Pl_AES_PDF.cc4
-rw-r--r--libqpdf/QPDFArgParser.cc2
-rw-r--r--libqpdf/QPDFJob.cc2
-rw-r--r--libqpdf/QPDFWriter.cc2
-rw-r--r--libqpdf/QPDF_encryption.cc2
-rw-r--r--libqpdf/QUtil.cc18
-rw-r--r--libqpdf/qpdfjob-c.cc2
8 files changed, 21 insertions, 19 deletions
diff --git a/libqpdf/AES_PDF_native.cc b/libqpdf/AES_PDF_native.cc
index 26df9540..afbc0bdc 100644
--- a/libqpdf/AES_PDF_native.cc
+++ b/libqpdf/AES_PDF_native.cc
@@ -19,12 +19,8 @@ AES_PDF_native::AES_PDF_native(bool encrypt, unsigned char const* key,
nrounds(0)
{
size_t keybits = 8 * key_bytes;
- this->key = std::unique_ptr<unsigned char[]>(
- new unsigned char[key_bytes],
- std::default_delete<unsigned char[]>());
- this->rk = std::unique_ptr<uint32_t[]>(
- new uint32_t[RKLENGTH(keybits)],
- std::default_delete<uint32_t[]>());
+ this->key = std::make_unique<unsigned char[]>(key_bytes);
+ this->rk = std::make_unique<uint32_t[]>(RKLENGTH(keybits));
size_t rk_bytes = RKLENGTH(keybits) * sizeof(uint32_t);
std::memcpy(this->key.get(), key, key_bytes);
std::memset(this->rk.get(), 0, rk_bytes);
diff --git a/libqpdf/Pl_AES_PDF.cc b/libqpdf/Pl_AES_PDF.cc
index 6a58a321..de1f666f 100644
--- a/libqpdf/Pl_AES_PDF.cc
+++ b/libqpdf/Pl_AES_PDF.cc
@@ -25,9 +25,7 @@ Pl_AES_PDF::Pl_AES_PDF(char const* identifier, Pipeline* next,
use_specified_iv(false),
disable_padding(false)
{
- this->key = std::unique_ptr<unsigned char[]>(
- new unsigned char[key_bytes],
- std::default_delete<unsigned char[]>());
+ this->key = std::make_unique<unsigned char[]>(key_bytes);
std::memcpy(this->key.get(), key, key_bytes);
std::memset(this->inbuf, 0, this->buf_size);
std::memset(this->outbuf, 0, this->buf_size);
diff --git a/libqpdf/QPDFArgParser.cc b/libqpdf/QPDFArgParser.cc
index 7e0ce7e4..b1658fea 100644
--- a/libqpdf/QPDFArgParser.cc
+++ b/libqpdf/QPDFArgParser.cc
@@ -20,7 +20,7 @@ QPDFArgParser::Members::Members(
option_table(nullptr),
final_check_handler(nullptr)
{
- auto tmp = QUtil::make_shared_cstr(argv[0]);
+ auto tmp = QUtil::make_unique_cstr(argv[0]);
char* p = QUtil::getWhoami(tmp.get());
// Remove prefix added by libtool for consistency during testing.
if (strncmp(p, "lt-", 3) == 0)
diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc
index 41166f9e..646692d5 100644
--- a/libqpdf/QPDFJob.cc
+++ b/libqpdf/QPDFJob.cc
@@ -3379,7 +3379,7 @@ QPDFJob::setEncryptionOptions(QPDF& pdf, QPDFWriter& w)
static void parse_version(std::string const& full_version_string,
std::string& version, int& extension_level)
{
- auto vp = QUtil::make_shared_cstr(full_version_string);
+ auto vp = QUtil::make_unique_cstr(full_version_string);
char* v = vp.get();
char* p1 = strchr(v, '.');
char* p2 = (p1 ? strchr(1 + p1, '.') : 0);
diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc
index 66bf677a..5f7b39a5 100644
--- a/libqpdf/QPDFWriter.cc
+++ b/libqpdf/QPDFWriter.cc
@@ -1919,7 +1919,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
}
else
{
- auto tmp_ph = QUtil::make_shared_cstr(val);
+ auto tmp_ph = QUtil::make_unique_cstr(val);
char* tmp = tmp_ph.get();
size_t vlen = val.length();
RC4 rc4(QUtil::unsigned_char_pointer(this->m->cur_data_key),
diff --git a/libqpdf/QPDF_encryption.cc b/libqpdf/QPDF_encryption.cc
index 54c2dadc..5ce63e4c 100644
--- a/libqpdf/QPDF_encryption.cc
+++ b/libqpdf/QPDF_encryption.cc
@@ -1211,7 +1211,7 @@ 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.
- auto tmp = QUtil::make_shared_cstr(str);
+ auto tmp = QUtil::make_unique_cstr(str);
RC4 rc4(QUtil::unsigned_char_pointer(key), toI(key.length()));
rc4.process(QUtil::unsigned_char_pointer(tmp.get()), vlen);
str = std::string(tmp.get(), vlen);
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc
index e3666d02..503d79f9 100644
--- a/libqpdf/QUtil.cc
+++ b/libqpdf/QUtil.cc
@@ -744,6 +744,16 @@ QUtil::make_shared_cstr(std::string const& str)
return result;
}
+std::unique_ptr<char[]>
+QUtil::make_unique_cstr(std::string const& str)
+{
+ auto result = std::make_unique<char[]>(str.length() + 1);
+ // 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 +2635,7 @@ call_main_from_wmain(bool, int argc, wchar_t const* const argv[],
// other systems. That way the rest of qpdf.cc can just act like
// arguments are UTF-8.
- std::vector<std::shared_ptr<char>> utf8_argv;
+ std::vector<std::unique_ptr<char[]>> utf8_argv;
for (int i = 0; i < argc; ++i)
{
std::string utf16;
@@ -2638,11 +2648,9 @@ call_main_from_wmain(bool, int argc, wchar_t const* const argv[],
QIntC::to_uchar(codepoint & 0xff)));
}
std::string utf8 = QUtil::utf16_to_utf8(utf16);
- utf8_argv.push_back(QUtil::make_shared_cstr(utf8));
+ utf8_argv.push_back(QUtil::make_unique_cstr(utf8));
}
- auto utf8_argv_sp =
- std::shared_ptr<char*>(
- new char*[1+utf8_argv.size()], std::default_delete<char*[]>());
+ auto utf8_argv_sp = std::make_unique<char*[]>(1+utf8_argv.size());
char** new_argv = utf8_argv_sp.get();
for (size_t i = 0; i < utf8_argv.size(); ++i)
{
diff --git a/libqpdf/qpdfjob-c.cc b/libqpdf/qpdfjob-c.cc
index 10e13043..98bd82df 100644
--- a/libqpdf/qpdfjob-c.cc
+++ b/libqpdf/qpdfjob-c.cc
@@ -9,7 +9,7 @@
int qpdfjob_run_from_argv(char const* const argv[])
{
- auto whoami_p = QUtil::make_shared_cstr(argv[0]);
+ auto whoami_p = QUtil::make_unique_cstr(argv[0]);
auto whoami = QUtil::getWhoami(whoami_p.get());
QUtil::setLineBuf(stdout);