From aa326bd6b169dc1b5b9b83048c71799799ab34c6 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 6 Jan 2016 10:06:12 -0800 Subject: Return unique_ptrs from test_support functions R=thestig@chromium.org Review URL: https://codereview.chromium.org/1563513002 . --- testing/test_support.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'testing/test_support.cpp') 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 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 buffer( + static_cast(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 GetFPDFWideString( + const std::wstring& wstr) { size_t length = sizeof(uint16_t) * (wstr.length() + 1); - unsigned char* ptr = static_cast(malloc(length)); + std::unique_ptr result( + static_cast(malloc(length))); + char* ptr = reinterpret_cast(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(ptr); + return result; } #ifdef PDF_ENABLE_V8 -- cgit v1.2.3