Coverage Report

Created: 2023-12-08 06:48

/src/clamav/libclamav/partition_intersection.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright (C) 2014-2023 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
3
 *
4
 *  Authors: Kevin Lin <klin@sourcefire.com>
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License version 2 as
8
 *  published by the Free Software Foundation.
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,
18
 *  MA 02110-1301, USA.
19
 */
20
21
#if HAVE_CONFIG_H
22
#include "clamav-config.h"
23
#endif
24
25
#include "clamav.h"
26
#include "others.h"
27
#include "partition_intersection.h"
28
29
static bool partition_intersection_list_is_empty(partition_intersection_list_t* list)
30
7
{
31
7
    return (list->Head == NULL);
32
7
}
33
34
cl_error_t partition_intersection_list_init(partition_intersection_list_t* list)
35
7
{
36
7
    list->Head = NULL;
37
7
    list->Size = 0;
38
7
    return CL_SUCCESS;
39
7
}
40
41
cl_error_t partition_intersection_list_check(partition_intersection_list_t* list, unsigned* pitxn, off_t start, size_t size)
42
0
{
43
0
    partition_intersection_node_t *new_node, *check_node;
44
0
    cl_error_t ret = CL_CLEAN;
45
46
0
    *pitxn = list->Size;
47
48
0
    check_node = list->Head;
49
0
    while (check_node != NULL) {
50
0
        (*pitxn)--;
51
52
0
        if (start > check_node->Start) {
53
0
            if (check_node->Start + check_node->Size > (unsigned long)start) {
54
0
                ret = CL_VIRUS;
55
0
                break;
56
0
            }
57
0
        } else if (start < check_node->Start) {
58
0
            if (start + size > (unsigned long)(check_node->Start)) {
59
0
                ret = CL_VIRUS;
60
0
                break;
61
0
            }
62
0
        } else {
63
0
            ret = CL_VIRUS;
64
0
            break;
65
0
        }
66
67
0
        check_node = check_node->Next;
68
0
    }
69
70
    /* allocate new node for partition bounds */
71
0
    new_node = (partition_intersection_node_t*)cli_malloc(sizeof(partition_intersection_node_t));
72
0
    if (!new_node) {
73
0
        cli_dbgmsg("PRTN_INTXN: could not allocate new node for checklist!\n");
74
0
        partition_intersection_list_free(list);
75
0
        return CL_EMEM;
76
0
    }
77
78
0
    new_node->Start = start;
79
0
    new_node->Size  = size;
80
0
    new_node->Next  = list->Head;
81
82
0
    list->Head = new_node;
83
0
    (list->Size)++;
84
0
    return ret;
85
0
}
86
87
cl_error_t partition_intersection_list_free(partition_intersection_list_t* list)
88
7
{
89
7
    partition_intersection_node_t* next = NULL;
90
91
7
    while (!partition_intersection_list_is_empty(list)) {
92
0
        next = list->Head->Next;
93
94
0
        free(list->Head);
95
96
0
        list->Head = next;
97
0
        list->Size--;
98
0
    }
99
100
7
    return CL_SUCCESS;
101
7
}