Skip to content

HashSetFn's calculation of maxSize can cause Overflow during functor instantiation #279

Description

@Skyb0rg007

Version

110.99.3 (Latest)

Operating System

  • Any
  • Linux
  • macOS
  • Windows
  • Other Unix

OS Version

No response

Processor

  • Any
  • Arm (using Rosetta)
  • PowerPC
  • Sparc
  • x86 (32-bit)
  • x86-64 (64-bit)
  • Other

System Component

SML/NJ Library

Severity

Major

Description

The HashSetFn functor (hash-set-fn.sml) calculates maxSize as the largest power of 2 that is still less than Array.maxLen.
However, the calculation does not properly handle systems in which Int.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 than Int.maxInt.
The issue is reproducible for MLton (latest version: 20230523-gd082c4a36), since there SOME Array.maxLen = Int.maxInt.

Transcript

(* test.sml *)
structure X = HashSetFn(
   struct
      type hash_key = int
      val hashVal = Word.fromInt
      val sameKey: int * int -> bool = op =
   end)
val () = print "Okay!\n"


(* test.mlb *)
$(SML_LIB)/basis/basis.mlb
$(SML_LIB)/smlnj-lib/Util/smlnj-lib.mlb
test.sml


$ mlton -output a.out test.mlb
$ ./a.out
unhandled exception: Overflow


$ # The `-default-type intinf` flag causes Int.int to have infinite precision, but keeps Array.maxLen the same
$ mlton -output a.out -default-type intinf test.mlb
$ ./a.out
Okay!

Expected Behavior

Instantiating the HashSetFn should never raise Overflow.

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:

(* old *)   
val maxSize = let
     fun f i = let
             val i' = i+i
             in
               if i' < Array.maxLen then f i' else i
             end
     in
       f 0x10000
     end

(* new*)   
val maxSize = let
     fun f i = let
             val i' = i+i
             in
               if i' < Array.maxLen then f i' else i
             end
             handle Overflow => i
     in
       f 0x10000
     end

Email address

ssoss AT uchicago DOT edu

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingfixed-in-110.99.4issues that will be fixed in the 110.99.4 versionsmlnj-libGeneral issue with the SML/NJ Library or the Util component

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions