Closed Skyb0rg007 closed 1 year ago
MLton patches the SML/NJ Library that it ships to avoid a similar problem in structure HashTableRep
(see https://github.com/MLton/mlton/blob/b1f1f0f0916d28c0d183fba85549d5bf96f1fa41/lib/smlnj-lib/smlnj-lib.patch#L4498-L4510).
I'll resubmit this report there. However I do still believe this should be addressed in the SML/NJ repo, if for nothing but future-proofing against a possible Array.maxLen
increase or Int.maxInt
decrease.
I've pushed a fix that computes floor(log2(Array.maxSize
)) to use as the maxSize
limit for hash tables.
This computation of floor(log2(Array.maxSize
)) is not robust when structure Word = Word32
and structure Int = IntInf
; in general, if the default word size is smaller than the default integer size.
$ cat z.sml
val maxSize = let
fun lp (0w0, k) = Word.toIntX(Word.<<(0w1, k-0w1))
| lp (w, k) = lp (Word.>>(w, 0w1), k+0w1)
in
lp (Word.fromInt Array.maxLen, 0w0)
end
val _ = print (concat ["maxSize: ", Int.toString maxSize, "\n"])
$ mlton -default-type intinf z.sml
$ ./z
maxSize: ~2147483648
Rewrote the code to just use the Int
module, so it should be portable now.
The max-hash-table-size.sml
file was not added in the commit.
I missed it in the initial commit, but I added it about 4 hours ago.
Version
110.99.3 (Latest)
Operating System
OS Version
No response
Processor
System Component
SML/NJ Library
Severity
Major
Description
The
HashSetFn
functor (hash-set-fn.sml
) calculatesmaxSize
as the largest power of 2 that is still less thanArray.maxLen
. However, the calculation does not properly handle systems in whichInt.maxInt = SOME Array.maxLen
, causing an exception to be raised during functor instantiation.Note: This bug does not occur on the SML/NJ compiler, since
Array.maxLen
is much smaller thanInt.maxInt
. The issue is reproducible for MLton (latest version: 20230523-gd082c4a36), since thereSOME Array.maxLen = Int.maxInt
.Transcript
Expected Behavior
Instantiating the
HashSetFn
should never raiseOverflow
.Steps to Reproduce
Just run the code from the transcript.
Additional Information
This can be fixed by modifying the code at line 37 in hash-set-fn.sml:
Email address
ssoss AT uchicago DOT edu