Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: samples/pdfium_test.cc

Issue 1357423006: Fix a couple potential file handle leaks in pdfium_test. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <limits.h> 5 #include <limits.h>
6 #include <stdio.h> 6 #include <stdio.h>
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <list> 10 #include <list>
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 std::string exe_path; 47 std::string exe_path;
48 std::string bin_directory; 48 std::string bin_directory;
49 std::string font_directory; 49 std::string font_directory;
50 }; 50 };
51 51
52 // Reads the entire contents of a file into a newly malloc'd buffer. 52 // Reads the entire contents of a file into a newly malloc'd buffer.
53 static char* GetFileContents(const char* filename, size_t* retlen) { 53 static char* GetFileContents(const char* filename, size_t* retlen) {
54 FILE* file = fopen(filename, "rb"); 54 FILE* file = fopen(filename, "rb");
55 if (!file) { 55 if (!file) {
56 fprintf(stderr, "Failed to open: %s\n", filename); 56 fprintf(stderr, "Failed to open: %s\n", filename);
57 return NULL; 57 return nullptr;
58 } 58 }
59 (void) fseek(file, 0, SEEK_END); 59 (void)fseek(file, 0, SEEK_END);
60 size_t file_length = ftell(file); 60 size_t file_length = ftell(file);
61 if (!file_length) { 61 if (!file_length) {
62 return NULL; 62 (void)fclose(file);
63 return nullptr;
63 } 64 }
64 (void) fseek(file, 0, SEEK_SET); 65 (void)fseek(file, 0, SEEK_SET);
65 char* buffer = (char*) malloc(file_length); 66 char* buffer = static_cast<char*>(malloc(file_length));
66 if (!buffer) { 67 if (!buffer) {
67 return NULL; 68 (void)fclose(file);
69 return nullptr;
68 } 70 }
69 size_t bytes_read = fread(buffer, 1, file_length, file); 71 size_t bytes_read = fread(buffer, 1, file_length, file);
70 (void) fclose(file); 72 (void)fclose(file);
71 if (bytes_read != file_length) { 73 if (bytes_read != file_length) {
72 fprintf(stderr, "Failed to read: %s\n", filename); 74 fprintf(stderr, "Failed to read: %s\n", filename);
73 free(buffer); 75 free(buffer);
74 return NULL; 76 return nullptr;
75 } 77 }
76 *retlen = bytes_read; 78 *retlen = bytes_read;
77 return buffer; 79 return buffer;
78 } 80 }
79 81
80 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 82 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
81 // Returns the full path for an external V8 data file based on either 83 // Returns the full path for an external V8 data file based on either
82 // the currect exectuable path or an explicit override. 84 // the currect exectuable path or an explicit override.
83 static std::string GetFullPathForSnapshotFile(const Options& options, 85 static std::string GetFullPathForSnapshotFile(const Options& options,
84 const std::string& filename) { 86 const std::string& filename) {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 if (!fp) { 189 if (!fp) {
188 fprintf(stderr, "Failed to open %s for output\n", filename); 190 fprintf(stderr, "Failed to open %s for output\n", filename);
189 return; 191 return;
190 } 192 }
191 193
192 size_t bytes_written = fwrite( 194 size_t bytes_written = fwrite(
193 &png_encoding.front(), 1, png_encoding.size(), fp); 195 &png_encoding.front(), 1, png_encoding.size(), fp);
194 if (bytes_written != png_encoding.size()) 196 if (bytes_written != png_encoding.size())
195 fprintf(stderr, "Failed to write to %s\n", filename); 197 fprintf(stderr, "Failed to write to %s\n", filename);
196 198
197 (void) fclose(fp); 199 (void)fclose(fp);
198 } 200 }
199 201
200 #ifdef _WIN32 202 #ifdef _WIN32
201 static void WriteBmp(const char* pdf_name, int num, const void* buffer, 203 static void WriteBmp(const char* pdf_name, int num, const void* buffer,
202 int stride, int width, int height) { 204 int stride, int width, int height) {
203 if (stride < 0 || width < 0 || height < 0) 205 if (stride < 0 || width < 0 || height < 0)
204 return; 206 return;
205 if (height > 0 && width > INT_MAX / height) 207 if (height > 0 && width > INT_MAX / height)
206 return; 208 return;
207 int out_len = stride * height; 209 int out_len = stride * height;
(...skipping 26 matching lines...) Expand all
234 fclose(fp); 236 fclose(fp);
235 } 237 }
236 238
237 void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { 239 void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
238 int width = static_cast<int>(FPDF_GetPageWidth(page)); 240 int width = static_cast<int>(FPDF_GetPageWidth(page));
239 int height = static_cast<int>(FPDF_GetPageHeight(page)); 241 int height = static_cast<int>(FPDF_GetPageHeight(page));
240 242
241 char filename[256]; 243 char filename[256];
242 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num); 244 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
243 245
244 HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL); 246 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
245 247
246 HRGN rgn = CreateRectRgn(0, 0, width, height); 248 HRGN rgn = CreateRectRgn(0, 0, width, height);
247 SelectClipRgn(dc, rgn); 249 SelectClipRgn(dc, rgn);
248 DeleteObject(rgn); 250 DeleteObject(rgn);
249 251
250 SelectObject(dc, GetStockObject(NULL_PEN)); 252 SelectObject(dc, GetStockObject(NULL_PEN));
251 SelectObject(dc, GetStockObject(WHITE_BRUSH)); 253 SelectObject(dc, GetStockObject(WHITE_BRUSH));
252 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. 254 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
253 Rectangle(dc, 0, 0, width + 1, height + 1); 255 Rectangle(dc, 0, 0, width + 1, height + 1);
254 256
255 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, 257 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
256 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); 258 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
257 259
258 DeleteEnhMetaFile(CloseEnhMetaFile(dc)); 260 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
259 } 261 }
260 #endif 262 #endif
261 263
262 int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING, 264 int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING,
263 int, int) { 265 int, int) {
264 // Deal with differences between UTF16LE and wchar_t on this platform. 266 // Deal with differences between UTF16LE and wchar_t on this platform.
265 size_t characters = 0; 267 size_t characters = 0;
266 while (msg[characters]) { 268 while (msg[characters]) {
267 ++characters; 269 ++characters;
268 } 270 }
269 wchar_t* platform_string = 271 wchar_t* platform_string =
270 (wchar_t*)malloc((characters + 1) * sizeof(wchar_t)); 272 static_cast<wchar_t*>(malloc((characters + 1) * sizeof(wchar_t)));
271 for (size_t i = 0; i < characters + 1; ++i) { 273 for (size_t i = 0; i < characters + 1; ++i) {
272 unsigned char* ptr = (unsigned char*)&msg[i]; 274 unsigned char* ptr = (unsigned char*)&msg[i];
273 platform_string[i] = ptr[0] + 256 * ptr[1]; 275 platform_string[i] = ptr[0] + 256 * ptr[1];
274 } 276 }
275 printf("Alert: %ls\n", platform_string); 277 printf("Alert: %ls\n", platform_string);
276 free(platform_string); 278 free(platform_string);
277 return 0; 279 return 0;
278 } 280 }
279 281
280 void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) { 282 void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 TestLoader(const char* pBuf, size_t len); 407 TestLoader(const char* pBuf, size_t len);
406 408
407 const char* m_pBuf; 409 const char* m_pBuf;
408 size_t m_Len; 410 size_t m_Len;
409 }; 411 };
410 412
411 TestLoader::TestLoader(const char* pBuf, size_t len) 413 TestLoader::TestLoader(const char* pBuf, size_t len)
412 : m_pBuf(pBuf), m_Len(len) { 414 : m_pBuf(pBuf), m_Len(len) {
413 } 415 }
414 416
415 int Get_Block(void* param, unsigned long pos, unsigned char* pBuf, 417 int GetBlock(void* param,
416 unsigned long size) { 418 unsigned long pos,
417 TestLoader* pLoader = (TestLoader*) param; 419 unsigned char* pBuf,
420 unsigned long size) {
421 TestLoader* pLoader = static_cast<TestLoader*>(param);
418 if (pos + size < pos || pos + size > pLoader->m_Len) return 0; 422 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
419 memcpy(pBuf, pLoader->m_pBuf + pos, size); 423 memcpy(pBuf, pLoader->m_pBuf + pos, size);
420 return 1; 424 return 1;
421 } 425 }
422 426
423 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { 427 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
424 return true; 428 return true;
425 } 429 }
426 430
427 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { 431 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
(...skipping 12 matching lines...) Expand all
440 FPDF_FORMFILLINFO form_callbacks; 444 FPDF_FORMFILLINFO form_callbacks;
441 memset(&form_callbacks, '\0', sizeof(form_callbacks)); 445 memset(&form_callbacks, '\0', sizeof(form_callbacks));
442 form_callbacks.version = 1; 446 form_callbacks.version = 1;
443 form_callbacks.m_pJsPlatform = &platform_callbacks; 447 form_callbacks.m_pJsPlatform = &platform_callbacks;
444 448
445 TestLoader loader(pBuf, len); 449 TestLoader loader(pBuf, len);
446 450
447 FPDF_FILEACCESS file_access; 451 FPDF_FILEACCESS file_access;
448 memset(&file_access, '\0', sizeof(file_access)); 452 memset(&file_access, '\0', sizeof(file_access));
449 file_access.m_FileLen = static_cast<unsigned long>(len); 453 file_access.m_FileLen = static_cast<unsigned long>(len);
450 file_access.m_GetBlock = Get_Block; 454 file_access.m_GetBlock = GetBlock;
451 file_access.m_Param = &loader; 455 file_access.m_Param = &loader;
452 456
453 FX_FILEAVAIL file_avail; 457 FX_FILEAVAIL file_avail;
454 memset(&file_avail, '\0', sizeof(file_avail)); 458 memset(&file_avail, '\0', sizeof(file_avail));
455 file_avail.version = 1; 459 file_avail.version = 1;
456 file_avail.IsDataAvail = Is_Data_Avail; 460 file_avail.IsDataAvail = Is_Data_Avail;
457 461
458 FX_DOWNLOADHINTS hints; 462 FX_DOWNLOADHINTS hints;
459 memset(&hints, '\0', sizeof(hints)); 463 memset(&hints, '\0', sizeof(hints));
460 hints.version = 1; 464 hints.version = 1;
461 hints.AddSegment = Add_Segment; 465 hints.AddSegment = Add_Segment;
462 466
463 FPDF_DOCUMENT doc; 467 FPDF_DOCUMENT doc;
464 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); 468 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
465 469
466 (void) FPDFAvail_IsDocAvail(pdf_avail, &hints); 470 (void)FPDFAvail_IsDocAvail(pdf_avail, &hints);
467 471
468 if (!FPDFAvail_IsLinearized(pdf_avail)) { 472 if (!FPDFAvail_IsLinearized(pdf_avail)) {
469 fprintf(stderr, "Non-linearized path...\n"); 473 fprintf(stderr, "Non-linearized path...\n");
470 doc = FPDF_LoadCustomDocument(&file_access, NULL); 474 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
471 } else { 475 } else {
472 fprintf(stderr, "Linearized path...\n"); 476 fprintf(stderr, "Linearized path...\n");
473 doc = FPDFAvail_GetDocument(pdf_avail, NULL); 477 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
474 } 478 }
475 479
476 (void) FPDF_GetDocPermissions(doc); 480 (void)FPDF_GetDocPermissions(doc);
477 (void) FPDFAvail_IsFormAvail(pdf_avail, &hints); 481 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints);
478 482
479 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); 483 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
480 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); 484 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
481 FPDF_SetFormFieldHighlightAlpha(form, 100); 485 FPDF_SetFormFieldHighlightAlpha(form, 100);
482 486
483 int first_page = FPDFAvail_GetFirstPageNum(doc); 487 int first_page = FPDFAvail_GetFirstPageNum(doc);
484 (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints); 488 (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
485 489
486 int page_count = FPDF_GetPageCount(doc); 490 int page_count = FPDF_GetPageCount(doc);
487 for (int i = 0; i < page_count; ++i) { 491 for (int i = 0; i < page_count; ++i) {
488 (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints); 492 (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
489 } 493 }
490 494
491 FORM_DoDocumentJSAction(form); 495 FORM_DoDocumentJSAction(form);
492 FORM_DoDocumentOpenAction(form); 496 FORM_DoDocumentOpenAction(form);
493 497
494 int rendered_pages = 0; 498 int rendered_pages = 0;
495 int bad_pages = 0; 499 int bad_pages = 0;
496 for (int i = 0; i < page_count; ++i) { 500 for (int i = 0; i < page_count; ++i) {
497 FPDF_PAGE page = FPDF_LoadPage(doc, i); 501 FPDF_PAGE page = FPDF_LoadPage(doc, i);
498 if (!page) { 502 if (!page) {
499 bad_pages ++; 503 ++bad_pages;
500 continue; 504 continue;
501 } 505 }
502 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); 506 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
503 FORM_OnAfterLoadPage(page, form); 507 FORM_OnAfterLoadPage(page, form);
504 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); 508 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
505 509
506 double scale = 1.0; 510 double scale = 1.0;
507 if (!options.scale_factor_as_string.empty()) { 511 if (!options.scale_factor_as_string.empty()) {
508 std::stringstream(options.scale_factor_as_string) >> scale; 512 std::stringstream(options.scale_factor_as_string) >> scale;
509 } 513 }
510 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); 514 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
511 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); 515 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
512 516
513 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); 517 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
514 if (!bitmap) { 518 if (!bitmap) {
515 fprintf(stderr, "Page was too large to be rendered.\n"); 519 fprintf(stderr, "Page was too large to be rendered.\n");
516 bad_pages++; 520 bad_pages++;
517 continue; 521 continue;
518 } 522 }
519 523
520 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); 524 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
521 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); 525 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
522 rendered_pages ++; 526 ++rendered_pages;
523 527
524 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); 528 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
525 int stride = FPDFBitmap_GetStride(bitmap); 529 int stride = FPDFBitmap_GetStride(bitmap);
526 const char* buffer = 530 const char* buffer =
527 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); 531 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
528 532
529 switch (options.output_format) { 533 switch (options.output_format) {
530 #ifdef _WIN32 534 #ifdef _WIN32
531 case OUTPUT_BMP: 535 case OUTPUT_BMP:
532 WriteBmp(name.c_str(), i, buffer, stride, width, height); 536 WriteBmp(name.c_str(), i, buffer, stride, width, height);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 RenderPdf(filename, file_contents, file_length, options); 640 RenderPdf(filename, file_contents, file_length, options);
637 free(file_contents); 641 free(file_contents);
638 } 642 }
639 643
640 FPDF_DestroyLibrary(); 644 FPDF_DestroyLibrary();
641 v8::V8::ShutdownPlatform(); 645 v8::V8::ShutdownPlatform();
642 delete platform; 646 delete platform;
643 647
644 return 0; 648 return 0;
645 } 649 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698