Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
array
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sven Tennie
array
Commits
cb2446df
Commit
cb2446df
authored
8 years ago
by
Ben Gamari
Browse files
Options
Downloads
Patches
Plain Diff
Data.Array.Base: Check for overflow in size calculations
Fixes GHC #4505.
parent
bab2c234
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
Data/Array/Base.hs
+30
-14
30 additions, 14 deletions
Data/Array/Base.hs
tests/T229.hs
+9
-0
9 additions, 0 deletions
tests/T229.hs
tests/T229.stderr
+3
-0
3 additions, 0 deletions
tests/T229.stderr
tests/all.T
+1
-1
1 addition, 1 deletion
tests/all.T
with
43 additions
and
15 deletions
Data/Array/Base.hs
+
30
−
14
View file @
cb2446df
...
...
@@ -1065,7 +1065,7 @@ instance MArray (STUArray s) Char (ST s) where
{-# INLINE getNumElements #-}
getNumElements
(
STUArray
_
_
n
_
)
=
return
n
{-# INLINE unsafeNewArray_ #-}
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
*#
4
#
)
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
safe_scale
4
#
)
{-# INLINE newArray_ #-}
newArray_
arrBounds
=
newArray
arrBounds
(
chr
0
)
{-# INLINE unsafeRead #-}
...
...
@@ -1227,7 +1227,7 @@ instance MArray (STUArray s) Int16 (ST s) where
{-# INLINE getNumElements #-}
getNumElements
(
STUArray
_
_
n
_
)
=
return
n
{-# INLINE unsafeNewArray_ #-}
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
*#
2
#
)
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
safe_scale
2
#
)
{-# INLINE newArray_ #-}
newArray_
arrBounds
=
newArray
arrBounds
0
{-# INLINE unsafeRead #-}
...
...
@@ -1245,7 +1245,7 @@ instance MArray (STUArray s) Int32 (ST s) where
{-# INLINE getNumElements #-}
getNumElements
(
STUArray
_
_
n
_
)
=
return
n
{-# INLINE unsafeNewArray_ #-}
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
*#
4
#
)
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
safe_scale
4
#
)
{-# INLINE newArray_ #-}
newArray_
arrBounds
=
newArray
arrBounds
0
{-# INLINE unsafeRead #-}
...
...
@@ -1263,7 +1263,7 @@ instance MArray (STUArray s) Int64 (ST s) where
{-# INLINE getNumElements #-}
getNumElements
(
STUArray
_
_
n
_
)
=
return
n
{-# INLINE unsafeNewArray_ #-}
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
*#
8
#
)
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
safe_scale
8
#
)
{-# INLINE newArray_ #-}
newArray_
arrBounds
=
newArray
arrBounds
0
{-# INLINE unsafeRead #-}
...
...
@@ -1299,7 +1299,7 @@ instance MArray (STUArray s) Word16 (ST s) where
{-# INLINE getNumElements #-}
getNumElements
(
STUArray
_
_
n
_
)
=
return
n
{-# INLINE unsafeNewArray_ #-}
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
*#
2
#
)
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
safe_scale
2
#
)
{-# INLINE newArray_ #-}
newArray_
arrBounds
=
newArray
arrBounds
0
{-# INLINE unsafeRead #-}
...
...
@@ -1317,7 +1317,7 @@ instance MArray (STUArray s) Word32 (ST s) where
{-# INLINE getNumElements #-}
getNumElements
(
STUArray
_
_
n
_
)
=
return
n
{-# INLINE unsafeNewArray_ #-}
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
*#
4
#
)
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
safe_scale
4
#
)
{-# INLINE newArray_ #-}
newArray_
arrBounds
=
newArray
arrBounds
0
{-# INLINE unsafeRead #-}
...
...
@@ -1335,7 +1335,7 @@ instance MArray (STUArray s) Word64 (ST s) where
{-# INLINE getNumElements #-}
getNumElements
(
STUArray
_
_
n
_
)
=
return
n
{-# INLINE unsafeNewArray_ #-}
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
*#
8
#
)
unsafeNewArray_
(
l
,
u
)
=
unsafeNewArraySTUArray_
(
l
,
u
)
(
safe_scale
8
#
)
{-# INLINE newArray_ #-}
newArray_
arrBounds
=
newArray
arrBounds
0
{-# INLINE unsafeRead #-}
...
...
@@ -1352,13 +1352,29 @@ instance MArray (STUArray s) Word64 (ST s) where
bOOL_SCALE
,
bOOL_WORD_SCALE
,
wORD_SCALE
,
dOUBLE_SCALE
,
fLOAT_SCALE
::
Int
#
->
Int
#
bOOL_SCALE
n
#
=
(
n
#
+#
last
#
)
`
uncheckedIShiftRA
#
`
3
#
where
!
(
I
#
last
#
)
=
SIZEOF_HSWORD
*
8
-
1
bOOL_WORD_SCALE
n
#
=
bOOL_INDEX
(
n
#
+#
last
#
)
where
!
(
I
#
last
#
)
=
SIZEOF_HSWORD
*
8
-
1
wORD_SCALE
n
#
=
scale
#
*#
n
#
where
!
(
I
#
scale
#
)
=
SIZEOF_HSWORD
dOUBLE_SCALE
n
#
=
scale
#
*#
n
#
where
!
(
I
#
scale
#
)
=
SIZEOF_HSDOUBLE
fLOAT_SCALE
n
#
=
scale
#
*#
n
#
where
!
(
I
#
scale
#
)
=
SIZEOF_HSFLOAT
bOOL_SCALE
n
#
|
isTrue
#
(
res
#
>#
n
#
)
=
res
#
|
otherwise
=
error
"Data.Array.Base.bOOL_SCALE: Overflow"
where
!
(
I
#
last
#
)
=
SIZEOF_HSWORD
*
8
-
1
!
res
#
=
(
n
#
+#
last
#
)
`
uncheckedIShiftRA
#
`
3
#
bOOL_WORD_SCALE
n
#
|
isTrue
#
(
res
#
>#
n
#
)
=
res
#
|
otherwise
=
error
"Data.Array.Base.bOOL_WORD_SCALE: Overflow"
where
!
(
I
#
last
#
)
=
SIZEOF_HSWORD
*
8
-
1
!
res
#
=
bOOL_INDEX
(
n
#
+#
last
#
)
wORD_SCALE
n
#
=
safe_scale
scale
#
n
#
where
!
(
I
#
scale
#
)
=
SIZEOF_HSWORD
dOUBLE_SCALE
n
#
=
safe_scale
scale
#
n
#
where
!
(
I
#
scale
#
)
=
SIZEOF_HSDOUBLE
fLOAT_SCALE
n
#
=
safe_scale
scale
#
n
#
where
!
(
I
#
scale
#
)
=
SIZEOF_HSFLOAT
safe_scale
::
Int
#
->
Int
#
->
Int
#
safe_scale
scale
#
n
#
|
isTrue
#
(
res
#
>=#
n
#
)
=
res
#
|
otherwise
=
error
"Data.Array.Base.safe_scale: Overflow"
where
!
res
#
=
scale
#
*#
n
#
bOOL_INDEX
::
Int
#
->
Int
#
#
if
SIZEOF_HSWORD
==
4
...
...
This diff is collapsed.
Click to expand it.
tests/T229.hs
0 → 100644
+
9
−
0
View file @
cb2446df
import
Data.Array.MArray
import
Data.Array.IO
import
Data.Word
main
::
IO
()
main
=
do
-- This should fail due to integer overflow
m
<-
newArray_
(
0
,
2
^
62
-
1
)
::
IO
(
IOUArray
Int
Word32
)
-- allocates 0 bytes
readArray
m
17
>>=
print
-- Read some random location in address space
This diff is collapsed.
Click to expand it.
tests/T229.stderr
0 → 100644
+
3
−
0
View file @
cb2446df
T229: Data.Array.Base.safe_scale: Overflow
CallStack (from HasCallStack):
error, called at libraries/array/Data/Array/Base.hs:1374:17 in array-0.5.1.2:Data.Array.Base
This diff is collapsed.
Click to expand it.
tests/all.T
+
1
−
1
View file @
cb2446df
test
('
T2120
',
normal
,
compile_and_run
,
[''])
test
('
largeArray
',
normal
,
compile_and_run
,
[''])
test
('
array001
',
[
...
...
@@ -7,3 +6,4 @@ test('array001', [
compile_and_run
,
[''])
test
('
T9220
',
normal
,
ghci_script
,
['
T9220.script
'])
test
('
T229
',
[
exit_code
(
1
)],
compile_and_run
,
[''])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment