Skip to content
Snippets Groups Projects
Commit fe067577 authored by Sylvain Henry's avatar Sylvain Henry Committed by Marge Bot
Browse files

Avoid out-of-bound array access in bigNatIsPowerOf2 (fix #24066)

bigNatIndex# in the `where` clause wasn't guarded by "bigNatIsZero a".
parent 13d3c613
No related branches found
No related tags found
No related merge requests found
...@@ -135,13 +135,8 @@ bigNatIsTwo# ba = ...@@ -135,13 +135,8 @@ bigNatIsTwo# ba =
bigNatIsPowerOf2# :: BigNat# -> (# (# #) | Word# #) bigNatIsPowerOf2# :: BigNat# -> (# (# #) | Word# #)
bigNatIsPowerOf2# a bigNatIsPowerOf2# a
| bigNatIsZero a = (# (# #) | #) | bigNatIsZero a = (# (# #) | #)
| True = case wordIsPowerOf2# msw of | True =
(# (# #) | #) -> (# (# #) | #) let
(# | c #) -> case checkAllZeroes (imax -# 1#) of
0# -> (# (# #) | #)
_ -> (# | c `plusWord#`
(int2Word# imax `uncheckedShiftL#` WORD_SIZE_BITS_SHIFT#) #)
where
msw = bigNatIndex# a imax msw = bigNatIndex# a imax
sz = bigNatSize# a sz = bigNatSize# a
imax = sz -# 1# imax = sz -# 1#
...@@ -150,6 +145,12 @@ bigNatIsPowerOf2# a ...@@ -150,6 +145,12 @@ bigNatIsPowerOf2# a
| True = case bigNatIndex# a i of | True = case bigNatIndex# a i of
0## -> checkAllZeroes (i -# 1#) 0## -> checkAllZeroes (i -# 1#)
_ -> 0# _ -> 0#
in case wordIsPowerOf2# msw of
(# (# #) | #) -> (# (# #) | #)
(# | c #) -> case checkAllZeroes (imax -# 1#) of
0# -> (# (# #) | #)
_ -> (# | c `plusWord#`
(int2Word# imax `uncheckedShiftL#` WORD_SIZE_BITS_SHIFT#) #)
-- | Return the Word# at the given index -- | Return the Word# at the given index
bigNatIndex# :: BigNat# -> Int# -> Word# bigNatIndex# :: BigNat# -> Int# -> Word#
......
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
module Main where
import GHC.Num.BigNat
import GHC.Exts
-- just to ensure that (future) rewrite rules don't mess with the test
{-# NOINLINE foo #-}
foo (# #) = bigNatZero# (# #)
main = do
case bigNatIsPowerOf2# (foo (# #)) of
(# _ | #) -> putStrLn "Zero isn't a power of two"
(# | w #) -> putStrLn $ "Zero is 2^" ++ show (W# w)
Zero isn't a power of two
...@@ -81,3 +81,4 @@ test('T20291', normal, compile_and_run, ['']) ...@@ -81,3 +81,4 @@ test('T20291', normal, compile_and_run, [''])
test('T22282', normal, compile_and_run, ['']) test('T22282', normal, compile_and_run, [''])
test('T22671', normal, compile_and_run, ['']) test('T22671', normal, compile_and_run, [''])
test('foundation', [when(js_arch(), run_timeout_multiplier(2))], compile_and_run, ['-O -package transformers']) test('foundation', [when(js_arch(), run_timeout_multiplier(2))], compile_and_run, ['-O -package transformers'])
test('T24066', normal, compile_and_run, [''])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment