From 155345e2c47188fa65da1603b7f06c986707bb69 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 23 Sep 2015 14:26:51 -0700 Subject: Fix a couple potential file handle leaks in pdfium_test. Fix lint issues / git cl format. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1357423006 . --- samples/pdfium_test.cc | 54 +++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc index cb8fef324e..a4c4ec2b73 100644 --- a/samples/pdfium_test.cc +++ b/samples/pdfium_test.cc @@ -54,24 +54,26 @@ static char* GetFileContents(const char* filename, size_t* retlen) { FILE* file = fopen(filename, "rb"); if (!file) { fprintf(stderr, "Failed to open: %s\n", filename); - return NULL; + return nullptr; } - (void) fseek(file, 0, SEEK_END); + (void)fseek(file, 0, SEEK_END); size_t file_length = ftell(file); if (!file_length) { - return NULL; + (void)fclose(file); + return nullptr; } - (void) fseek(file, 0, SEEK_SET); - char* buffer = (char*) malloc(file_length); + (void)fseek(file, 0, SEEK_SET); + char* buffer = static_cast(malloc(file_length)); if (!buffer) { - return NULL; + (void)fclose(file); + return nullptr; } size_t bytes_read = fread(buffer, 1, file_length, file); - (void) fclose(file); + (void)fclose(file); if (bytes_read != file_length) { fprintf(stderr, "Failed to read: %s\n", filename); free(buffer); - return NULL; + return nullptr; } *retlen = bytes_read; return buffer; @@ -194,7 +196,7 @@ static void WritePng(const char* pdf_name, int num, const void* buffer_void, if (bytes_written != png_encoding.size()) fprintf(stderr, "Failed to write to %s\n", filename); - (void) fclose(fp); + (void)fclose(fp); } #ifdef _WIN32 @@ -241,7 +243,7 @@ void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { char filename[256]; snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num); - HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL); + HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr); HRGN rgn = CreateRectRgn(0, 0, width, height); SelectClipRgn(dc, rgn); @@ -267,7 +269,7 @@ int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING, ++characters; } wchar_t* platform_string = - (wchar_t*)malloc((characters + 1) * sizeof(wchar_t)); + static_cast(malloc((characters + 1) * sizeof(wchar_t))); for (size_t i = 0; i < characters + 1; ++i) { unsigned char* ptr = (unsigned char*)&msg[i]; platform_string[i] = ptr[0] + 256 * ptr[1]; @@ -412,9 +414,11 @@ TestLoader::TestLoader(const char* pBuf, size_t len) : m_pBuf(pBuf), m_Len(len) { } -int Get_Block(void* param, unsigned long pos, unsigned char* pBuf, - unsigned long size) { - TestLoader* pLoader = (TestLoader*) param; +int GetBlock(void* param, + unsigned long pos, + unsigned char* pBuf, + unsigned long size) { + TestLoader* pLoader = static_cast(param); if (pos + size < pos || pos + size > pLoader->m_Len) return 0; memcpy(pBuf, pLoader->m_pBuf + pos, size); return 1; @@ -447,7 +451,7 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len, FPDF_FILEACCESS file_access; memset(&file_access, '\0', sizeof(file_access)); file_access.m_FileLen = static_cast(len); - file_access.m_GetBlock = Get_Block; + file_access.m_GetBlock = GetBlock; file_access.m_Param = &loader; FX_FILEAVAIL file_avail; @@ -463,29 +467,29 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len, FPDF_DOCUMENT doc; FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); - (void) FPDFAvail_IsDocAvail(pdf_avail, &hints); + (void)FPDFAvail_IsDocAvail(pdf_avail, &hints); if (!FPDFAvail_IsLinearized(pdf_avail)) { fprintf(stderr, "Non-linearized path...\n"); - doc = FPDF_LoadCustomDocument(&file_access, NULL); + doc = FPDF_LoadCustomDocument(&file_access, nullptr); } else { fprintf(stderr, "Linearized path...\n"); - doc = FPDFAvail_GetDocument(pdf_avail, NULL); + doc = FPDFAvail_GetDocument(pdf_avail, nullptr); } - (void) FPDF_GetDocPermissions(doc); - (void) FPDFAvail_IsFormAvail(pdf_avail, &hints); + (void)FPDF_GetDocPermissions(doc); + (void)FPDFAvail_IsFormAvail(pdf_avail, &hints); FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); FPDF_SetFormFieldHighlightAlpha(form, 100); int first_page = FPDFAvail_GetFirstPageNum(doc); - (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints); + (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints); int page_count = FPDF_GetPageCount(doc); for (int i = 0; i < page_count; ++i) { - (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints); + (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints); } FORM_DoDocumentJSAction(form); @@ -496,8 +500,8 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len, for (int i = 0; i < page_count; ++i) { FPDF_PAGE page = FPDF_LoadPage(doc, i); if (!page) { - bad_pages ++; - continue; + ++bad_pages; + continue; } FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); FORM_OnAfterLoadPage(page, form); @@ -519,7 +523,7 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len, FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); - rendered_pages ++; + ++rendered_pages; FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); int stride = FPDFBitmap_GetStride(bitmap); -- cgit v1.2.3