/src/avahi/fuzz/fuzz-consume-record.c
Line | Count | Source |
1 | | /*** |
2 | | This file is part of avahi. |
3 | | |
4 | | avahi is free software; you can redistribute it and/or modify it |
5 | | under the terms of the GNU Lesser General Public License as |
6 | | published by the Free Software Foundation; either version 2.1 of the |
7 | | License, or (at your option) any later version. |
8 | | |
9 | | avahi is distributed in the hope that it will be useful, but WITHOUT |
10 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General |
12 | | Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU Lesser General Public |
15 | | License along with avahi; if not, write to the Free Software |
16 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
17 | | USA. |
18 | | ***/ |
19 | | |
20 | | #include <stdint.h> |
21 | | #include <string.h> |
22 | | |
23 | | #include "avahi-common/malloc.h" |
24 | | #include "avahi-core/dns.h" |
25 | | #include "avahi-core/log.h" |
26 | | #include "avahi-core/rr-util.h" |
27 | | |
28 | | #ifdef HAVE_NALLOCFUZZ |
29 | | #include "nallocinc.c" |
30 | | #endif |
31 | | |
32 | 7.64k | void log_function(AvahiLogLevel level, const char *txt) {} |
33 | | |
34 | 2.88k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
35 | 2.88k | AvahiDnsPacket *p = NULL; |
36 | 2.88k | AvahiRecord *r = NULL, *rs = NULL, *c = NULL; |
37 | 2.88k | uint8_t rdata[AVAHI_DNS_RDATA_MAX]; |
38 | 2.88k | size_t rdata_size; |
39 | 2.88k | int ret; |
40 | | |
41 | 2.88k | avahi_set_log_function(log_function); |
42 | | |
43 | 2.88k | if (!(p = avahi_dns_packet_new(size + AVAHI_DNS_PACKET_EXTRA_SIZE))) |
44 | 0 | goto finish; |
45 | | |
46 | 2.88k | memcpy(AVAHI_DNS_PACKET_DATA(p), data, size); |
47 | 2.88k | p->size = size; |
48 | | |
49 | | #ifdef HAVE_NALLOCFUZZ |
50 | | nalloc_start(data, size); |
51 | | #endif |
52 | | |
53 | 2.88k | if (!(r = avahi_dns_packet_consume_record(p, NULL))) |
54 | 575 | goto finish; |
55 | | |
56 | 2.30k | ret = avahi_record_is_valid(r); |
57 | 2.30k | assert(ret); |
58 | | |
59 | 2.30k | avahi_record_is_goodbye(r); |
60 | | |
61 | 2.30k | avahi_record_is_link_local_address(r); |
62 | | |
63 | 2.30k | avahi_record_get_estimate_size(r); |
64 | | |
65 | 2.30k | avahi_free(avahi_record_to_string(r)); |
66 | | |
67 | 2.30k | rdata_size = avahi_rdata_serialize(r, rdata, sizeof(rdata)); |
68 | | #ifdef HAVE_NALLOCFUZZ |
69 | | if (rdata_size == (size_t) -1) |
70 | | goto finish; |
71 | | #endif |
72 | 2.30k | assert(rdata_size != (size_t) -1); |
73 | | |
74 | 2.30k | if (!(rs = avahi_record_new_full(r->key->name, r->key->clazz, r->key->type, r->ttl))) |
75 | 0 | goto finish; |
76 | | |
77 | 2.30k | if (avahi_rdata_parse(rs, rdata, rdata_size) < 0) |
78 | 0 | goto finish; |
79 | | |
80 | 2.30k | ret = avahi_record_is_valid(rs); |
81 | 2.30k | assert(ret); |
82 | | |
83 | 2.30k | ret = avahi_record_equal_no_ttl(r, rs); |
84 | 2.30k | assert(ret); |
85 | | |
86 | 2.30k | ret = avahi_record_lexicographical_compare(r, rs); |
87 | 2.30k | #ifndef HAVE_NALLOCFUZZ |
88 | 2.30k | assert(ret == 0); |
89 | 2.30k | #endif |
90 | | |
91 | 2.30k | if (!(c = avahi_record_copy(r))) |
92 | 0 | goto finish; |
93 | | |
94 | 2.30k | ret = avahi_record_equal_no_ttl(r, c); |
95 | 2.30k | assert(ret); |
96 | | |
97 | 2.30k | ret = avahi_record_lexicographical_compare(r, c); |
98 | 2.30k | #ifndef HAVE_NALLOCFUZZ |
99 | 2.30k | assert(ret == 0); |
100 | 2.30k | #endif |
101 | | |
102 | 2.30k | avahi_record_unref(c); |
103 | 2.30k | if (!(c = avahi_dns_packet_consume_record(p, NULL))) |
104 | 833 | goto finish; |
105 | | |
106 | 1.47k | avahi_record_equal_no_ttl(r, c); |
107 | 1.47k | avahi_record_lexicographical_compare(r, c); |
108 | | |
109 | 1.47k | avahi_dns_packet_free(p); |
110 | 1.47k | if (!(p = avahi_dns_packet_new(size + AVAHI_DNS_PACKET_EXTRA_SIZE))) |
111 | 0 | goto finish; |
112 | | |
113 | 1.47k | if (!avahi_dns_packet_append_record(p, r, 0, 0)) |
114 | 142 | goto finish; |
115 | | |
116 | 2.88k | finish: |
117 | 2.88k | if (c) |
118 | 1.47k | avahi_record_unref(c); |
119 | 2.88k | if (rs) |
120 | 2.30k | avahi_record_unref(rs); |
121 | 2.88k | if (r) |
122 | 2.30k | avahi_record_unref(r); |
123 | 2.88k | if (p) |
124 | 2.88k | avahi_dns_packet_free(p); |
125 | | |
126 | | #ifdef HAVE_NALLOCFUZZ |
127 | | nalloc_end(); |
128 | | #endif |
129 | | |
130 | 2.88k | return 0; |
131 | 1.47k | } |