Coverage Report

Created: 2023-03-26 07:33

/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) ? NULL :
90
0
      gnutls_realloc(ptr, nmemb * size);
91
0
}
92
93
#if 0
94
/* don't use them. They are included for documentation.
95
 */
96
97
/**
98
 * gnutls_malloc:
99
 * @s: size to allocate in bytes
100
 *
101
 * This function will allocate 's' bytes data, and
102
 * return a pointer to memory. This function is supposed
103
 * to be used by callbacks.
104
 *
105
 * The allocation function used is the one set by
106
 * gnutls_global_set_mem_functions().
107
 **/
108
void *gnutls_malloc(size_t s)
109
{
110
  int x;
111
}
112
113
/**
114
 * gnutls_free:
115
 * @ptr: pointer to memory
116
 *
117
 * This function will free data pointed by ptr.
118
 *
119
 * The deallocation function used is the one set by
120
 * gnutls_global_set_mem_functions().
121
 *
122
 **/
123
void gnutls_free(void *ptr)
124
{
125
  int x;
126
}
127
128
#endif
129
130
/* Returns 1 if the provided buffer is all zero.
131
 * It leaks no information via timing.
132
 */
133
unsigned _gnutls_mem_is_zero(const uint8_t * ptr, unsigned size)
134
0
{
135
0
  unsigned i;
136
0
  uint8_t res = 0;
137
138
0
  for (i = 0; i < size; i++) {
139
0
    res |= ptr[i];
140
0
  }
141
142
0
  return ((res == 0) ? 1 : 0);
143
0
}