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

Side by Side Diff: fuzz/FuzzGradients.cpp

Issue 2446643003: Fix memory leak in FuzzGradients (Closed)
Patch Set: More cleanup Created 4 years, 1 month 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 | « fuzz/Fuzz.h ('k') | 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 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "Fuzz.h" 8 #include "Fuzz.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkGradientShader.h" 10 #include "SkGradientShader.h"
(...skipping 14 matching lines...) Expand all
25 !fuzz->next(&transY) || 25 !fuzz->next(&transY) ||
26 !fuzz->next(&persp0) || 26 !fuzz->next(&persp0) ||
27 !fuzz->next(&persp1) || 27 !fuzz->next(&persp1) ||
28 !fuzz->next(&persp2)) { 28 !fuzz->next(&persp2)) {
29 return false; 29 return false;
30 } 30 }
31 m->setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, pers p2); 31 m->setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, pers p2);
32 return true; 32 return true;
33 } 33 }
34 34
35 bool initGradientParams(Fuzz* fuzz, uint32_t* count, SkColor** colors, SkScalar* * pos, 35 bool initGradientParams(Fuzz* fuzz, std::vector<SkColor>* colors,
36 SkShader::TileMode* mode) { 36 std::vector<SkScalar>* pos, SkShader::TileMode* mode) {
37 if (fuzz->remaining() < sizeof(uint32_t)) { 37 if (fuzz->remaining() < sizeof(uint32_t)) {
38 return false; 38 return false;
39 } 39 }
40 uint32_t t_count; 40 uint32_t count = fuzz->nextRangeU(0, MAX_COUNT);
41 SkColor* t_colors;
42 SkScalar* t_pos;
43 41
44 t_count = fuzz->nextRangeU(0, MAX_COUNT); 42 if (fuzz->remaining() < sizeof(uint8_t)) {
45 if (t_count == 1) {
46 t_count = 2;
47 }
48
49 if (fuzz->remaining() < (1 + t_count * (sizeof(SkColor) + sizeof(SkScalar))) ) {
50 return false; 43 return false;
51 } 44 }
52 t_colors = new SkColor[t_count]; 45 *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3));
mtklein_C 2016/10/24 20:52:23 This call consumes 4 bytes...
kjlubick 2016/10/25 12:20:40 Whoops.
53 t_pos = new SkScalar[t_count]; 46
54 for (uint32_t i = 0; i < t_count; i++) { 47 colors->clear();
55 fuzz->next(&t_colors[i]); 48 pos ->clear();
56 fuzz->next(&t_pos[i]); 49 for (uint32_t i = 0; i < count; i++) {
50 SkColor c;
51 SkScalar s;
52 if (!fuzz->next(&c) || !fuzz->next(&s)) {
53 return false;
54 }
55 colors->push_back(c);
56 pos ->push_back(s);
57 } 57 }
58 58 if (count) {
59 if (t_count == 0) { 59 std::sort(pos->begin(), pos->end());
60 *count = 0; 60 (*pos)[count - 1] = 1;
mtklein_C 2016/10/24 20:52:23 I was wondering about this. You want pos=0 for co
kjlubick 2016/10/25 12:20:40 Done.
61 *colors = NULL; 61 (*pos)[0] = 0;
62 *pos = NULL;
63 } else {
64 std::sort(t_pos, t_pos + t_count);
65 t_pos[0] = 0;
66 t_pos[t_count - 1] = 1;
67 *count = t_count;
68 *colors = t_colors;
69 *pos = t_pos;
70 } 62 }
71
72 *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3));
73 return true; 63 return true;
74 } 64 }
75 65
76 void fuzzLinearGradient(Fuzz* fuzz) { 66 void fuzzLinearGradient(Fuzz* fuzz) {
77 SkScalar a, b, c, d; 67 SkScalar a, b, c, d;
78 bool useLocalMatrix, useGlobalMatrix; 68 bool useLocalMatrix, useGlobalMatrix;
79 if (!fuzz->next(&a) || 69 if (!fuzz->next(&a) ||
80 !fuzz->next(&b) || 70 !fuzz->next(&b) ||
81 !fuzz->next(&c) || 71 !fuzz->next(&c) ||
82 !fuzz->next(&d) || 72 !fuzz->next(&d) ||
83 !fuzz->next(&useLocalMatrix) || 73 !fuzz->next(&useLocalMatrix) ||
84 !fuzz->next(&useGlobalMatrix)) { 74 !fuzz->next(&useGlobalMatrix)) {
85 return; 75 return;
86 } 76 }
87 SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)}; 77 SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)};
88 78
89 uint32_t count; 79 std::vector<SkColor> colors;
90 SkColor* colors; 80 std::vector<SkScalar> pos;
91 SkScalar* pos;
92 SkShader::TileMode mode; 81 SkShader::TileMode mode;
93 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { 82 if (!initGradientParams(fuzz, &colors, &pos, &mode)) {
94 return; 83 return;
95 } 84 }
96 85
97 SkPaint p; 86 SkPaint p;
98 uint32_t flags; 87 uint32_t flags;
99 if (!fuzz->next(&flags)) { 88 if (!fuzz->next(&flags)) {
100 return; 89 return;
101 } 90 }
102 91
103 SkTLazy<SkMatrix> localMatrix; 92 SkTLazy<SkMatrix> localMatrix;
104 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { 93 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) {
105 return; 94 return;
106 } 95 }
107 p.setShader(SkGradientShader::MakeLinear(pts, colors, pos, count, mode, 96 p.setShader(SkGradientShader::MakeLinear(pts, colors.data(), pos.data(),
108 flags, localMatrix.getMaybeNull())); 97 colors.size(), mode, flags, localMatrix.getMaybeNull()));
109 98
110 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); 99 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
111 if (useGlobalMatrix) { 100 if (useGlobalMatrix) {
112 SkMatrix gm; 101 SkMatrix gm;
113 if (!makeMatrix(fuzz, &gm)) { 102 if (!makeMatrix(fuzz, &gm)) {
114 return; 103 return;
115 } 104 }
116 SkCanvas* c = surface->getCanvas(); 105 SkCanvas* c = surface->getCanvas();
117 c->setMatrix(gm); 106 c->setMatrix(gm);
118 c->drawPaint(p); 107 c->drawPaint(p);
119 } else { 108 } else {
120 surface->getCanvas()->drawPaint(p); 109 surface->getCanvas()->drawPaint(p);
121 } 110 }
122 } 111 }
123 112
124 void fuzzRadialGradient(Fuzz* fuzz) { 113 void fuzzRadialGradient(Fuzz* fuzz) {
125 SkScalar a, b, radius; 114 SkScalar a, b, radius;
126 bool useLocalMatrix, useGlobalMatrix; 115 bool useLocalMatrix, useGlobalMatrix;
127 if (!fuzz->next(&a) || 116 if (!fuzz->next(&a) ||
128 !fuzz->next(&b) || 117 !fuzz->next(&b) ||
129 !fuzz->next(&radius) || 118 !fuzz->next(&radius) ||
130 !fuzz->next(&useLocalMatrix) || 119 !fuzz->next(&useLocalMatrix) ||
131 !fuzz->next(&useGlobalMatrix)) { 120 !fuzz->next(&useGlobalMatrix)) {
132 return; 121 return;
133 } 122 }
134 SkPoint center = SkPoint::Make(a,b); 123 SkPoint center = SkPoint::Make(a,b);
135 124
136 uint32_t count; 125 std::vector<SkColor> colors;
137 SkColor* colors; 126 std::vector<SkScalar> pos;
138 SkScalar* pos;
139 SkShader::TileMode mode; 127 SkShader::TileMode mode;
140 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { 128 if (!initGradientParams(fuzz, &colors, &pos, &mode)) {
141 return; 129 return;
142 } 130 }
143 131
144 SkPaint p; 132 SkPaint p;
145 uint32_t flags; 133 uint32_t flags;
146 if (!fuzz->next(&flags)) { 134 if (!fuzz->next(&flags)) {
147 return; 135 return;
148 } 136 }
149 137
150 SkTLazy<SkMatrix> localMatrix; 138 SkTLazy<SkMatrix> localMatrix;
151 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { 139 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) {
152 return; 140 return;
153 } 141 }
154 p.setShader(SkGradientShader::MakeRadial(center, radius, colors, pos, 142 p.setShader(SkGradientShader::MakeRadial(center, radius, colors.data(),
155 count, mode, flags, localMatrix.getMaybeNull())); 143 pos.data(), colors.size(), mode, flags, localMatrix.getMaybeNull())) ;
156 144
157 145
158 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); 146 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
159 if (useGlobalMatrix) { 147 if (useGlobalMatrix) {
160 SkMatrix gm; 148 SkMatrix gm;
161 if (!makeMatrix(fuzz, &gm)) { 149 if (!makeMatrix(fuzz, &gm)) {
162 return; 150 return;
163 } 151 }
164 SkCanvas* c = surface->getCanvas(); 152 SkCanvas* c = surface->getCanvas();
165 c->setMatrix(gm); 153 c->setMatrix(gm);
(...skipping 12 matching lines...) Expand all
178 !fuzz->next(&c) || 166 !fuzz->next(&c) ||
179 !fuzz->next(&d) || 167 !fuzz->next(&d) ||
180 !fuzz->next(&endRadius) || 168 !fuzz->next(&endRadius) ||
181 !fuzz->next(&useLocalMatrix) || 169 !fuzz->next(&useLocalMatrix) ||
182 !fuzz->next(&useGlobalMatrix)) { 170 !fuzz->next(&useGlobalMatrix)) {
183 return; 171 return;
184 } 172 }
185 SkPoint start = SkPoint::Make(a, b); 173 SkPoint start = SkPoint::Make(a, b);
186 SkPoint end = SkPoint::Make(c, d); 174 SkPoint end = SkPoint::Make(c, d);
187 175
188 uint32_t count; 176 std::vector<SkColor> colors;
189 SkColor* colors; 177 std::vector<SkScalar> pos;
190 SkScalar* pos;
191 SkShader::TileMode mode; 178 SkShader::TileMode mode;
192 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { 179 if (!initGradientParams(fuzz, &colors, &pos, &mode)) {
193 return; 180 return;
194 } 181 }
195 182
196 SkPaint p; 183 SkPaint p;
197 uint32_t flags; 184 uint32_t flags;
198 if (!fuzz->next(&flags)) { 185 if (!fuzz->next(&flags)) {
199 return; 186 return;
200 } 187 }
201 188
202 SkTLazy<SkMatrix> localMatrix; 189 SkTLazy<SkMatrix> localMatrix;
203 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { 190 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) {
204 return; 191 return;
205 } 192 }
206 p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius, en d, 193 p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius,
207 endRadius, colors, pos, count, mode, flags, localMatrix.getMaybeNull ())); 194 end, endRadius, colors.data(), pos.data(), colors.size(), mode,
195 flags, localMatrix.getMaybeNull()));
208 196
209 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); 197 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
210 if (useGlobalMatrix) { 198 if (useGlobalMatrix) {
211 SkMatrix gm; 199 SkMatrix gm;
212 if (!makeMatrix(fuzz, &gm)) { 200 if (!makeMatrix(fuzz, &gm)) {
213 return; 201 return;
214 } 202 }
215 SkCanvas* c = surface->getCanvas(); 203 SkCanvas* c = surface->getCanvas();
216 c->setMatrix(gm); 204 c->setMatrix(gm);
217 c->drawPaint(p); 205 c->drawPaint(p);
218 } else { 206 } else {
219 surface->getCanvas()->drawPaint(p); 207 surface->getCanvas()->drawPaint(p);
220 } 208 }
221 } 209 }
222 210
223 void fuzzSweepGradient(Fuzz* fuzz) { 211 void fuzzSweepGradient(Fuzz* fuzz) {
224 SkScalar cx, cy; 212 SkScalar cx, cy;
225 bool useLocalMatrix, useGlobalMatrix; 213 bool useLocalMatrix, useGlobalMatrix;
226 if (!fuzz->next(&cx) || 214 if (!fuzz->next(&cx) ||
227 !fuzz->next(&cy) || 215 !fuzz->next(&cy) ||
228 !fuzz->next(&useLocalMatrix) || 216 !fuzz->next(&useLocalMatrix) ||
229 !fuzz->next(&useGlobalMatrix)) { 217 !fuzz->next(&useGlobalMatrix)) {
230 return; 218 return;
231 } 219 }
232 220
233 uint32_t count; 221 std::vector<SkColor> colors;
234 SkColor* colors; 222 std::vector<SkScalar> pos;
235 SkScalar* pos;
236 SkShader::TileMode mode; 223 SkShader::TileMode mode;
237 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { 224 if (!initGradientParams(fuzz, &colors, &pos, &mode)) {
238 return; 225 return;
239 } 226 }
240 227
241 SkPaint p; 228 SkPaint p;
242 if (useLocalMatrix) { 229 if (useLocalMatrix) {
243 SkMatrix m; 230 SkMatrix m;
244 if (!makeMatrix(fuzz, &m)) { 231 if (!makeMatrix(fuzz, &m)) {
245 return; 232 return;
246 } 233 }
247 uint32_t flags; 234 uint32_t flags;
248 if (!fuzz->next(&flags)) { 235 if (!fuzz->next(&flags)) {
249 return; 236 return;
250 } 237 }
251 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors, pos, count, flags, &m)); 238 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(),
239 pos.data(), colors.size(), flags, &m));
252 } else { 240 } else {
253 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors, pos, count)) ; 241 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(),
242 pos.data(), colors.size()));
254 } 243 }
255 244
256 245
257 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); 246 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
258 if (useGlobalMatrix) { 247 if (useGlobalMatrix) {
259 SkMatrix gm; 248 SkMatrix gm;
260 if (!makeMatrix(fuzz, &gm)) { 249 if (!makeMatrix(fuzz, &gm)) {
261 return; 250 return;
262 } 251 }
263 SkCanvas* c = surface->getCanvas(); 252 SkCanvas* c = surface->getCanvas();
(...skipping 21 matching lines...) Expand all
285 return; 274 return;
286 case 2: 275 case 2:
287 SkDebugf("TwoPointConicalGradient\n"); 276 SkDebugf("TwoPointConicalGradient\n");
288 fuzzTwoPointConicalGradient(fuzz); 277 fuzzTwoPointConicalGradient(fuzz);
289 return; 278 return;
290 } 279 }
291 SkDebugf("SweepGradient\n"); 280 SkDebugf("SweepGradient\n");
292 fuzzSweepGradient(fuzz); 281 fuzzSweepGradient(fuzz);
293 return; 282 return;
294 } 283 }
OLDNEW
« no previous file with comments | « fuzz/Fuzz.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698