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

Side by Side Diff: pdf/pdfium/fuzzers/pdfium_fuzzer.cc

Issue 2746363009: Avoid leaks in pdfium_fuzzer by using unique_ptr<> (Closed)
Patch Set: destruction order Created 3 years, 9 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 // This fuzzer is simplified & cleaned up pdfium/samples/pdfium_test.cc 5 // This fuzzer is simplified & cleaned up pdfium/samples/pdfium_test.cc
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <limits.h> 8 #include <limits.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 #include <stdint.h> 10 #include <stdint.h>
11 #include <stdio.h> 11 #include <stdio.h>
12 #include <stdlib.h> 12 #include <stdlib.h>
13 #include <string.h> 13 #include <string.h>
14 14
15 #ifdef _MSC_VER 15 #ifdef _MSC_VER
16 #include <Windows.h> 16 #include <Windows.h>
17 #else 17 #else
18 #include <unistd.h> 18 #include <unistd.h>
19 #endif 19 #endif
20 20
21 #include <list> 21 #include <list>
22 #include <memory>
22 #include <sstream> 23 #include <sstream>
23 #include <string> 24 #include <string>
24 #include <utility> 25 #include <utility>
25 #include <vector> 26 #include <vector>
26 27
28 #include "third_party/pdfium/public/cpp/fpdf_deleters.h"
27 #include "third_party/pdfium/public/fpdf_dataavail.h" 29 #include "third_party/pdfium/public/fpdf_dataavail.h"
28 #include "third_party/pdfium/public/fpdf_ext.h" 30 #include "third_party/pdfium/public/fpdf_ext.h"
29 #include "third_party/pdfium/public/fpdf_formfill.h" 31 #include "third_party/pdfium/public/fpdf_formfill.h"
30 #include "third_party/pdfium/public/fpdf_text.h" 32 #include "third_party/pdfium/public/fpdf_text.h"
31 #include "third_party/pdfium/public/fpdfview.h" 33 #include "third_party/pdfium/public/fpdfview.h"
32 #include "third_party/pdfium/testing/test_support.h" 34 #include "third_party/pdfium/testing/test_support.h"
33 35
34 #include "v8/include/v8.h" 36 #include "v8/include/v8.h"
35 37
36 static int ExampleAppAlert(IPDF_JSPLATFORM*, 38 static int ExampleAppAlert(IPDF_JSPLATFORM*,
(...skipping 10 matching lines...) Expand all
47 49
48 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { 50 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
49 return true; 51 return true;
50 } 52 }
51 53
52 static void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {} 54 static void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
53 55
54 static bool RenderPage(const FPDF_DOCUMENT& doc, 56 static bool RenderPage(const FPDF_DOCUMENT& doc,
55 const FPDF_FORMHANDLE& form, 57 const FPDF_FORMHANDLE& form,
56 const int page_index) { 58 const int page_index) {
57 FPDF_PAGE page = FPDF_LoadPage(doc, page_index); 59 std::unique_ptr<void, FPDFPageDeleter> page(FPDF_LoadPage(doc, page_index));
58 if (!page) 60 if (!page)
59 return false; 61 return false;
60 62
61 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); 63 std::unique_ptr<void, FPDFTextPageDeleter> text_page(
62 FORM_OnAfterLoadPage(page, form); 64 FPDFText_LoadPage(page.get()));
63 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); 65 FORM_OnAfterLoadPage(page.get(), form);
66 FORM_DoPageAAction(page.get(), form, FPDFPAGE_AACTION_OPEN);
64 67
65 const double scale = 1.0; 68 const double scale = 1.0;
66 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); 69 int width = static_cast<int>(FPDF_GetPageWidth(page.get()) * scale);
67 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); 70 int height = static_cast<int>(FPDF_GetPageHeight(page.get()) * scale);
68 71 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
69 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); 72 FPDFBitmap_Create(width, height, 0));
70 if (bitmap) { 73 if (bitmap) {
71 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); 74 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, 0xFFFFFFFF);
72 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); 75 FPDF_RenderPageBitmap(bitmap.get(), page.get(), 0, 0, width, height, 0, 0);
73 76 FPDF_FFLDraw(form, bitmap.get(), page.get(), 0, 0, width, height, 0, 0);
74 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
75
76 FPDFBitmap_Destroy(bitmap);
77 } 77 }
78 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); 78 FORM_DoPageAAction(page.get(), form, FPDFPAGE_AACTION_CLOSE);
79 FORM_OnBeforeClosePage(page, form); 79 FORM_OnBeforeClosePage(page.get(), form);
80 FPDFText_ClosePage(text_page);
81 FPDF_ClosePage(page);
82 return !!bitmap; 80 return !!bitmap;
83 } 81 }
84 82
85 static void RenderPdf(const char* pBuf, size_t len) { 83 static void RenderPdf(const char* pBuf, size_t len) {
86 IPDF_JSPLATFORM platform_callbacks; 84 IPDF_JSPLATFORM platform_callbacks;
87 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); 85 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
88 platform_callbacks.version = 3; 86 platform_callbacks.version = 3;
89 platform_callbacks.app_alert = ExampleAppAlert; 87 platform_callbacks.app_alert = ExampleAppAlert;
90 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; 88 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
91 89
(...skipping 12 matching lines...) Expand all
104 FX_FILEAVAIL file_avail; 102 FX_FILEAVAIL file_avail;
105 memset(&file_avail, '\0', sizeof(file_avail)); 103 memset(&file_avail, '\0', sizeof(file_avail));
106 file_avail.version = 1; 104 file_avail.version = 1;
107 file_avail.IsDataAvail = Is_Data_Avail; 105 file_avail.IsDataAvail = Is_Data_Avail;
108 106
109 FX_DOWNLOADHINTS hints; 107 FX_DOWNLOADHINTS hints;
110 memset(&hints, '\0', sizeof(hints)); 108 memset(&hints, '\0', sizeof(hints));
111 hints.version = 1; 109 hints.version = 1;
112 hints.AddSegment = Add_Segment; 110 hints.AddSegment = Add_Segment;
113 111
114 FPDF_DOCUMENT doc; 112 std::unique_ptr<void, FPDFAvailDeleter> pdf_avail(
113 FPDFAvail_Create(&file_avail, &file_access));
114
115 int nRet = PDF_DATA_NOTAVAIL; 115 int nRet = PDF_DATA_NOTAVAIL;
116 bool bIsLinearized = false; 116 bool bIsLinearized = false;
117 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); 117 std::unique_ptr<void, FPDFDocumentDeleter> doc;
118 if (FPDFAvail_IsLinearized(pdf_avail.get()) == PDF_LINEARIZED) {
119 doc.reset(FPDFAvail_GetDocument(pdf_avail.get(), nullptr));
120 if (doc) {
121 while (nRet == PDF_DATA_NOTAVAIL)
122 nRet = FPDFAvail_IsDocAvail(pdf_avail.get(), &hints);
118 123
119 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) { 124 if (nRet == PDF_DATA_ERROR)
120 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
121 if (doc) {
122 while (nRet == PDF_DATA_NOTAVAIL) {
123 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
124 }
125 if (nRet == PDF_DATA_ERROR) {
126 return; 125 return;
127 } 126
128 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints); 127 nRet = FPDFAvail_IsFormAvail(pdf_avail.get(), &hints);
129 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) { 128 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL)
130 return; 129 return;
131 } 130
132 bIsLinearized = true; 131 bIsLinearized = true;
133 } 132 }
134 } else { 133 } else {
135 doc = FPDF_LoadCustomDocument(&file_access, nullptr); 134 doc.reset(FPDF_LoadCustomDocument(&file_access, nullptr));
136 } 135 }
137 136
138 if (!doc) { 137 if (!doc)
139 FPDFAvail_Destroy(pdf_avail);
140 return; 138 return;
141 }
142 139
143 (void)FPDF_GetDocPermissions(doc); 140 (void)FPDF_GetDocPermissions(doc.get());
144 141
145 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); 142 std::unique_ptr<void, FPDFFormHandleDeleter> form(
146 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); 143 FPDFDOC_InitFormFillEnvironment(doc.get(), &form_callbacks));
147 FPDF_SetFormFieldHighlightAlpha(form, 100); 144 FPDF_SetFormFieldHighlightColor(form.get(), 0, 0xFFE4DD);
145 FPDF_SetFormFieldHighlightAlpha(form.get(), 100);
146 FORM_DoDocumentJSAction(form.get());
147 FORM_DoDocumentOpenAction(form.get());
148 148
149 FORM_DoDocumentJSAction(form); 149 int page_count = FPDF_GetPageCount(doc.get());
150 FORM_DoDocumentOpenAction(form);
151
152 int page_count = FPDF_GetPageCount(doc);
153
154 for (int i = 0; i < page_count; ++i) { 150 for (int i = 0; i < page_count; ++i) {
155 if (bIsLinearized) { 151 if (bIsLinearized) {
156 nRet = PDF_DATA_NOTAVAIL; 152 nRet = PDF_DATA_NOTAVAIL;
157 while (nRet == PDF_DATA_NOTAVAIL) { 153 while (nRet == PDF_DATA_NOTAVAIL)
158 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); 154 nRet = FPDFAvail_IsPageAvail(pdf_avail.get(), i, &hints);
159 } 155
160 if (nRet == PDF_DATA_ERROR) { 156 if (nRet == PDF_DATA_ERROR)
161 return; 157 return;
162 }
163 } 158 }
164 RenderPage(doc, form, i); 159 RenderPage(doc.get(), form.get(), i);
165 } 160 }
166 161 FORM_DoDocumentAAction(form.get(), FPDFDOC_AACTION_WC);
167 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
168 FPDFDOC_ExitFormFillEnvironment(form);
169 FPDF_CloseDocument(doc);
170 FPDFAvail_Destroy(pdf_avail);
171 } 162 }
172 163
173 std::string ProgramPath() { 164 std::string ProgramPath() {
174 #ifdef _MSC_VER 165 #ifdef _MSC_VER
175 wchar_t wpath[MAX_PATH]; 166 wchar_t wpath[MAX_PATH];
176 char path[MAX_PATH]; 167 char path[MAX_PATH];
177 DWORD res = GetModuleFileName(NULL, wpath, MAX_PATH); 168 DWORD res = GetModuleFileName(NULL, wpath, MAX_PATH);
178 assert(res != 0); 169 assert(res != 0);
179 wcstombs(path, wpath, MAX_PATH); 170 wcstombs(path, wpath, MAX_PATH);
180 return std::string(path, res); 171 return std::string(path, res);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 FPDF_LIBRARY_CONFIG config; 204 FPDF_LIBRARY_CONFIG config;
214 UNSUPPORT_INFO unsupport_info; 205 UNSUPPORT_INFO unsupport_info;
215 }; 206 };
216 207
217 static TestCase* testCase = new TestCase(); 208 static TestCase* testCase = new TestCase();
218 209
219 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 210 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
220 RenderPdf(reinterpret_cast<const char*>(data), size); 211 RenderPdf(reinterpret_cast<const char*>(data), size);
221 return 0; 212 return 0;
222 } 213 }
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