Coverage Report

Created: 2025-11-11 06:56

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