Re: [PATCH 4/4] config: avoid calling `labs()` on too-large data type
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Jun 13, 2019, 16:13 UTC
- Message-ID
- <xmqqimt9wji2.fsf@gitster-ct.c.googlers.com>
- In-Reply-To
- <9613c88849e53e00e1f7ade49c6c9056309ef0b9.1560426581.git.gitgitgadget@gmail.com>
"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 10 quoted lines
> From: Johannes Schindelin <johannes.schindelin@gmx.de> > > The `labs()` function operates, as the initial `l` suggests, on `long` > parameters. However, in `config.c` we tried to use it on values of type > `intmax_t`. > > This problem was found by GCC v9.x. > > To fix it, let's just "unroll" the function (i.e. negate the value if it > is negative).
Thanks. Obviously correct.
Show 22 quoted lines
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
> config.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/config.c b/config.c
> index 296a6d9cc4..01c6e9df23 100644
> --- a/config.c
> +++ b/config.c
> @@ -869,9 +869,9 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
> errno = EINVAL;
> return 0;
> }
> - uval = labs(val);
> + uval = val < 0 ? -val : val;
> uval *= factor;
> - if (uval > max || labs(val) > uval) {
> + if (uval > max || (val < 0 ? -val : val) > uval) {
> errno = ERANGE;
> return 0;
> }