Coverage Report

Created: 2023-06-07 07:10

/src/libidn2/fuzz/libidn2_to_ascii_8z_fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright(c) 2017 Tim Ruehsen
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include <config.h>
19
20
#include <assert.h>   /* assert */
21
#include <stdlib.h>   /* malloc, free */
22
#include <string.h>   /* memcpy */
23
24
#include "idn2.h"
25
#include "fuzzer.h"
26
27
static int flags[] = {
28
  0,
29
  IDN2_NFC_INPUT,
30
  IDN2_TRANSITIONAL,
31
  IDN2_NONTRANSITIONAL,
32
  IDN2_TRANSITIONAL | IDN2_USE_STD3_ASCII_RULES,
33
  IDN2_NONTRANSITIONAL | IDN2_USE_STD3_ASCII_RULES
34
};
35
36
int
37
LLVMFuzzerTestOneInput (const uint8_t * data, size_t size)
38
2.90k
{
39
2.90k
  char *domain;
40
2.90k
  char *out;
41
42
2.90k
  if (size > 1024)
43
12
    return 0;
44
45
2.89k
  domain = (char *) malloc (size + 1);
46
2.89k
  assert (domain != NULL);
47
48
  /* 0 terminate */
49
0
  memcpy (domain, data, size);
50
2.89k
  domain[size] = 0;
51
52
2.89k
  if (size == 0)
53
0
    {
54
    /*** test NULL input/output combinations ***/
55
56
0
      if (idn2_to_ascii_8z (NULL, &out, 0) == IDN2_OK)
57
0
  idn2_free (out);
58
0
      idn2_to_ascii_8z (NULL, NULL, 0);
59
0
      idn2_to_ascii_8z (domain, NULL, 0);
60
61
0
      if (idn2_to_ascii_lz (NULL, &out, 0) == IDN2_OK)
62
0
  idn2_free (out);
63
0
      idn2_to_ascii_lz (NULL, NULL, 0);
64
0
      idn2_to_ascii_lz (domain, NULL, 0);
65
66
0
      {
67
0
  uint32_t in32[1] = { 0 };
68
0
  char out8[1];
69
0
  idn2_to_ascii_4i (NULL, 0, out8, 0);
70
0
  idn2_to_ascii_4i (NULL, 0, NULL, 0);
71
0
  idn2_to_ascii_4i (in32, 0, NULL, 0);
72
0
      }
73
74
0
      {
75
0
  uint32_t in32[1] = { 0 };
76
0
  if (idn2_to_ascii_4i2 (NULL, 0, &out, 0) == IDN2_OK)
77
0
    idn2_free (out);
78
0
  idn2_to_ascii_4i2 (NULL, 0, NULL, 0);
79
0
  idn2_to_ascii_4i2 (in32, 0, NULL, 0);
80
81
0
  if (idn2_to_ascii_4z (NULL, &out, 0) == IDN2_OK)
82
0
    idn2_free (out);
83
0
  idn2_to_ascii_4z (NULL, NULL, 0);
84
0
  idn2_to_ascii_4z (in32, NULL, 0);
85
0
      }
86
0
    }
87
88
20.2k
  for (unsigned it = 0; it < sizeof (flags) / sizeof (flags[0]); it++)
89
17.3k
    {
90
17.3k
      if (idn2_to_ascii_8z (domain, &out, flags[it]) == IDN2_OK)
91
6.19k
  idn2_free (out);
92
17.3k
      if (idn2_to_ascii_lz (domain, &out, flags[it]) == IDN2_OK)
93
2.41k
  idn2_free (out);
94
17.3k
    }
95
96
2.89k
  if ((size & 3) == 0)
97
1.02k
    {
98
1.02k
      uint32_t *u32 = (uint32_t *) malloc (size + 4);
99
1.02k
      char *out2 = (char *) malloc (64);
100
101
1.02k
      assert (u32 != NULL);
102
0
      assert (out2 != NULL);
103
104
0
      idn2_to_ascii_4i ((uint32_t *) data, size / 4, out2, 0);
105
106
7.14k
      for (unsigned it = 0; it < sizeof (flags) / sizeof (flags[0]); it++)
107
6.12k
  if (idn2_to_ascii_4i2 ((uint32_t *) data, size / 4, &out, flags[it])
108
6.12k
      == IDN2_OK)
109
996
    idn2_free (out);
110
111
1.02k
      memcpy (u32, data, size);
112
1.02k
      u32[size / 4] = 0;
113
114
7.14k
      for (unsigned it = 0; it < sizeof (flags) / sizeof (flags[0]); it++)
115
6.12k
  if (idn2_to_ascii_4z (u32, &out, flags[it]) == IDN2_OK)
116
1.01k
    idn2_free (out);
117
118
1.02k
      free (out2);
119
1.02k
      free (u32);
120
1.02k
    }
121
122
0
  free (domain);
123
2.89k
  return 0;
124
2.90k
}