alloc-util: introduce MALLOC_SIZEOF_SAFE() helper

It's a wrapper around malloc_usable_size() that is supposed to be
compatible with _FORTIFY_SOURCES=1, by taking the
__builtin_object_size() data into account, the same way as the
_FORTIFY_SOURCES=1 logic does.

Fixes: #19203
This commit is contained in:
Lennart Poettering
2021-05-18 22:27:24 +02:00
parent 871a3a33bb
commit 6df28e1f84
4 changed files with 17 additions and 5 deletions

View File

@@ -805,7 +805,7 @@ int strextendf(char **x, const char *format, ...) {
/* Let's try to use the allocated buffer, if there's room at the end still. Otherwise let's extend by 64 chars. */
if (*x) {
m = strlen(*x);
a = malloc_usable_size(*x);
a = MALLOC_SIZEOF_SAFE(*x);
assert(a >= m + 1);
} else
m = a = 0;
@@ -821,7 +821,7 @@ int strextendf(char **x, const char *format, ...) {
return -ENOMEM;
*x = n;
a = malloc_usable_size(*x);
a = MALLOC_SIZEOF_SAFE(*x);
}
/* Now, let's try to format the string into it */