Coverage Report

Created: 2023-09-25 06:03

/src/igraph/fuzzing/read_ncol.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
775
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
26
27
775
    igraph_set_error_handler(igraph_error_handler_ignore);
28
775
    igraph_set_warning_handler(igraph_warning_handler_ignore);
29
30
    // Turn on attribute handling
31
775
    igraph_set_attribute_table(&igraph_cattribute_table);
32
33
    // Create input file
34
775
    char filename[256];
35
775
    sprintf(filename, "/tmp/libfuzzer.ncol");
36
775
    FILE *fp = fopen(filename, "wb");
37
775
    if (!fp) return 0;
38
775
    fwrite(data, size, 1, fp);
39
775
    fclose(fp);
40
41
    // Read input file
42
775
    FILE *ifile;
43
775
    ifile = fopen("/tmp/libfuzzer.ncol", "r");
44
775
    if (!ifile) {
45
0
        remove(filename);
46
0
        return 0;
47
0
    }
48
49
    // Do the fuzzing
50
775
    igraph_t g;
51
775
    if (igraph_read_graph_ncol(&g, ifile, NULL, 1, IGRAPH_ADD_WEIGHTS_IF_PRESENT, IGRAPH_UNDIRECTED) == IGRAPH_SUCCESS) {
52
        // Clean up
53
476
        igraph_destroy(&g);
54
476
    }
55
56
    // no need to call igraph_destroy() if igraph_read_graph_ncol() returns an
57
    // error code as we don't have a valid graph object in that case
58
59
775
    fclose(ifile);
60
775
    remove(filename);
61
62
775
    IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY);
63
64
775
    return 0;
65
775
}