Coverage Report

Created: 2025-08-03 06:03

/src/pjsip/tests/fuzz/fuzz-dns.c
Line
Count
Source
1
/* 
2
 * Copyright (C) 2023 Teluu Inc. (http://www.teluu.com)
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 2 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, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
17
 */
18
#include <stdio.h>
19
#include <stdint.h>
20
#include <stdlib.h>
21
22
#include <pjlib.h>
23
24
#include <pjlib-util/dns.h>
25
26
29.6k
#define kMinInputLength 10
27
14.7k
#define kMaxInputLength 5120
28
29
pj_pool_factory *mem;
30
31
int dns_parser(char *data, size_t size)
32
478
{
33
34
478
    int ret = 0;
35
478
  pj_pool_t *pool;
36
478
  pj_status_t status;
37
478
    pj_dns_parsed_packet *dns;
38
39
478
    pool = pj_pool_create(mem, "dns_test", 4000, 4000, NULL);
40
41
478
    status = pj_dns_parse_packet(pool, data, size, &dns);
42
478
    if (status != PJ_SUCCESS)
43
354
        ret = 1;
44
45
478
    pj_pool_release(pool);
46
47
478
    return ret;
48
478
}
49
50
extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
51
14.8k
{
52
14.8k
    int ret = 0;
53
14.8k
    char *data;
54
14.8k
    pj_caching_pool caching_pool;
55
56
14.8k
    if (Size < kMinInputLength || Size > kMaxInputLength) {
57
344
        return 1;
58
344
    }
59
60
    /* Add null termination for the data */
61
14.4k
    data = (char *)calloc((Size+1), sizeof(char));
62
14.4k
    memcpy((void *)data, (void *)Data, Size);
63
64
    /* Init */
65
14.4k
    pj_init();
66
14.4k
    pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0);
67
14.4k
    pj_log_set_level(0);
68
69
14.4k
    mem = &caching_pool.factory;
70
71
    /* Fuzz */
72
14.4k
    ret = dns_parser(data, Size);
73
74
14.4k
    free(data);
75
14.4k
    pj_caching_pool_destroy(&caching_pool);
76
77
14.4k
    return ret;
78
14.8k
}