Coverage Report

Created: 2025-08-25 07:06

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