Coverage Report

Created: 2024-11-25 06:29

/src/gnutls/lib/mem.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2001-2012 Free Software Foundation, Inc.
3
 *
4
 * Author: Nikos Mavrogiannopoulos
5
 *
6
 * This file is part of GnuTLS.
7
 *
8
 * The GnuTLS is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public License
10
 * as published by the Free Software Foundation; either version 2.1 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
20
 *
21
 */
22
23
#include "gnutls_int.h"
24
#include "errors.h"
25
#include "num.h"
26
#include "xalloc-oversized.h"
27
28
gnutls_alloc_function gnutls_secure_malloc = malloc;
29
gnutls_alloc_function gnutls_malloc = malloc;
30
gnutls_free_function gnutls_free = free;
31
gnutls_realloc_function gnutls_realloc = realloc;
32
33
void *(*gnutls_calloc)(size_t, size_t) = calloc;
34
char *(*gnutls_strdup)(const char *) = _gnutls_strdup;
35
36
/* This realloc will free ptr in case realloc
37
 * fails.
38
 */
39
void *gnutls_realloc_fast(void *ptr, size_t size)
40
32.0k
{
41
32.0k
  void *ret;
42
43
32.0k
  if (size == 0)
44
0
    return ptr;
45
46
32.0k
  ret = gnutls_realloc(ptr, size);
47
32.0k
  if (ret == NULL) {
48
0
    gnutls_free(ptr);
49
0
  }
50
51
32.0k
  return ret;
52
32.0k
}
53
54
/* This will free ptr in case reallocarray fails.
55
 */
56
void *_gnutls_reallocarray_fast(void *ptr, size_t nmemb, size_t size)
57
12.3k
{
58
12.3k
  void *ret;
59
60
12.3k
  if (size == 0)
61
0
    return ptr;
62
63
12.3k
  ret = _gnutls_reallocarray(ptr, nmemb, size);
64
12.3k
  if (ret == NULL) {
65
0
    gnutls_free(ptr);
66
0
  }
67
68
12.3k
  return ret;
69
12.3k
}
70
71
char *_gnutls_strdup(const char *str)
72
18.7k
{
73
18.7k
  size_t siz;
74
18.7k
  char *ret;
75
76
18.7k
  if (unlikely(!str))
77
0
    return NULL;
78
79
18.7k
  siz = strlen(str) + 1;
80
81
18.7k
  ret = gnutls_malloc(siz);
82
18.7k
  if (ret != NULL)
83
18.7k
    memcpy(ret, str, siz);
84
18.7k
  return ret;
85
18.7k
}
86
87
void *_gnutls_reallocarray(void *ptr, size_t nmemb, size_t size)
88
13.3k
{
89
13.3k
  return xalloc_oversized(nmemb, size) ?
90
0
           NULL :
91
13.3k
           gnutls_realloc(ptr, nmemb * size);
92
13.3k
}
93
94
#if 0
95
/* don't use them. They are included for documentation.
96
 */
97
98
/**
99
 * gnutls_malloc:
100
 * @s: size to allocate in bytes
101
 *
102
 * This function will allocate 's' bytes data, and
103
 * return a pointer to memory. This function is supposed
104
 * to be used by callbacks.
105
 *
106
 * The allocation function used is the one set by
107
 * gnutls_global_set_mem_functions().
108
 **/
109
void *gnutls_malloc(size_t s)
110
{
111
  int x;
112
}
113
114
/**
115
 * gnutls_free:
116
 * @ptr: pointer to memory
117
 *
118
 * This function will free data pointed by ptr.
119
 *
120
 * The deallocation function used is the one set by
121
 * gnutls_global_set_mem_functions().
122
 *
123
 **/
124
void gnutls_free(void *ptr)
125
{
126
  int x;
127
}
128
129
#endif
130
131
/* Returns 1 if the provided buffer is all zero.
132
 * It leaks no information via timing.
133
 */
134
unsigned _gnutls_mem_is_zero(const uint8_t *ptr, unsigned size)
135
332
{
136
332
  unsigned i;
137
332
  uint8_t res = 0;
138
139
12.9k
  for (i = 0; i < size; i++) {
140
12.5k
    res |= ptr[i];
141
12.5k
  }
142
143
332
  return ((res == 0) ? 1 : 0);
144
332
}