rtld_malloc: add cp2op() helper

converting user allocation address into overhead pointer

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D41150
This commit is contained in:
Konstantin Belousov 2023-07-22 07:24:03 +03:00
parent bc7e86106d
commit 86c7368f2b

View File

@ -106,6 +106,12 @@ static int pagesz; /* page size */
* increasing order.
*/
static union overhead *
cp2op(void *cp)
{
return ((union overhead *)((caddr_t)cp - sizeof(union overhead)));
}
void *
__crt_malloc(size_t nbytes)
{
@ -209,7 +215,7 @@ __crt_free(void *cp)
if (cp == NULL)
return;
op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
op = cp2op(cp);
if (op->ov_magic != MAGIC)
return; /* sanity */
size = op->ov_index;
@ -227,7 +233,7 @@ __crt_realloc(void *cp, size_t nbytes)
if (cp == NULL)
return (__crt_malloc(nbytes));
op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
op = cp2op(cp);
if (op->ov_magic != MAGIC)
return (NULL); /* Double-free or bad argument */
i = op->ov_index;