Coverage Report

Created: 2025-07-11 06:45

/src/croaring_fuzzer.c
Line
Count
Source (jump to first uncovered line)
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
1.03k
int bitmap32(const char *data, size_t size) {
25
    // We test that deserialization never fails.
26
1.03k
    roaring_bitmap_t *bitmap =
27
1.03k
        roaring_bitmap_portable_deserialize_safe(data, size);
28
1.03k
    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
549
        const char *reason_failure = NULL;
32
549
        if (roaring_bitmap_internal_validate(bitmap, &reason_failure)) {
33
            // the bitmap is ok!
34
392
            uint32_t cardinality = roaring_bitmap_get_cardinality(bitmap);
35
36
353k
            for (uint32_t i = 100; i < 1000; i++) {
37
352k
                if (!roaring_bitmap_contains(bitmap, i)) {
38
310k
                    cardinality++;
39
310k
                    roaring_bitmap_add(bitmap, i);
40
310k
                }
41
352k
            }
42
392
            uint32_t new_cardinality = roaring_bitmap_get_cardinality(bitmap);
43
392
            if (cardinality != new_cardinality) {
44
0
                printf("bug\n");
45
0
                exit(1);
46
0
            }
47
392
        }
48
549
        roaring_bitmap_free(bitmap);
49
549
    }
50
1.03k
    return 0;
51
1.03k
}
52
53
1.74k
int bitmap64(const char *data, size_t size) {
54
    // We test that deserialization never fails.
55
1.74k
    roaring64_bitmap_t *bitmap =
56
1.74k
        roaring64_bitmap_portable_deserialize_safe(data, size);
57
1.74k
    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
788
        const char *reason_failure = NULL;
61
788
        if (roaring64_bitmap_internal_validate(bitmap, &reason_failure)) {
62
            // the bitmap is ok!
63
588
            uint64_t cardinality = roaring64_bitmap_get_cardinality(bitmap);
64
65
529k
            for (uint32_t i = 100; i < 1000; i++) {
66
529k
                if (!roaring64_bitmap_contains(bitmap, i)) {
67
497k
                    cardinality++;
68
497k
                    roaring64_bitmap_add(bitmap, i);
69
497k
                }
70
529k
            }
71
588
            uint64_t new_cardinality = roaring64_bitmap_get_cardinality(bitmap);
72
588
            if (cardinality != new_cardinality) {
73
0
                printf("bug\n");
74
0
                exit(1);
75
0
            }
76
588
        }
77
788
        roaring64_bitmap_free(bitmap);
78
788
    }
79
1.74k
    return 0;
80
1.74k
}
81
82
2.78k
int LLVMFuzzerTestOneInput(const char *data, size_t size) {
83
2.78k
    if (size == 0) {
84
0
        return 0;
85
0
    }
86
2.78k
    if (data[0] % 2 == 0) {
87
1.03k
        return bitmap32(data + 1, size - 1);
88
1.74k
    } else {
89
1.74k
        return bitmap64(data + 1, size - 1);
90
1.74k
    }
91
2.78k
}