Coverage Report

Created: 2026-08-31 06:48

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.01k
bool vector_isininterval_eps(const igraph_vector_t *v, igraph_real_t lo, igraph_real_t hi) {
27
3.01k
    const igraph_int_t n = igraph_vector_size(v);
28
29
101k
    for (igraph_int_t i=0; i < n; i++) {
30
98.6k
        igraph_real_t x = VECTOR(*v)[i];
31
98.6k
        if (igraph_cmp_epsilon(lo, x, eps) > 0 || igraph_cmp_epsilon(x, hi, eps) > 0) {
32
0
            return false;
33
0
        }
34
98.6k
    }
35
36
3.01k
    return true;
37
3.01k
}
38
39
801
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
40
801
    igraph_t graph;
41
801
    igraph_vector_int_t edges;
42
801
    igraph_vector_t weights;
43
44
801
    igraph_set_warning_handler(igraph_warning_handler_ignore);
45
46
801
    if (Size % 3 == 0 || Size % 3 == 2 || Size > (3 * 256) + 1 || Size < 1) {
47
15
        return 0;
48
15
    }
49
50
786
    igraph_vector_int_init(&edges, ((Size-1) / 3) * 2);
51
786
    igraph_vector_init(&weights, (Size-1) / 3);
52
17.7k
    for (size_t i=0; i < ((Size-1) / 3); ++i) {
53
16.9k
        VECTOR(edges)[i * 2] = Data[i * 3 + 1];
54
16.9k
        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
16.9k
        VECTOR(weights)[i] = ((double) Data[i * 3 + 3] + 1.0) / 105.0;
58
16.9k
    }
59
60
    // Turn on attribute handling. Weights will be stored as an edge attribute
61
    // in order to allow graph simplification while retainingweights.
62
786
    igraph_set_attribute_table(&igraph_cattribute_table);
63
64
786
    igraph_rng_seed(igraph_rng_default(), 42);
65
66
786
    if (igraph_create(&graph, &edges, Data[0], IGRAPH_DIRECTED) == IGRAPH_SUCCESS) {
67
786
        const igraph_int_t vcount = igraph_vcount(&graph);
68
786
        igraph_vector_t v;
69
786
        igraph_vector_int_t iv;
70
786
        igraph_bool_t b;
71
786
        igraph_real_t r;
72
73
        /* Limit graph size for the sake of performance. */
74
786
        if (vcount <= 64) {
75
753
            igraph_vector_init(&v, 0);
76
753
            igraph_vector_int_init(&iv, 0);
77
78
753
            igraph_betweenness_cutoff(&graph, &weights, &v, igraph_vss_all(), IGRAPH_UNDIRECTED, false, 4);
79
753
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount == 0 ? 0 : (vcount-1)*(vcount-2)/2));
80
81
753
            igraph_betweenness_cutoff(&graph, &weights, &v, igraph_vss_all(), IGRAPH_DIRECTED, false, 5);
82
753
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount == 0 ? 0 : (vcount-1)*(vcount-2)));
83
84
753
            igraph_edge_betweenness_cutoff(&graph, &weights, &v, igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_DIRECTED,
85
753
                                           false, 4);
86
753
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount*(vcount-1)/2));
87
88
753
            igraph_edge_betweenness_cutoff(&graph, &weights, &v, igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_UNDIRECTED,
89
753
                                           false, 3);
90
753
            IGRAPH_ASSERT(vector_isininterval_eps(&v, 0, vcount*(vcount-1)));
91
92
753
            if (vcount >= 10) {
93
687
                igraph_betweenness_subset(&graph, &weights,
94
687
                                          &v,
95
687
                                          igraph_vss_range(0,5), igraph_vss_range(5,10),
96
687
                                          igraph_vss_all(), IGRAPH_DIRECTED, false);
97
687
                igraph_edge_betweenness_subset(&graph, &weights,
98
687
                                               &v,
99
687
                                               igraph_vss_range(0,10), igraph_vss_range(0, 10),
100
687
                                               igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_DIRECTED,false);
101
687
            }
102
753
            igraph_closeness_cutoff(&graph, &v, &iv, &b, igraph_vss_all(), IGRAPH_ALL, &weights, true, 3);
103
753
            igraph_closeness_cutoff(&graph, &v, &iv, &b, igraph_vss_all(), IGRAPH_OUT, &weights, true, 4);
104
753
            igraph_harmonic_centrality_cutoff(&graph, &v, igraph_vss_all(), IGRAPH_ALL, &weights, true, 3);
105
753
            igraph_harmonic_centrality_cutoff(&graph, &v, igraph_vss_all(), IGRAPH_IN, &weights, true, 4);
106
753
            igraph_global_efficiency(&graph, &weights, &r, IGRAPH_DIRECTED);
107
753
            igraph_local_efficiency(&graph, &weights, &v, igraph_vss_all(), IGRAPH_DIRECTED, IGRAPH_OUT);
108
109
753
            igraph_pagerank(&graph, &weights, &v, &r, 0.6, IGRAPH_DIRECTED, igraph_vss_all(),
110
753
                            IGRAPH_PAGERANK_ALGO_PRPACK, NULL);
111
753
            IGRAPH_ASSERT(vcount == 0 || igraph_almost_equals(igraph_vector_sum(&v), 1.0, 1e-10));
112
113
753
            igraph_constraint(&graph, &v, igraph_vss_all(), &weights);
114
115
753
            {
116
753
                igraph_attribute_combination_t comb;
117
753
                SETEANV(&graph, "weight", &weights);
118
753
                igraph_attribute_combination(&comb,
119
753
                                             "weight", IGRAPH_ATTRIBUTE_COMBINE_SUM,
120
753
                                             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
753
                igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_COLLAPSE, &comb);
125
753
                igraph_simplify(&graph, true, true, &comb);
126
753
                igraph_attribute_combination_destroy(&comb);
127
753
                EANV(&graph, "weight", &weights);
128
753
                DELEA(&graph, "weight");
129
753
            }
130
131
753
            igraph_diversity(&graph, &weights, &v, igraph_vss_all());
132
753
            igraph_transitivity_barrat(&graph, &v, igraph_vss_all(), &weights, IGRAPH_TRANSITIVITY_NAN);
133
134
753
            igraph_vector_int_destroy(&iv);
135
753
            igraph_vector_destroy(&v);
136
753
        }
137
138
786
        igraph_destroy(&graph);
139
786
    }
140
141
786
    igraph_vector_int_destroy(&edges);
142
786
    igraph_vector_destroy(&weights);
143
144
786
    IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY);
145
146
786
    return 0;  // Non-zero return values are reserved for future use.
147
786
}