Coverage Report

Created: 2025-07-01 06:59

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