Coverage Report

Created: 2023-09-25 06:05

/src/libidn/fuzz/libidn_stringprep_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 <https://www.gnu.org/licenses/>.
16
 */
17
18
#include <config.h>
19
20
#include <assert.h>   /* assert */
21
#include <stdint.h>   /* uint8_t, uint32_t */
22
#include <stdlib.h>   /* malloc, free */
23
#include <string.h>   /* memcpy */
24
25
#include "stringprep.h"
26
#include "pr29.h"
27
#include "tld.h"
28
#include "idn-free.h"
29
#include "fuzzer.h"
30
31
int
32
LLVMFuzzerTestOneInput (const uint8_t * data, size_t size)
33
2.64k
{
34
2.64k
  char *wdata;
35
2.64k
  char *label;
36
2.64k
  char *utf8_seq;
37
2.64k
  char *out;
38
2.64k
  uint32_t cp;
39
2.64k
  size_t errpos;
40
41
2.64k
  if (size > 2048)
42
11
    return 0;
43
44
2.63k
  wdata = (char *) malloc (size + 1);
45
2.63k
  label = (char *) malloc (size + 1);
46
2.63k
  utf8_seq = (char *) malloc (6);
47
2.63k
  assert (wdata != NULL);
48
0
  assert (label != NULL);
49
0
  assert (utf8_seq != NULL);
50
51
  /* 0 terminate */
52
0
  memcpy (label, data, size);
53
2.63k
  label[size] = 0;
54
55
2.63k
  stringprep_check_version (label);
56
57
2.63k
  if (stringprep_profile
58
2.63k
      (label, &out, "Nodeprep",
59
2.63k
       (Stringprep_profile_flags) 0) == STRINGPREP_OK)
60
1.32k
    idn_free (out);
61
62
2.63k
  pr29_8z (label);    /* internally calls stringprep_utf8_to_ucs4() */
63
64
2.63k
#ifdef WITH_TLD
65
2.63k
  if (tld_get_z (label, &out) == TLD_SUCCESS)  /* internally calls tld_get_4() */
66
261
    idn_free (out);
67
2.63k
  const Tld_table *tld = tld_default_table ("fr", NULL);
68
2.63k
  tld_check_8z (label, &errpos, NULL);
69
2.63k
  tld_check_lz (label, &errpos, NULL);
70
2.63k
#endif
71
72
2.63k
  out = stringprep_utf8_nfkc_normalize ((char *) data, size);
73
2.63k
  idn_free (out);
74
75
2.63k
  cp = stringprep_utf8_to_unichar (label);
76
2.63k
  stringprep_unichar_to_utf8 (cp, utf8_seq);
77
78
2.63k
  memcpy (wdata, data, size);
79
2.63k
  wdata[size] = 0;
80
2.63k
  stringprep (wdata, size, (Stringprep_profile_flags) 0, stringprep_nameprep);
81
2.63k
  memcpy (wdata, data, size);
82
2.63k
  wdata[size] = 0;
83
2.63k
  stringprep (wdata, size, STRINGPREP_NO_UNASSIGNED, stringprep_nameprep);
84
85
2.63k
  if ((size & 3) == 0)
86
1.24k
    {
87
1.24k
      uint32_t *u32 = (uint32_t *) malloc (size + 4);
88
89
1.24k
      assert (u32 != NULL);
90
91
0
      memcpy (u32, data, size);
92
1.24k
      u32[size / 4] = 0;
93
1.24k
      stringprep_4zi (u32, size / 4, (Stringprep_profile_flags) 0,
94
1.24k
          stringprep_xmpp_nodeprep);
95
96
1.24k
      memcpy (u32, data, size);
97
1.24k
      u32[size / 4] = 0;
98
1.24k
#ifdef WITH_TLD
99
1.24k
      if (tld_get_4z (u32, &out) == TLD_SUCCESS)  /* internally calls tld_get_4() */
100
85
  idn_free (out);
101
102
1.24k
      tld_check_4tz (u32, &errpos, tld);
103
1.24k
      tld_check_4z (u32, &errpos, NULL);
104
1.24k
#endif
105
106
1.24k
      free (u32);
107
1.24k
    }
108
109
0
  free (utf8_seq);
110
2.63k
  free (label);
111
2.63k
  free (wdata);
112
113
2.63k
  return 0;
114
2.64k
}