Coverage Report

Created: 2024-11-25 06:27

/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
74.0k
{
41
74.0k
  void *ret;
42
43
74.0k
  if (size == 0)
44
0
    return ptr;
45
46
74.0k
  ret = gnutls_realloc(ptr, size);
47
74.0k
  if (ret == NULL) {
48
0
    gnutls_free(ptr);
49
0
  }
50
51
74.0k
  return ret;
52
74.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
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
25.8k
{
73
25.8k
  size_t siz;
74
25.8k
  char *ret;
75
76
25.8k
  if (unlikely(!str))
77
0
    return NULL;
78
79
25.8k
  siz = strlen(str) + 1;
80
81
25.8k
  ret = gnutls_malloc(siz);
82
25.8k
  if (ret != NULL)
83
25.8k
    memcpy(ret, str, siz);
84
25.8k
  return ret;
85
25.8k
}
86
87
void *_gnutls_reallocarray(void *ptr, size_t nmemb, size_t size)
88
244
{
89
244
  return xalloc_oversized(nmemb, size) ?
90
0
           NULL :
91
244
           gnutls_realloc(ptr, nmemb * size);
92
244
}
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
38
{
136
38
  unsigned i;
137
38
  uint8_t res = 0;
138
139
1.25k
  for (i = 0; i < size; i++) {
140
1.21k
    res |= ptr[i];
141
1.21k
  }
142
143
38
  return ((res == 0) ? 1 : 0);
144
38
}