[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Undefined behavior in conv-integer.i.c?

From: Miroslav Lichvar
Subject: Undefined behavior in conv-integer.i.c?
Date: Wed, 17 Feb 2016 17:16:39 +0100
User-agent: Mutt/1.5.24 (2015-08-30)


I was looking at a problem with guile-1.8.8 when compiled with
gcc-6.0. Two of the tests from the test suite were failing with
strange "out of range" errors [1]. After some investigation I think
the bug is that the code in libguile/conv-integer.i.c relies on
overflow of signed integers in the following code (starting on line
77), specifically -TYPE_MIN being less than zero. Adding -fwrapv to
CFLAGS worked as a workaround for me.

          if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0)
              if (n < 0)
                goto out_of_range;
              n = -n;
              if (n >= 0)
                goto out_of_range;

Looking at the current guile code, conv-integer.i.c is identical to
what it was in 1.8.8, but interestingly the tests didn't fail for me.
Maybe something else is preventing gcc from using the optimization?

I'm not sure what would be the best way to fix it. Maybe n should
really be unsigned and compared to the maximum values, but what would
be the absolute value of TYPE_MIN if it should work also with other
integer representations than two's complement?


Miroslav Lichvar

reply via email to

[Prev in Thread] Current Thread [Next in Thread]