| 1 |
/* |
| 2 |
* Copyright (C) 2014 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitmemory.h" |
| 9 |
|
| 10 |
#include <stdarg.h> |
| 11 |
#include <stdint.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
#include <errno.h> |
| 16 |
|
| 17 |
/** |
| 18 |
* cockpit_memory_clear: |
| 19 |
* |
| 20 |
* The cockpit_memory_clear function overwrites LEN bytes of memory |
| 21 |
* pointed to by DATA with non-sensitive values. When LEN is -1, DATA |
| 22 |
* must be zero-terminated and all bytes until the zero are |
| 23 |
* overwritten. |
| 24 |
* |
| 25 |
* This is very similar to memset but we take extra measures to |
| 26 |
* prevent the compiler from optimizing it away. |
| 27 |
*/ |
| 28 |
|
| 29 |
void |
| 30 |
cockpit_memory_clear (void * data, |
| 31 |
ssize_t len) |
| 32 |
{ |
| 33 |
if (len < 0) |
| 34 |
len = strlen (data); |
| 35 |
|
| 36 |
explicit_bzero (data, len); |
| 37 |
} |
| 38 |
|
| 39 |
static void |
| 40 |
abort_errno (const char *msg) |
| 41 |
{ |
| 42 |
perror (msg); |
| 43 |
abort (); |
| 44 |
} |
| 45 |
|
| 46 |
void * |
| 47 |
mallocx (size_t size) |
| 48 |
{ |
| 49 |
void *r = malloc (size); |
| 50 |
if (r == NULL) |
| 51 |
abort_errno ("failed to allocate memory"); |
| 52 |
return r; |
| 53 |
} |
| 54 |
|
| 55 |
void * |
| 56 |
callocx (size_t nmemb, size_t size) |
| 57 |
{ |
| 58 |
void *r = calloc (nmemb, size); |
| 59 |
if (r == NULL) |
| 60 |
abort_errno ("failed to allocate memory"); |
| 61 |
return r; |
| 62 |
} |
| 63 |
|
| 64 |
char * |
| 65 |
strdupx (const char *s) |
| 66 |
{ |
| 67 |
char *r = strdup (s); |
| 68 |
if (r == NULL) |
| 69 |
abort_errno ("failed to allocate memory for strdup"); |
| 70 |
return r; |
| 71 |
} |
| 72 |
|
| 73 |
char * |
| 74 |
strndupx (const char *s, |
| 75 |
size_t n) |
| 76 |
{ |
| 77 |
char *r = strndup (s, n); |
| 78 |
if (r == NULL) |
| 79 |
abort_errno ("failed to allocate memory for strndup"); |
| 80 |
return r; |
| 81 |
} |
| 82 |
|
| 83 |
int |
| 84 |
asprintfx (char **strp, |
| 85 |
const char *fmt, ...) |
| 86 |
{ |
| 87 |
va_list args; |
| 88 |
int r; |
| 89 |
|
| 90 |
va_start (args, fmt); |
| 91 |
r = vasprintf (strp, fmt, args); |
| 92 |
va_end (args); |
| 93 |
if (r < 0) |
| 94 |
{ |
| 95 |
fprintf (stderr, "Cannot allocate memory for asprintf\n"); |
| 96 |
abort (); |
| 97 |
} |
| 98 |
return r; |
| 99 |
} |
| 100 |
|
| 101 |
void * |
| 102 |
reallocx (void *ptr, |
| 103 |
size_t size) |
| 104 |
{ |
| 105 |
void *r = realloc (ptr, size); |
| 106 |
if (r == NULL) |
| 107 |
abort_errno ("failed to allocate memory"); |
| 108 |
return r; |
| 109 |
} |
| 110 |
|
| 111 |
/* this is like reallocarray(3), but this does not yet exist everywhere; plus |
| 112 |
* abort() on ENOMEM */ |
| 113 |
void * |
| 114 |
reallocarrayx (void *ptr, |
| 115 |
size_t nmemb, |
| 116 |
size_t size) |
| 117 |
{ |
| 118 |
void *r; |
| 119 |
|
| 120 |
if (nmemb >= SIZE_MAX / size) |
| 121 |
{ |
| 122 |
fprintf (stderr, "reallocarr: overflow (nmemb %zu)\n", nmemb); |
| 123 |
abort (); |
| 124 |
} |
| 125 |
r = realloc (ptr, nmemb * size); |
| 126 |
if (r == NULL) |
| 127 |
abort_errno ("failed to allocate memory for realloc"); |
| 128 |
return r; |
| 129 |
} |