Coverage Report

Created: 2025-07-18 06:53

/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
2
{
121
2
  return ERROR_SUCCESS;
122
2
}
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
936k
{
131
936k
  return calloc(count, size);
132
936k
}
133
134
void* yr_malloc(size_t size)
135
16.0M
{
136
16.0M
  return malloc(size);
137
16.0M
}
138
139
void* yr_realloc(void* ptr, size_t size)
140
1.35M
{
141
1.35M
  return realloc(ptr, size);
142
1.35M
}
143
144
char* yr_strdup(const char* str)
145
13.6M
{
146
13.6M
  return strdup(str);
147
13.6M
}
148
149
char* yr_strndup(const char* str, size_t n)
150
0
{
151
0
  return strndup(str, n);
152
0
}
153
154
YR_API void yr_free(void* ptr)
155
35.5M
{
156
35.5M
  free(ptr);
157
35.5M
}
158
159
#endif