2 Copyright (C) 2017-2023 Ben Kibbey <bjk@luxsci.net>
4 This file is part of libpwmd.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License version 2.1 as published by the Free Software Foundation.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
35 _xrealloc_gpgrt (void *p
, size_t n
)
38 return p
? _xrealloc (p
, n
) : _xmalloc (n
);
44 /* Borrowed from g10code:
45 * https://lists.gnupg.org/pipermail/gnupg-devel/2018-November/034060.html
48 wipememory (void *ptr
, int c
, size_t len
)
50 #if defined(HAVE_W32_SYSTEM) && defined(SecureZeroMemory)
52 SecureZeroMemory (ptr
, len
);
53 #elif defined(HAVE_EXPLICIT_BZERO)
55 explicit_bzero (ptr
, len
);
56 #elif defined(HAVE_MEMSET_S)
57 memset_s (ptr
, len
, c
, len
);
59 /* Prevent compiler from optimizing away the call to memset by accessing
60 memset through volatile pointer. */
61 static void *(*volatile memset_ptr
)(void *, int, size_t) = (void *)memset
;
62 memset_ptr (ptr
, c
, len
);
82 m
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
83 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
84 wipememory (p
, 0, m
->size
);
89 _xmalloc (size_t size
)
96 m
= malloc (sizeof (struct memchunk_s
)+size
);
101 return (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
105 _xcalloc (size_t nmemb
, size_t size
)
108 struct memchunk_s
*m
;
110 m
= malloc (sizeof (struct memchunk_s
)+(nmemb
*size
));
114 m
->size
= nmemb
*size
;
115 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
116 memset (p
, 0, m
->size
);
121 _xrealloc (void *ptr
, size_t size
)
124 struct memchunk_s
*m
, *mp
;
129 m
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
130 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
131 wipememory (p
, 0, m
->size
);
136 return _xmalloc (size
);
138 m
= malloc (sizeof (struct memchunk_s
)+size
);
143 np
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
145 mp
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
146 p
= (void *)((char *)mp
+(offsetof (struct memchunk_s
, data
)));
148 n
= size
> mp
->size
? mp
->size
: size
;
150 wipememory (p
, 0, mp
->size
);
153 return (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));