/src/gd_image_string_fuzzer.cc
Line | Count | Source |
1 | | // Copyright 2020 Google Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | ///////////////////////////////////////////////////////////////////////////// |
16 | | |
17 | | #include <fuzzer/FuzzedDataProvider.h> |
18 | | |
19 | | #include <cstddef> |
20 | | #include <cstdint> |
21 | | #include <cstdlib> |
22 | | #include <string> |
23 | | |
24 | | #include "gd.h" |
25 | | #include "gdfontg.h" |
26 | | #include "gdfontl.h" |
27 | | #include "gdfontmb.h" |
28 | | #include "gdfonts.h" |
29 | | #include "gdfontt.h" |
30 | | |
31 | 453 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
32 | 453 | FuzzedDataProvider stream(data, size); |
33 | 453 | const uint8_t slate_width = stream.ConsumeIntegral<uint8_t>(); |
34 | 453 | const uint8_t slate_height = stream.ConsumeIntegral<uint8_t>(); |
35 | 453 | gdImagePtr slate_image = gdImageCreateTrueColor(slate_width, slate_height); |
36 | 453 | if (slate_image == nullptr) { |
37 | 24 | return 0; |
38 | 24 | } |
39 | | |
40 | 429 | const int x_position = stream.ConsumeIntegral<int>(); |
41 | 429 | const int y_position = stream.ConsumeIntegral<int>(); |
42 | 429 | const int text_color = stream.ConsumeIntegral<int>(); |
43 | 429 | const gdFontPtr font_ptr = stream.PickValueInArray( |
44 | 429 | {gdFontGetGiant(), gdFontGetLarge(), gdFontGetMediumBold(), |
45 | 429 | gdFontGetSmall(), gdFontGetTiny()}); |
46 | 429 | const std::string text = stream.ConsumeRemainingBytesAsString(); |
47 | | |
48 | 429 | gdImageString(slate_image, font_ptr, x_position, y_position, |
49 | 429 | reinterpret_cast<uint8_t*>(const_cast<char*>(text.c_str())), |
50 | 429 | text_color); |
51 | 429 | gdImageDestroy(slate_image); |
52 | 429 | return 0; |
53 | 453 | } |