Coverage Report

Created: 2025-04-11 06:09

/src/yara/libyara/mem.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright (c) 2007-2013. The YARA Authors. All Rights Reserved.
3
4
Redistribution and use in source and binary forms, with or without modification,
5
are permitted provided that the following conditions are met:
6
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
9
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation and/or
12
other materials provided with the distribution.
13
14
3. Neither the name of the copyright holder nor the names of its contributors
15
may be used to endorse or promote products derived from this software without
16
specific prior written permission.
17
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <yara/error.h>
31
#include <yara/mem.h>
32
33
#if defined(_WIN32) || defined(__CYGWIN__)
34
35
#include <string.h>
36
#include <windows.h>
37
38
static HANDLE hHeap;
39
40
int yr_heap_alloc(void)
41
{
42
  hHeap = HeapCreate(0, 0x8000, 0);
43
44
  if (hHeap == NULL)
45
    return ERROR_INTERNAL_FATAL_ERROR;
46
47
  return ERROR_SUCCESS;
48
}
49
50
int yr_heap_free(void)
51
{
52
  if (HeapDestroy(hHeap))
53
    return ERROR_SUCCESS;
54
  else
55
    return ERROR_INTERNAL_FATAL_ERROR;
56
}
57
58
void* yr_calloc(size_t count, size_t size)
59
{
60
  return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, count * size);
61
}
62
63
void* yr_malloc(size_t size)
64
{
65
  return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, size);
66
}
67
68
void* yr_realloc(void* ptr, size_t size)
69
{
70
  if (ptr == NULL)
71
    return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, size);
72
73
  return (void*) HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, ptr, size);
74
}
75
76
char* yr_strdup(const char* str)
77
{
78
  size_t len = strlen(str);
79
  char* dup = (char*) yr_malloc(len + 1);
80
81
  if (dup == NULL)
82
    return NULL;
83
84
  memcpy(dup, str, len);
85
  dup[len] = '\0';
86
87
  return (char*) dup;
88
}
89
90
char* yr_strndup(const char* str, size_t n)
91
{
92
  size_t len = strnlen(str, n);
93
  char* dup = (char*) yr_malloc(len + 1);
94
95
  if (dup == NULL)
96
    return NULL;
97
98
  memcpy(dup, str, len);
99
  dup[len] = '\0';
100
101
  return (char*) dup;
102
}
103
104
YR_API void yr_free(void* ptr)
105
{
106
  HeapFree(hHeap, 0, ptr);
107
}
108
109
#else
110
111
#include <stdio.h>
112
#include <stdlib.h>
113
#include <string.h>
114
115
#if defined(MIMALLOC)
116
#include <mimalloc-override.h>
117
#endif
118
119
int yr_heap_alloc(void)
120
11
{
121
11
  return ERROR_SUCCESS;
122
11
}
123
124
int yr_heap_free(void)
125
0
{
126
0
  return ERROR_SUCCESS;
127
0
}
128
129
void* yr_calloc(size_t count, size_t size)
130
2.23M
{
131
2.23M
  return calloc(count, size);
132
2.23M
}
133
134
void* yr_malloc(size_t size)
135
235M
{
136
235M
  return malloc(size);
137
235M
}
138
139
void* yr_realloc(void* ptr, size_t size)
140
1.29M
{
141
1.29M
  return realloc(ptr, size);
142
1.29M
}
143
144
char* yr_strdup(const char* str)
145
114M
{
146
114M
  return strdup(str);
147
114M
}
148
149
char* yr_strndup(const char* str, size_t n)
150
3.48M
{
151
3.48M
  return strndup(str, n);
152
3.48M
}
153
154
YR_API void yr_free(void* ptr)
155
361M
{
156
361M
  free(ptr);
157
361M
}
158
159
#endif