summaryrefslogtreecommitdiff
path: root/core/fpdfapi/parser
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2016-12-15 13:51:34 -0800
committerCommit bot <commit-bot@chromium.org>2016-12-15 13:51:34 -0800
commit47fb8c06acd0ff9ea50c8c2d7f67510ea5c28577 (patch)
treeb6c3594bdf3690d00f2fd959f06cef1d45ae0f71 /core/fpdfapi/parser
parentb04b96b1ea3ef88385bbf736b2bccab7ed12c486 (diff)
downloadpdfium-47fb8c06acd0ff9ea50c8c2d7f67510ea5c28577.tar.xz
Return unique_ptr from CFX_BinaryBuf::DetachBuffer()
In turn, make CPDF_Stream() take an unique_ptr. Review-Url: https://codereview.chromium.org/2584683002
Diffstat (limited to 'core/fpdfapi/parser')
-rw-r--r--core/fpdfapi/parser/cpdf_object_unittest.cpp19
-rw-r--r--core/fpdfapi/parser/cpdf_stream.cpp6
-rw-r--r--core/fpdfapi/parser/cpdf_stream.h2
-rw-r--r--core/fpdfapi/parser/cpdf_syntax_parser.cpp15
4 files changed, 21 insertions, 21 deletions
diff --git a/core/fpdfapi/parser/cpdf_object_unittest.cpp b/core/fpdfapi/parser/cpdf_object_unittest.cpp
index 303ad6fa7a..4977c9931f 100644
--- a/core/fpdfapi/parser/cpdf_object_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_object_unittest.cpp
@@ -69,14 +69,14 @@ class PDFObjectsTest : public testing::Test {
// Stream object.
const char content[] = "abcdefghijklmnopqrstuvwxyz";
size_t buf_len = FX_ArraySize(content);
- uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_len));
- memcpy(buf, content, buf_len);
+ std::unique_ptr<uint8_t, FxFreeDeleter> buf(FX_Alloc(uint8_t, buf_len));
+ memcpy(buf.get(), content, buf_len);
auto pNewDict = pdfium::MakeUnique<CPDF_Dictionary>();
m_StreamDictObj = pNewDict.get();
m_StreamDictObj->SetNewFor<CPDF_String>("key1", L" test dict");
m_StreamDictObj->SetNewFor<CPDF_Number>("key2", -1);
CPDF_Stream* stream_obj =
- new CPDF_Stream(buf, buf_len, std::move(pNewDict));
+ new CPDF_Stream(std::move(buf), buf_len, std::move(pNewDict));
// Null Object.
CPDF_Null* null_obj = new CPDF_Null;
// All direct objects.
@@ -588,9 +588,10 @@ TEST(PDFArrayTest, GetTypeAt) {
}
uint8_t content[] = "content: this is a stream";
size_t data_size = FX_ArraySize(content);
- uint8_t* data = reinterpret_cast<uint8_t*>(malloc(data_size));
- memcpy(data, content, data_size);
- stream_vals[i] = arr->AddNew<CPDF_Stream>(data, data_size,
+ std::unique_ptr<uint8_t, FxFreeDeleter> data(
+ FX_Alloc(uint8_t, data_size));
+ memcpy(data.get(), content, data_size);
+ stream_vals[i] = arr->AddNew<CPDF_Stream>(std::move(data), data_size,
pdfium::WrapUnique(vals[i]));
}
for (size_t i = 0; i < 3; ++i) {
@@ -634,10 +635,10 @@ TEST(PDFArrayTest, GetTypeAt) {
// The data buffer will be owned by stream object, so it needs to be
// dynamically allocated.
size_t buf_size = sizeof(data);
- uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_size));
- memcpy(buf, data, buf_size);
+ std::unique_ptr<uint8_t, FxFreeDeleter> buf(FX_Alloc(uint8_t, buf_size));
+ memcpy(buf.get(), data, buf_size);
CPDF_Stream* stream_val = arr->InsertNewAt<CPDF_Stream>(
- 13, buf, buf_size, pdfium::WrapUnique(stream_dict));
+ 13, std::move(buf), buf_size, pdfium::WrapUnique(stream_dict));
const char* const expected_str[] = {
"true", "false", "0", "-1234", "2345", "0.05", "",
"It is a test!", "NAME", "test", "", "", "", ""};
diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp
index 3159791e82..e186bc19d1 100644
--- a/core/fpdfapi/parser/cpdf_stream.cpp
+++ b/core/fpdfapi/parser/cpdf_stream.cpp
@@ -18,10 +18,10 @@
CPDF_Stream::CPDF_Stream() {}
-CPDF_Stream::CPDF_Stream(uint8_t* pData,
+CPDF_Stream::CPDF_Stream(std::unique_ptr<uint8_t, FxFreeDeleter> pData,
uint32_t size,
std::unique_ptr<CPDF_Dictionary> pDict)
- : m_dwSize(size), m_pDict(std::move(pDict)), m_pDataBuf(pData) {}
+ : m_dwSize(size), m_pDict(std::move(pDict)), m_pDataBuf(std::move(pData)) {}
CPDF_Stream::~CPDF_Stream() {
m_ObjNum = kInvalidObjNum;
@@ -93,7 +93,7 @@ std::unique_ptr<CPDF_Object> CPDF_Stream::CloneNonCyclic(
pNewDict = ToDictionary(
static_cast<CPDF_Object*>(pDict)->CloneNonCyclic(bDirect, pVisited));
}
- return pdfium::MakeUnique<CPDF_Stream>(acc.DetachData().release(), streamSize,
+ return pdfium::MakeUnique<CPDF_Stream>(acc.DetachData(), streamSize,
std::move(pNewDict));
}
diff --git a/core/fpdfapi/parser/cpdf_stream.h b/core/fpdfapi/parser/cpdf_stream.h
index cd4113b22b..902cd75365 100644
--- a/core/fpdfapi/parser/cpdf_stream.h
+++ b/core/fpdfapi/parser/cpdf_stream.h
@@ -19,7 +19,7 @@ class CPDF_Stream : public CPDF_Object {
CPDF_Stream();
// Takes ownership of |pData|.
- CPDF_Stream(uint8_t* pData,
+ CPDF_Stream(std::unique_ptr<uint8_t, FxFreeDeleter> pData,
uint32_t size,
std::unique_ptr<CPDF_Dictionary> pDict);
diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.cpp b/core/fpdfapi/parser/cpdf_syntax_parser.cpp
index 2a20e43591..48d77c2cbd 100644
--- a/core/fpdfapi/parser/cpdf_syntax_parser.cpp
+++ b/core/fpdfapi/parser/cpdf_syntax_parser.cpp
@@ -723,25 +723,24 @@ std::unique_ptr<CPDF_Stream> CPDF_SyntaxParser::ReadStream(
if (len < 0)
return nullptr;
- uint8_t* pData = nullptr;
+ std::unique_ptr<uint8_t, FxFreeDeleter> pData;
if (len > 0) {
- pData = FX_Alloc(uint8_t, len);
- ReadBlock(pData, len);
+ pData.reset(FX_Alloc(uint8_t, len));
+ ReadBlock(pData.get(), len);
if (pCryptoHandler) {
CFX_BinaryBuf dest_buf;
dest_buf.EstimateSize(pCryptoHandler->DecryptGetSize(len));
void* context = pCryptoHandler->DecryptStart(objnum, gennum);
- pCryptoHandler->DecryptStream(context, pData, len, dest_buf);
+ pCryptoHandler->DecryptStream(context, pData.get(), len, dest_buf);
pCryptoHandler->DecryptFinish(context, dest_buf);
- FX_Free(pData);
- pData = dest_buf.GetBuffer();
len = dest_buf.GetSize();
- dest_buf.DetachBuffer();
+ pData = dest_buf.DetachBuffer();
}
}
- auto pStream = pdfium::MakeUnique<CPDF_Stream>(pData, len, std::move(pDict));
+ auto pStream =
+ pdfium::MakeUnique<CPDF_Stream>(std::move(pData), len, std::move(pDict));
streamStartPos = m_Pos;
FXSYS_memset(m_WordBuffer, 0, kEndObjStr.GetLength() + 1);
GetNextWordInternal(nullptr);