Re: [hackers] [libgrapheme] Add reallocarray implementation || robert

From: Mattias Andrée <maandree_AT_kth.se>
Date: Sun, 31 Jul 2022 12:05:20 +0200

On Sun, 31 Jul 2022 11:47:40 +0200
<git_AT_suckless.org> wrote:

> commit bdf42537c5792f6beb0360517ff378834cfd8a68
> Author: robert <robertrussell.72001_AT_gmail.com>
> AuthorDate: Sat Jul 30 14:29:05 2022 -0700
> Commit: Laslo Hunhold <dev_AT_frign.de>
> CommitDate: Sun Jul 31 11:41:08 2022 +0200
>
> Add reallocarray implementation
>
> reallocarray is nonstandard and glibc declares it only when _GNU_SOURCE
> is defined. Without this patch or _GNU_SOURCE (for glibc < 2.29) defined,
> you get a segfault from reallocarray being implicitly declared with the
> wrong signature.
>
> Signed-off-by: Laslo Hunhold <dev_AT_frign.de>
>
> diff --git a/gen/util.c b/gen/util.c
> index d234ddd..c97a1ea 100644
> --- a/gen/util.c
> +++ b/gen/util.c
> _AT_@ -31,6 +31,16 @@ struct break_test_payload
> size_t *testlen;
> };
>
> +static void *
> +reallocarray(void *p, size_t len, size_t size)
> +{
> + if (len > 0 && size > SIZE_MAX/len) {

I think

        if (size && len > SIZE_MAX / size) {

would be a little nicer, the compiler has a better chance of optimising
it to simply `len > SOME_CONSTANT` (remove the if-statement completely)
would `size` be 0) and I think it is also more intuitive as you think
in terms of if the memory can fit enough elements, not if it can fit
large enough elements.

> + errno = ENOMEM;
> + return NULL;
> + }
> + return realloc(p, len*size);
> +}
> +
> int
> hextocp(const char *str, size_t len, uint_least32_t *cp)
> {
>
Received on Sun Jul 31 2022 - 12:05:20 CEST

This archive was generated by hypermail 2.3.0 : Sun Jul 31 2022 - 12:48:35 CEST