skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit 038082c425c40037a28111934dfb5037edbcad8c
parent 20568ed2f00772e0a927ed21b7aa437c92ab8707
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Wed, 29 Apr 2020 19:08:19 +0000

 Fix alloc_realloc UB

 void ** does not exist: the address of a generic pointer is not
properly defined (different pointer types may have different
representations). So, alloc_realloc cannot exist as is without UB.
Fortunately, it's not supposed to be used in the skalibs programming
style, and skalibs itself only uses it in two places
(stralloc_ready_tuned and stralloc_shrink) where the pointer is a
char *.
 So we just fix the UB by making alloc_realloc() take a char **,
and it's only defined for that pointer type.
 Nothing to see here folks, nothing happened at all.

Diffstat:
Msrc/include/skalibs/alloc.h | 2+-
Msrc/libstddjb/alloc_realloc.c | 4++--
Msrc/libstddjb/stralloc_ready_tuned.c | 2+-
Msrc/libstddjb/stralloc_shrink.c | 2+-
4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/include/skalibs/alloc.h b/src/include/skalibs/alloc.h @@ -10,6 +10,6 @@ extern void *alloc (size_t) ; #define alloc_free(p) free(p) #define alloc_re(p, old, new) alloc_realloc(p, new) -extern int alloc_realloc (void **, size_t) ; +extern int alloc_realloc (char **, size_t) ; #endif diff --git a/src/libstddjb/alloc_realloc.c b/src/libstddjb/alloc_realloc.c @@ -3,9 +3,9 @@ #include <stdlib.h> #include <skalibs/alloc.h> -int alloc_realloc (void **x, size_t n) +int alloc_realloc (char **x, size_t n) { - void *y = n ? realloc(*x, n) : (free(*x), alloc(0)) ; + char *y = n ? (char *)realloc(*x, n) : (free(*x), (char *)alloc(0)) ; if (!y) return 0 ; *x = y ; return 1 ; diff --git a/src/libstddjb/stralloc_ready_tuned.c b/src/libstddjb/stralloc_ready_tuned.c @@ -19,7 +19,7 @@ int stralloc_ready_tuned (stralloc *sa, size_t n, size_t base, size_t a, size_t } else if (n > sa->a) { - if (!alloc_re((void **)&sa->s, sa->a, t)) return 0 ; + if (!alloc_re(&sa->s, sa->a, t)) return 0 ; sa->a = t ; } return 1 ; diff --git a/src/libstddjb/stralloc_shrink.c b/src/libstddjb/stralloc_shrink.c @@ -7,7 +7,7 @@ int stralloc_shrink (stralloc *sa) { if (sa->a > sa->len) { - if (!alloc_re((void **)&sa->s, sa->a, sa->len)) return 0 ; + if (!alloc_re(&sa->s, sa->a, sa->len)) return 0 ; sa->a = sa->len ; } return 1 ;