diff options
Diffstat (limited to 'testing')
-rw-r--r-- | testing/embedder_test.cpp | 3 | ||||
-rw-r--r-- | testing/embedder_test.h | 3 | ||||
-rw-r--r-- | testing/test_support.cpp | 18 | ||||
-rw-r--r-- | testing/test_support.h | 20 |
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 |