Skip to content

Bug in MonoBufferFn #608

@dhansel

Description

@dhansel

I believe there is a bug in the MonoBufferFn implementation in the smlnj-lib library in the current version (and the 20241230 release) of MLton.

The bug can be triggered by calling Random.toString (Random.rand (0,0)) (or any other initial seeds).
Random.toString uses a Word8Buffer (an instance of MonoBufferFn) to construct its result.
After a few seconds this call produces an "Uncaught exception: Overflow".

The bug appears to have been introduced when switching to SML/NJ 110.99.6.1 libraries. The ensureCapacity() function in the MonoBufferFn functor adds an extra line

+ val amt = Int.max(curCap - len + curCap, amt)

that was not present in the smlnj-lib.patch file for the SML/NJ 110.99.3 libraries. If I see it correctly, that line doubles the size of the array each time ensureCapacity() is called, which after some calls leads to the Overflow condition (in that same line).

I assume the intent of the new code was to avoid the (worst-case) scenario where only a single byte of buffer space is allocated at a time (by doubling the buffer size when more space is needed). But as is the array size gets doubled for every call to ensureCapacity().

David

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions