git: 86c7368f2bce - main - rtld_malloc: add cp2op() helper

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 26 Jul 2023 14:30:15 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=86c7368f2bcefeb257f7dc40e4296261b295a674

commit 86c7368f2bcefeb257f7dc40e4296261b295a674
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-07-22 04:24:03 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-07-26 14:24:18 +0000

    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
---
 libexec/rtld-elf/rtld_malloc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/libexec/rtld-elf/rtld_malloc.c b/libexec/rtld-elf/rtld_malloc.c
index 45d3f743e348..3437f638d339 100644
--- a/libexec/rtld-elf/rtld_malloc.c
+++ b/libexec/rtld-elf/rtld_malloc.c
@@ -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;