Re: [hackers] [libgrapheme] Use (size_t)(-1) instead of SIZE_MAX and fix style || Laslo Hunhold

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

Why wouldn't SIZE_MAX be the maximum of size_t?

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

> commit 25d89e6e460e68329e7a3f388fe3e150a8f5474a
> Author: Laslo Hunhold <dev_AT_frign.de>
> AuthorDate: Sun Jul 31 11:46:48 2022 +0200
> Commit: Laslo Hunhold <dev_AT_frign.de>
> CommitDate: Sun Jul 31 11:46:48 2022 +0200
>
> Use (size_t)(-1) instead of SIZE_MAX and fix style
>
> SIZE_MAX cannot be relied upon to fully reflect size_t's size. A
> portable idiom is to simply cast -1 to size_t to get the type's maximum
> value.
>
> Signed-off-by: Laslo Hunhold <dev_AT_frign.de>
>
> diff --git a/gen/util.c b/gen/util.c
> index cefcee7..bfe0dbf 100644
> --- a/gen/util.c
> +++ b/gen/util.c
> _AT_@ -34,11 +34,12 @@ struct break_test_payload
> static void *
> reallocate_array(void *p, size_t len, size_t size)
> {
> - if (len > 0 && size > SIZE_MAX/len) {
> + if (len > 0 && size > (size_t)(-1) / len) {
> errno = ENOMEM;
> return NULL;
> }
> - return realloc(p, len*size);
> +
> + return realloc(p, len * size);
> }
>
> int
>
Received on Sun Jul 31 2022 - 12:18:22 CEST

This archive was generated by hypermail 2.3.0 : Sun Jul 31 2022 - 13:00:36 CEST