Coverage Report

Created: 2024-06-20 06:28

/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
0
{
41
0
  void *ret;
42
43
0
  if (size == 0)
44
0
    return ptr;
45
46
0
  ret = gnutls_realloc(ptr, size);
47
0
  if (ret == NULL) {
48
0
    gnutls_free(ptr);
49
0
  }
50
51
0
  return ret;
52
0
}
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
0
{
58
0
  void *ret;
59
60
0
  if (size == 0)
61
0
    return ptr;
62
63
0
  ret = _gnutls_reallocarray(ptr, nmemb, size);
64
0
  if (ret == NULL) {
65
0
    gnutls_free(ptr);
66
0
  }
67
68
0
  return ret;
69
0
}
70
71
char *_gnutls_strdup(const char *str)
72
0
{
73
0
  size_t siz;
74
0
  char *ret;
75
76
0
  if (unlikely(!str))
77
0
    return NULL;
78
79
0
  siz = strlen(str) + 1;
80
81
0
  ret = gnutls_malloc(siz);
82
0
  if (ret != NULL)
83
0
    memcpy(ret, str, siz);
84
0
  return ret;
85
0
}
86
87
void *_gnutls_reallocarray(void *ptr, size_t nmemb, size_t size)
88
0
{
89
0
  return xalloc_oversized(nmemb, size) ?
90
0
           NULL :
91
0
           gnutls_realloc(ptr, nmemb * size);
92
0
}
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
0
{
136
0
  unsigned i;
137
0
  uint8_t res = 0;
138
139
0
  for (i = 0; i < size; i++) {
140
0
    res |= ptr[i];
141
0
  }
142
143
0
  return ((res == 0) ? 1 : 0);
144
0
}