Coverage Report

Created: 2026-04-12 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/igraph/fuzzing/centrality.cpp
Line
Count
Source
1
/*
2
   igraph library.
3
   Copyright (C) 2024  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 <cstdlib>
23
24
const igraph_real_t eps = 1e-10;
25
26
9.66k
bool vector_isininterval_eps(const igraph_vector_t *v, igraph_real_t lo, igraph_real_t hi) {
27
9.66k
    const igraph_int_t n = igraph_vector_size(v);
28
29
437k
    for (igraph_int_t i=0; i < n; i++) {
30
427k
        igraph_real_t x = VECTOR(*v)[i];
31
427k
        if (igraph_cmp_epsilon(lo, x, eps) > 0 || igraph_cmp_epsilon(x, hi, eps) > 0) {
32
0
            return false;
33
0
        }
34
427k
    }
35
36
9.66k
    return true;
37
9.66k
}
38
39
807
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
40
807
    igraph_t graph;
41
807
    igraph_vector_int_t edges;
42
43
807
    igraph_set_warning_handler(igraph_warning_handler_ignore);
44
45
807
    if (Size % 2 == 0 || Size > 512+1 || Size < 1) {
46
13
        return 0;
47
13
    }
48
49
794
    igraph_vector_int_init(&edges, Size-1);
50
85.2k
    for (size_t i=0; i < Size-1; ++i) {
51
84.4k
        VECTOR(edges)[i] = Data[i+1];
52
84.4k
    }
53
54
794
    igraph_rng_seed(igraph_rng_default(), 42);
55
56
794
    if (igraph_create(&graph, &edges, Data[0], IGRAPH_DIRECTED) == IGRAPH_SUCCESS) {
57
794
        const igraph_int_t vcount = igraph_vcount(&graph);
58
794
        igraph_vector_t v, v2;
59
794
        igraph_vector_int_t iv;
60
794
        igraph_bool_t b;
61
794
        igraph_real_t r;
62
63
        /* Limit graph size for the sake of performance. */
64
794
        if (vcount <= 64) {
65
794
            igraph_vector_init(&v, 0);
66
794
            igraph_vector_init(&v2, 0);
67
794
            igraph_vector_int_init(&iv, 0);
68
69
794
            igraph_betweenness(&graph, NULL, &v2, igraph_vss_all(), IGRAPH_UNDIRECTED, false);
70
794
            IGRAPH_ASSERT(vector_isininterval_eps(&v2, 0, vcount == 0 ? 0 : (vcount-1)*(vcount-2)/2));
71
72
794
            igraph_betweenness_cutoff(&graph, NULL, &v, igraph_vss_all(), IGRAPH_UNDIRECTED, false, 4);
73
794
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount == 0 ? 0 : (vcount-1)*(vcount-2)/2));
74
794
            IGRAPH_ASSERT(igraph_vector_all_le(&v, &v2));
75
76
794
            igraph_betweenness(&graph, NULL, &v2, igraph_vss_all(), IGRAPH_DIRECTED, false);
77
794
            IGRAPH_ASSERT(vector_isininterval_eps(&v2, 0, vcount == 0 ? 0 : (vcount-1)*(vcount-2)));
78
79
794
            igraph_betweenness_cutoff(&graph, NULL, &v, igraph_vss_all(), IGRAPH_DIRECTED, false, 5);
80
794
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount == 0 ? 0 : (vcount-1)*(vcount-2)));
81
794
            IGRAPH_ASSERT(igraph_vector_all_le(&v, &v2));
82
83
794
            igraph_edge_betweenness(&graph, NULL, &v2, igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_DIRECTED, false);
84
794
            IGRAPH_ASSERT(vector_isininterval_eps(&v2, 0, vcount*(vcount-1)/2));
85
86
794
            igraph_edge_betweenness_cutoff(&graph, NULL, &v, igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_DIRECTED,
87
794
                                           false, 4);
88
794
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount*(vcount-1)/2));
89
794
            IGRAPH_ASSERT(igraph_vector_all_le(&v, &v2));
90
91
794
            igraph_edge_betweenness(&graph, NULL, &v2, igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_UNDIRECTED, false);
92
794
            IGRAPH_ASSERT(vector_isininterval_eps(&v2, 0, vcount*(vcount-1)));
93
94
794
            igraph_edge_betweenness_cutoff(&graph, NULL, &v, igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_UNDIRECTED,
95
794
                                           false, 3);
96
794
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount*(vcount-1)));
97
794
            IGRAPH_ASSERT(igraph_vector_all_le(&v, &v2));
98
99
794
            if (vcount >= 10) {
100
752
                igraph_betweenness_subset(&graph, NULL, &v,
101
752
                                          igraph_vss_range(0,5), igraph_vss_range(5,10),
102
752
                                          igraph_vss_all(), IGRAPH_DIRECTED, false);
103
752
                igraph_edge_betweenness_subset(&graph, NULL, &v,
104
752
                                               igraph_vss_range(0,10), igraph_vss_range(0,10),
105
752
                                               igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_DIRECTED, false);
106
752
            }
107
794
            igraph_closeness_cutoff(&graph, &v, &iv, &b, igraph_vss_all(), IGRAPH_ALL, NULL, true, 3);
108
794
            igraph_closeness_cutoff(&graph, &v, &iv, &b, igraph_vss_all(), IGRAPH_OUT, NULL, true, 4);
109
794
            igraph_harmonic_centrality_cutoff(&graph, &v, igraph_vss_all(), IGRAPH_ALL, NULL, true, 3);
110
794
            igraph_harmonic_centrality_cutoff(&graph, &v, igraph_vss_all(), IGRAPH_IN, NULL, true, 4);
111
794
            igraph_global_efficiency(&graph, NULL, &r, IGRAPH_DIRECTED);
112
794
            igraph_local_efficiency(&graph, NULL, &v, igraph_vss_all(), IGRAPH_DIRECTED, IGRAPH_OUT);
113
794
            igraph_transitivity_undirected(&graph, &r, IGRAPH_TRANSITIVITY_NAN);
114
794
            igraph_transitivity_local_undirected(&graph, &v, igraph_vss_all(), IGRAPH_TRANSITIVITY_NAN);
115
794
            igraph_transitivity_avglocal_undirected(&graph, &r, IGRAPH_TRANSITIVITY_ZERO);
116
794
            igraph_count_adjacent_triangles(&graph, &v, igraph_vss_all());
117
118
794
            igraph_pagerank(&graph, NULL, &v, &r, 0.6, IGRAPH_DIRECTED, igraph_vss_all(), IGRAPH_PAGERANK_ALGO_PRPACK,
119
794
                            NULL);
120
794
            IGRAPH_ASSERT(vcount == 0 || igraph_almost_equals(igraph_vector_sum(&v), 1.0, 1e-10));
121
122
794
            igraph_constraint(&graph, &v, igraph_vss_all(), NULL);
123
794
            igraph_spanner(&graph, &iv, 2.34, NULL);
124
125
794
            igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_COLLAPSE, NULL);
126
794
            igraph_simplify(&graph, /* remove_multiple */ true, /* remove_loops */ false, NULL);
127
794
            igraph_trussness(&graph, &iv);
128
129
794
            igraph_vector_int_destroy(&iv);
130
794
            igraph_vector_destroy(&v2);
131
794
            igraph_vector_destroy(&v);
132
794
        }
133
134
794
        igraph_destroy(&graph);
135
794
    }
136
137
794
    igraph_vector_int_destroy(&edges);
138
139
794
    IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY);
140
141
794
    return 0;  // Non-zero return values are reserved for future use.
142
794
}