Coverage Report

Created: 2025-10-10 06:07

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