]> git.ipfire.org Git - thirdparty/util-linux.git/commit
c.h: really avoid undefined behavior in SINT_MAX
authorThomas Weißschuh <thomas@t-8ch.de>
Sat, 14 Jan 2023 22:16:28 +0000 (22:16 +0000)
committerThomas Weißschuh <thomas@t-8ch.de>
Sat, 14 Jan 2023 22:24:39 +0000 (22:24 +0000)
commite53463ea40c58a6f84a597966ac69be9be72cd7d
treef5082f29e9a55c8e5b7a99441eeb476b91540629
parentdbf77f7a19e50686324e4fd5bb922411c2ada24e
c.h: really avoid undefined behavior in SINT_MAX

The previous fix in #2013 still invoked undefined behavior by shifting
into the sign-bit.

Now we have a correct, albeit unwieldy solution that avoids undefined
behavior.
Instead of bit fiddling it uses plain addition and substraction.

We are looking for (in LaTeX notation):
  2^(n - 1) - 1
= 2 * 2^(n - 2) - 1
= 2^(n - 2) + 2^(n - 2) - 1
= 2^(n - 2) - 1 + 2^(n - 2)
include/c.h