summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2016-01-06 10:16:32 -0800
committerTom Sepez <tsepez@chromium.org>2016-01-06 10:16:32 -0800
commit0aa3531a87dde44ffd90416f6d621b1828edaf22 (patch)
tree9b5f0e12a5e1aec13fd3d4f0242efcc6834d830e /testing
parentd483eb46d8920547d1b47351e65fa0299eaba191 (diff)
downloadpdfium-0aa3531a87dde44ffd90416f6d621b1828edaf22.tar.xz
Merge to XFA: Return unique_ptrs from test_support functions
Orignal Review URL: https://codereview.chromium.org/1563513002 . (cherry picked from commit aa326bd6b169dc1b5b9b83048c71799799ab34c6) TBR=thestig@chromium.org Review URL: https://codereview.chromium.org/1564583004 .
Diffstat (limited to 'testing')
-rw-r--r--testing/embedder_test.cpp3
-rw-r--r--testing/embedder_test.h3
-rw-r--r--testing/test_support.cpp18
-rw-r--r--testing/test_support.h20
4 files changed, 30 insertions, 14 deletions
diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp
index 7224e227c6..ceca205666 100644
--- a/testing/embedder_test.cpp
+++ b/testing/embedder_test.cpp
@@ -99,7 +99,6 @@ void EmbedderTest::TearDown() {
#endif // PDF_ENABLE_V8
delete loader_;
- free(file_contents_);
}
bool EmbedderTest::CreateEmptyDocument() {
@@ -120,7 +119,7 @@ bool EmbedderTest::OpenDocument(const std::string& filename,
if (!file_contents_)
return false;
- loader_ = new TestLoader(file_contents_, file_length_);
+ loader_ = new TestLoader(file_contents_.get(), file_length_);
file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
file_access_.m_GetBlock = TestLoader::GetBlock;
file_access_.m_Param = loader_;
diff --git a/testing/embedder_test.h b/testing/embedder_test.h
index c780cca74b..a21a55de2c 100644
--- a/testing/embedder_test.h
+++ b/testing/embedder_test.h
@@ -14,6 +14,7 @@
#include "public/fpdf_formfill.h"
#include "public/fpdfview.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/test_support.h"
#ifdef PDF_ENABLE_V8
#include "v8/include/v8.h"
@@ -129,7 +130,7 @@ class EmbedderTest : public ::testing::Test,
void* external_isolate_;
TestLoader* loader_;
size_t file_length_;
- char* file_contents_;
+ std::unique_ptr<char, pdfium::FreeDeleter> file_contents_;
private:
static void UnsupportedHandlerTrampoline(UNSUPPORT_INFO*, int type);
diff --git a/testing/test_support.cpp b/testing/test_support.cpp
index c7ab10ee3c..ce143ae8e9 100644
--- a/testing/test_support.cpp
+++ b/testing/test_support.cpp
@@ -71,7 +71,8 @@ void InitializeV8Common(v8::Platform** platform) {
} // namespace
-char* GetFileContents(const char* filename, size_t* retlen) {
+std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
+ size_t* retlen) {
FILE* file = fopen(filename, "rb");
if (!file) {
fprintf(stderr, "Failed to open: %s\n", filename);
@@ -83,15 +84,15 @@ char* GetFileContents(const char* filename, size_t* retlen) {
return nullptr;
}
(void)fseek(file, 0, SEEK_SET);
- char* buffer = (char*)malloc(file_length);
+ std::unique_ptr<char, pdfium::FreeDeleter> buffer(
+ static_cast<char*>(malloc(file_length)));
if (!buffer) {
return nullptr;
}
- size_t bytes_read = fread(buffer, 1, file_length, file);
+ size_t bytes_read = fread(buffer.get(), 1, file_length, file);
(void)fclose(file);
if (bytes_read != file_length) {
fprintf(stderr, "Failed to read: %s\n", filename);
- free(buffer);
return nullptr;
}
*retlen = bytes_read;
@@ -114,9 +115,12 @@ std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
return platform_string;
}
-FPDF_WIDESTRING GetFPDFWideString(const std::wstring& wstr) {
+std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
+ const std::wstring& wstr) {
size_t length = sizeof(uint16_t) * (wstr.length() + 1);
- unsigned char* ptr = static_cast<unsigned char*>(malloc(length));
+ std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
+ static_cast<unsigned short*>(malloc(length)));
+ char* ptr = reinterpret_cast<char*>(result.get());
size_t i = 0;
for (wchar_t w : wstr) {
ptr[i++] = w & 0xff;
@@ -124,7 +128,7 @@ FPDF_WIDESTRING GetFPDFWideString(const std::wstring& wstr) {
}
ptr[i++] = 0;
ptr[i] = 0;
- return reinterpret_cast<FPDF_WIDESTRING>(ptr);
+ return result;
}
#ifdef PDF_ENABLE_V8
diff --git a/testing/test_support.h b/testing/test_support.h
index d48d5596f4..945704194d 100644
--- a/testing/test_support.h
+++ b/testing/test_support.h
@@ -6,6 +6,7 @@
#define TESTING_EMBEDDER_TEST_SUPPORT_H_
#include <stdlib.h>
+#include <memory>
#include <string>
#include "public/fpdfview.h"
@@ -14,16 +15,27 @@
#include "v8/include/v8.h"
#endif // PDF_ENABLE_V8
-// Reads the entire contents of a file into a newly malloc'd buffer.
-char* GetFileContents(const char* filename, size_t* retlen);
+namespace pdfium {
+
+// Used with std::unique_ptr to free() objects that can't be deleted.
+struct FreeDeleter {
+ inline void operator()(void* ptr) const { free(ptr); }
+};
+
+} // namespace pdfium
+
+// Reads the entire contents of a file into a newly alloc'd buffer.
+std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
+ size_t* retlen);
// Converts a FPDF_WIDESTRING to a std::wstring.
// Deals with differences between UTF16LE and wchar_t.
std::wstring GetPlatformWString(const FPDF_WIDESTRING wstr);
-// Returns a newly mallocated FPDF_WIDESTRING (caller must free()).
+// Returns a newly allocated FPDF_WIDESTRING.
// Deals with differences between UTF16LE and wchar_t.
-FPDF_WIDESTRING GetFPDFWideString(const std::wstring& wstr);
+std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
+ const std::wstring& wstr);
#ifdef PDF_ENABLE_V8
#ifdef V8_USE_EXTERNAL_STARTUP_DATA