Coverage Report

Created: 2026-05-30 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/croaring_fuzzer.c
Line
Count
Source
1
// Copyright 2021 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
17
#include <stdint.h>
18
#include <stdlib.h>
19
#include <string.h>
20
21
#include "roaring/roaring.h"
22
#include "roaring/roaring64.h"
23
24
0
int bitmap32(const char *data, size_t size) {
25
    // We test that deserialization never fails.
26
0
    roaring_bitmap_t *bitmap =
27
0
        roaring_bitmap_portable_deserialize_safe(data, size);
28
0
    if (bitmap) {
29
        // The bitmap may not be usable if it does not follow the specification.
30
        // We can validate the bitmap we recovered to make sure it is proper.
31
0
        const char *reason_failure = NULL;
32
0
        if (roaring_bitmap_internal_validate(bitmap, &reason_failure)) {
33
            // the bitmap is ok!
34
0
            uint32_t cardinality = roaring_bitmap_get_cardinality(bitmap);
35
36
0
            for (uint32_t i = 100; i < 1000; i++) {
37
0
                if (!roaring_bitmap_contains(bitmap, i)) {
38
0
                    cardinality++;
39
0
                    roaring_bitmap_add(bitmap, i);
40
0
                }
41
0
            }
42
0
            uint32_t new_cardinality = roaring_bitmap_get_cardinality(bitmap);
43
0
            if (cardinality != new_cardinality) {
44
0
                printf("bug\n");
45
0
                exit(1);
46
0
            }
47
0
        }
48
0
        roaring_bitmap_free(bitmap);
49
0
    }
50
0
    return 0;
51
0
}
52
53
0
int bitmap64(const char *data, size_t size) {
54
    // We test that deserialization never fails.
55
0
    roaring64_bitmap_t *bitmap =
56
0
        roaring64_bitmap_portable_deserialize_safe(data, size);
57
0
    if (bitmap) {
58
        // The bitmap may not be usable if it does not follow the specification.
59
        // We can validate the bitmap we recovered to make sure it is proper.
60
0
        const char *reason_failure = NULL;
61
0
        if (roaring64_bitmap_internal_validate(bitmap, &reason_failure)) {
62
            // the bitmap is ok!
63
0
            uint64_t cardinality = roaring64_bitmap_get_cardinality(bitmap);
64
65
0
            for (uint32_t i = 100; i < 1000; i++) {
66
0
                if (!roaring64_bitmap_contains(bitmap, i)) {
67
0
                    cardinality++;
68
0
                    roaring64_bitmap_add(bitmap, i);
69
0
                }
70
0
            }
71
0
            uint64_t new_cardinality = roaring64_bitmap_get_cardinality(bitmap);
72
0
            if (cardinality != new_cardinality) {
73
0
                printf("bug\n");
74
0
                exit(1);
75
0
            }
76
0
        }
77
0
        roaring64_bitmap_free(bitmap);
78
0
    }
79
0
    return 0;
80
0
}
81
82
0
int LLVMFuzzerTestOneInput(const char *data, size_t size) {
83
0
    if (size == 0) {
84
0
        return 0;
85
0
    }
86
0
    if (data[0] % 2 == 0) {
87
0
        return bitmap32(data + 1, size - 1);
88
0
    } else {
89
0
        return bitmap64(data + 1, size - 1);
90
0
    }
91
0
}