Coverage Report

Created: 2025-07-01 06:55

/src/libyaml_reformatter_fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2020 Google LLC
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
#include "yaml.h"
16
#include "yaml_write_handler.h"
17
#include <assert.h>
18
#include <stdbool.h>
19
#include <stdint.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
24
#ifdef NDEBUG
25
#undef NDEBUG
26
#endif
27
28
10.8k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
29
10.8k
  if (size < 2)
30
1
    return 0;
31
32
10.8k
  bool done = false;
33
10.8k
  bool is_canonical = data[0] & 1;
34
10.8k
  bool is_unicode = data[1] & 1;
35
10.8k
  data += 2;
36
10.8k
  size -= 2;
37
38
10.8k
  yaml_parser_t parser;
39
10.8k
  yaml_emitter_t emitter;
40
10.8k
  yaml_event_t event;
41
42
  /* Initialize the parser and emitter objects. */
43
44
10.8k
  if (!yaml_parser_initialize(&parser))
45
0
    return 0;
46
47
10.8k
  if (!yaml_emitter_initialize(&emitter))
48
0
    goto cleanup_parser;
49
50
  /* Set the parser parameters. */
51
52
10.8k
  yaml_parser_set_input_string(&parser, data, size);
53
54
  /* Set the emitter parameters. */
55
10.8k
  yaml_output_buffer_t out = {/*buf=*/NULL, /*size=*/0, /*capacity=*/1000};
56
10.8k
  yaml_emitter_set_output(&emitter, yaml_write_handler, &out);
57
58
10.8k
  yaml_emitter_set_canonical(&emitter, is_canonical);
59
10.8k
  yaml_emitter_set_unicode(&emitter, is_unicode);
60
61
  /* The main loop. */
62
63
8.47M
  while (!done) {
64
    /* Get the next event. */
65
66
8.47M
    if (!yaml_parser_parse(&parser, &event))
67
5.19k
      break;
68
69
    /* Check if this is the stream end. */
70
71
8.46M
    done = (event.type == YAML_STREAM_END_EVENT);
72
73
    /* Emit the event. */
74
75
8.46M
    if (!yaml_emitter_emit(&emitter, &event))
76
4.48k
      break;
77
8.46M
  }
78
79
10.8k
  free(out.buf);
80
10.8k
  yaml_emitter_delete(&emitter);
81
82
10.8k
cleanup_parser:
83
84
10.8k
  yaml_parser_delete(&parser);
85
10.8k
  return 0;
86
10.8k
}