Coverage Report

Created: 2026-04-12 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/igraph/fuzzing/weighted_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
3.30k
bool vector_isininterval_eps(const igraph_vector_t *v, igraph_real_t lo, igraph_real_t hi) {
27
3.30k
    const igraph_int_t n = igraph_vector_size(v);
28
29
108k
    for (igraph_int_t i=0; i < n; i++) {
30
104k
        igraph_real_t x = VECTOR(*v)[i];
31
104k
        if (igraph_cmp_epsilon(lo, x, eps) > 0 || igraph_cmp_epsilon(x, hi, eps) > 0) {
32
0
            return false;
33
0
        }
34
104k
    }
35
36
3.30k
    return true;
37
3.30k
}
38
39
881
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
40
881
    igraph_t graph;
41
881
    igraph_vector_int_t edges;
42
881
    igraph_vector_t weights;
43
44
881
    igraph_set_warning_handler(igraph_warning_handler_ignore);
45
46
881
    if (Size % 3 == 0 || Size % 3 == 2 || Size > (3 * 256) + 1 || Size < 1) {
47
15
        return 0;
48
15
    }
49
50
866
    igraph_vector_int_init(&edges, ((Size-1) / 3) * 2);
51
866
    igraph_vector_init(&weights, (Size-1) / 3);
52
19.7k
    for (size_t i=0; i < ((Size-1) / 3); ++i) {
53
18.8k
        VECTOR(edges)[i * 2] = Data[i * 3 + 1];
54
18.8k
        VECTOR(edges)[i * 2 + 1] = Data[i * 3 + 2];
55
        // We keep the weights strictly positive, as this is required by some algorithms.
56
        // Error at src/centrality/betweenness.c:437 : Weight vector must be positive. - Invalid value.
57
18.8k
        VECTOR(weights)[i] = ((double) Data[i * 3 + 3] + 1.0) / 105.0;
58
18.8k
    }
59
60
    // Turn on attribute handling. Weights will be stored as an edge attribute
61
    // in order to allow graph simplification while retainingweights.
62
866
    igraph_set_attribute_table(&igraph_cattribute_table);
63
64
866
    igraph_rng_seed(igraph_rng_default(), 42);
65
66
866
    if (igraph_create(&graph, &edges, Data[0], IGRAPH_DIRECTED) == IGRAPH_SUCCESS) {
67
866
        const igraph_int_t vcount = igraph_vcount(&graph);
68
866
        igraph_vector_t v;
69
866
        igraph_vector_int_t iv;
70
866
        igraph_bool_t b;
71
866
        igraph_real_t r;
72
73
        /* Limit graph size for the sake of performance. */
74
866
        if (vcount <= 64) {
75
827
            igraph_vector_init(&v, 0);
76
827
            igraph_vector_int_init(&iv, 0);
77
78
827
            igraph_betweenness_cutoff(&graph, &weights, &v, igraph_vss_all(), IGRAPH_UNDIRECTED, false, 4);
79
827
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount == 0 ? 0 : (vcount-1)*(vcount-2)/2));
80
81
827
            igraph_betweenness_cutoff(&graph, &weights, &v, igraph_vss_all(), IGRAPH_DIRECTED, false, 5);
82
827
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount == 0 ? 0 : (vcount-1)*(vcount-2)));
83
84
827
            igraph_edge_betweenness_cutoff(&graph, &weights, &v, igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_DIRECTED,
85
827
                                           false, 4);
86
827
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount*(vcount-1)/2));
87
88
827
            igraph_edge_betweenness_cutoff(&graph, &weights, &v, igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_UNDIRECTED,
89
827
                                           false, 3);
90
827
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount*(vcount-1)));
91
92
827
            if (vcount >= 10) {
93
732
                igraph_betweenness_subset(&graph, &weights,
94
732
                                          &v,
95
732
                                          igraph_vss_range(0,5), igraph_vss_range(5,10),
96
732
                                          igraph_vss_all(), IGRAPH_DIRECTED, false);
97
732
                igraph_edge_betweenness_subset(&graph, &weights,
98
732
                                               &v,
99
732
                                               igraph_vss_range(0,10), igraph_vss_range(0, 10),
100
732
                                               igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_DIRECTED,false);
101
732
            }
102
827
            igraph_closeness_cutoff(&graph, &v, &iv, &b, igraph_vss_all(), IGRAPH_ALL, &weights, true, 3);
103
827
            igraph_closeness_cutoff(&graph, &v, &iv, &b, igraph_vss_all(), IGRAPH_OUT, &weights, true, 4);
104
827
            igraph_harmonic_centrality_cutoff(&graph, &v, igraph_vss_all(), IGRAPH_ALL, &weights, true, 3);
105
827
            igraph_harmonic_centrality_cutoff(&graph, &v, igraph_vss_all(), IGRAPH_IN, &weights, true, 4);
106
827
            igraph_global_efficiency(&graph, &weights, &r, IGRAPH_DIRECTED);
107
827
            igraph_local_efficiency(&graph, &weights, &v, igraph_vss_all(), IGRAPH_DIRECTED, IGRAPH_OUT);
108
109
827
            igraph_pagerank(&graph, &weights, &v, &r, 0.6, IGRAPH_DIRECTED, igraph_vss_all(),
110
827
                            IGRAPH_PAGERANK_ALGO_PRPACK, NULL);
111
827
            IGRAPH_ASSERT(vcount == 0 || igraph_almost_equals(igraph_vector_sum(&v), 1.0, 1e-10));
112
113
827
            igraph_constraint(&graph, &v, igraph_vss_all(), &weights);
114
115
827
            {
116
827
                igraph_attribute_combination_t comb;
117
827
                SETEANV(&graph, "weight", &weights);
118
827
                igraph_attribute_combination(&comb,
119
827
                                             "weight", IGRAPH_ATTRIBUTE_COMBINE_SUM,
120
827
                                             IGRAPH_NO_MORE_ATTRIBUTES);
121
                // This operation would be simpler if we use IGRAPH_TO_UNDIRECTED_EACH,
122
                // as collapsing edges happens in the simplification step anyway.
123
                // We use IGRAPH_TO_UNDIRECTED_COLLAPSE to exercise more code.
124
827
                igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_COLLAPSE, &comb);
125
827
                igraph_simplify(&graph, true, true, &comb);
126
827
                igraph_attribute_combination_destroy(&comb);
127
827
                EANV(&graph, "weight", &weights);
128
827
                DELEA(&graph, "weight");
129
827
            }
130
131
827
            igraph_diversity(&graph, &weights, &v, igraph_vss_all());
132
827
            igraph_transitivity_barrat(&graph, &v, igraph_vss_all(), &weights, IGRAPH_TRANSITIVITY_NAN);
133
134
827
            igraph_vector_int_destroy(&iv);
135
827
            igraph_vector_destroy(&v);
136
827
        }
137
138
866
        igraph_destroy(&graph);
139
866
    }
140
141
866
    igraph_vector_int_destroy(&edges);
142
866
    igraph_vector_destroy(&weights);
143
144
866
    IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY);
145
146
866
    return 0;  // Non-zero return values are reserved for future use.
147
866
}