Coverage Report

Created: 2026-10-19 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/yara/libyara/mem.c
Line
Count
Source
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
  // HeapAlloc, unlike the calloc that the C runtime provides on the platforms
61
  // below, does not detect a count * size overflow and would silently hand out
62
  // a buffer smaller than the caller asked for. Callers of yr_calloc rely on
63
  // it behaving like the standard calloc, so perform the check here to keep
64
  // both implementations equivalent.
65
  if (size != 0 && count > (~(size_t) 0) / size)
66
    return NULL;
67
68
  return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, count * size);
69
}
70
71
void* yr_malloc(size_t size)
72
{
73
  return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, size);
74
}
75
76
void* yr_realloc(void* ptr, size_t size)
77
{
78
  if (ptr == NULL)
79
    return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, size);
80
81
  return (void*) HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, ptr, size);
82
}
83
84
char* yr_strdup(const char* str)
85
{
86
  size_t len = strlen(str);
87
  char* dup = (char*) yr_malloc(len + 1);
88
89
  if (dup == NULL)
90
    return NULL;
91
92
  memcpy(dup, str, len);
93
  dup[len] = '\0';
94
95
  return (char*) dup;
96
}
97
98
char* yr_strndup(const char* str, size_t n)
99
{
100
  size_t len = strnlen(str, n);
101
  char* dup = (char*) yr_malloc(len + 1);
102
103
  if (dup == NULL)
104
    return NULL;
105
106
  memcpy(dup, str, len);
107
  dup[len] = '\0';
108
109
  return (char*) dup;
110
}
111
112
YR_API void yr_free(void* ptr)
113
{
114
  HeapFree(hHeap, 0, ptr);
115
}
116
117
#else
118
119
#include <stdio.h>
120
#include <stdlib.h>
121
#include <string.h>
122
123
#if defined(MIMALLOC)
124
#include <mimalloc-override.h>
125
#endif
126
127
int yr_heap_alloc(void)
128
1
{
129
1
  return ERROR_SUCCESS;
130
1
}
131
132
int yr_heap_free(void)
133
0
{
134
0
  return ERROR_SUCCESS;
135
0
}
136
137
void* yr_calloc(size_t count, size_t size)
138
4
{
139
4
  return calloc(count, size);
140
4
}
141
142
void* yr_malloc(size_t size)
143
308
{
144
308
  return malloc(size);
145
308
}
146
147
void* yr_realloc(void* ptr, size_t size)
148
8
{
149
8
  return realloc(ptr, size);
150
8
}
151
152
char* yr_strdup(const char* str)
153
153
{
154
153
  return strdup(str);
155
153
}
156
157
char* yr_strndup(const char* str, size_t n)
158
0
{
159
0
  return strndup(str, n);
160
0
}
161
162
YR_API void yr_free(void* ptr)
163
470
{
164
470
  free(ptr);
165
470
}
166
167
#endif