Coverage Report

Created: 2023-06-07 06:06

/src/igraph/fuzzing/read_graphdb_fuzzer.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
   IGraph library.
3
   Copyright (C) 2022  The igraph development team
4
5
   This program is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 2 of the License, or
8
   (at your option) any later version.
9
10
   This program is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU General Public License for more details.
14
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, write to the Free Software
17
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
   02110-1301 USA
19
*/
20
21
#include "igraph.h"
22
#include <cstdio>
23
24
extern "C"
25
30.3k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
26
27
30.3k
    igraph_set_error_handler(igraph_error_handler_ignore);
28
30.3k
    igraph_set_warning_handler(igraph_warning_handler_ignore);
29
30
    // Create input file
31
30.3k
    char filename[256];
32
30.3k
    sprintf(filename, "/tmp/libfuzzer.graphdb");
33
30.3k
    FILE *fp = fopen(filename, "wb");
34
30.3k
    if (!fp) return 0;
35
30.3k
    fwrite(data, size, 1, fp);
36
30.3k
    fclose(fp);
37
38
    // Read input file
39
30.3k
    FILE *ifile;
40
30.3k
    ifile = fopen("/tmp/libfuzzer.graphdb", "rb");
41
30.3k
    if(ifile == 0){
42
0
        remove(filename);
43
0
        return 0;
44
0
    }
45
46
    // Do the fuzzing
47
30.3k
    igraph_t g;
48
30.3k
    if (igraph_read_graph_graphdb(&g, ifile, IGRAPH_DIRECTED) == IGRAPH_SUCCESS) {
49
        // Clean up
50
5.50k
        igraph_destroy(&g);
51
5.50k
    }
52
53
    // no need to call igraph_destroy() if igraph_read_graph_edgelist() returns an
54
    // error code as we don't have a valid graph object in that case
55
56
30.3k
    fclose(ifile);
57
30.3k
    remove(filename);
58
59
30.3k
    IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY);
60
61
30.3k
    return 0;
62
30.3k
}