Coverage Report

Created: 2025-03-15 06:58

/src/karchive_fuzzer.cc
Line
Count
Source
1
/*
2
# Copyright 2019 Google Inc.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#      http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
#
16
################################################################################
17
*/
18
19
/*
20
  Usage:
21
    python infra/helper.py build_image karchive
22
    python infra/helper.py build_fuzzers --sanitizer undefined|address|memory karchive
23
    python infra/helper.py run_fuzzer karchive karchive_fuzzer
24
*/
25
26
27
#include <QBuffer>
28
#include <QCoreApplication>
29
#include <QVector>
30
#include <iostream>
31
32
#include <KF6/KArchive/k7zip.h>
33
#include <KF6/KArchive/ktar.h>
34
#include <KF6/KArchive/kzip.h>
35
#include <KF6/KArchive/kar.h>
36
#include <KF6/KArchive/kcompressiondevice.h>
37
38
289k
void traverseArchive(const KArchiveDirectory *dir, const QString &path = QString()) {
39
289k
    const auto allEntries = dir->entries();
40
41
289k
    for (const auto& entryName : allEntries) {
42
243k
        auto entry = dir->entry(entryName);
43
243k
        const QString fullPath = path + QString::fromUtf8("/") + entryName;
44
45
243k
        if (entry->isFile()) {
46
36.8k
            auto file = static_cast<const KArchiveFile*>(entry);
47
36.8k
            auto fullpath = fullPath.toStdString();
48
36.8k
            auto filesize =  file->size();
49
36.8k
            auto datasize = file->data().size();
50
36.8k
            auto date =  file->date().toString().toStdString();
51
36.8k
            auto filename = file->name().toStdString();
52
36.8k
            auto user = file->user().toStdString();
53
36.8k
            auto group = file->group().toStdString();
54
206k
        } else if (entry->isDirectory()) {
55
206k
            auto subDir = static_cast<const KArchiveDirectory*>(entry);
56
206k
            traverseArchive(subDir, fullPath);
57
206k
        }
58
243k
    }
59
289k
}
60
61
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
62
19.6k
{
63
19.6k
    int argc = 0;
64
19.6k
    QCoreApplication a(argc, nullptr);
65
66
19.6k
    QBuffer b;
67
19.6k
    b.setData(QByteArray((const char *)data, size));
68
69
19.6k
    std::unique_ptr<KCompressionDevice> gzipKD(new KCompressionDevice(&b, false, KCompressionDevice::GZip));
70
19.6k
    std::unique_ptr<KCompressionDevice> bzipKD(new KCompressionDevice(&b, false, KCompressionDevice::BZip2));
71
19.6k
    std::unique_ptr<KCompressionDevice> xzKD(new KCompressionDevice(&b, false, KCompressionDevice::Xz));
72
19.6k
    std::unique_ptr<KCompressionDevice> zstdKD(new KCompressionDevice(&b, false, KCompressionDevice::Zstd));
73
74
19.6k
    const QVector<KArchive*> handlers = {
75
19.6k
        new K7Zip(&b),
76
19.6k
        new KTar(&b),
77
19.6k
        new KTar(gzipKD.get()),
78
19.6k
        new KTar(bzipKD.get()),
79
19.6k
        new KTar(xzKD.get()),
80
19.6k
        new KTar(zstdKD.get()),
81
19.6k
        new KZip(&b),
82
19.6k
        new KAr(&b)
83
19.6k
    };
84
85
156k
    for (KArchive *h : handlers) {
86
156k
        b.reset();
87
156k
        if (h->open(QIODevice::ReadOnly)) {
88
82.3k
            const KArchiveDirectory *rootDir = h->directory();
89
82.3k
            traverseArchive(rootDir); 
90
82.3k
            h->close();
91
82.3k
        }
92
156k
    }
93
94
19.6k
    qDeleteAll(handlers);
95
96
19.6k
    return 0;
97
19.6k
}