Skip to content

Array and string slicing throw when the finish bound is near Int32.MinValue #20530

Description

@xperiandri

Tolerant slicing (RFC FS-1077) returns an empty result when finish < start. On arrays and strings the length finish - start + 1 overflows when finish is close to Int32.MinValue, and the slice throws instead.

let m = System.Int32.MinValue
[| 1..5 |][3..m]                  // OutOfMemoryException
(Array2D.init 5 5 (+))[3..m, *]   // OutOfMemoryException
"hello"[3..m]                     // ArgumentOutOfRangeException
[ 1..5 ][3..m]                    // [], lists are correct
[| 1..5 |][3..(m + 10)]           // [||], no overflow

Expected: an empty array or string, as for lists.

Cause: GetArraySlice passes finish - start + 1 to GetArraySub. With start = 3 and finish = Int32.MinValue the result wraps to a large positive length. GetStringSlice and the 2D–4D slicers use the same arithmetic. Comparing finish < start before subtracting avoids it: after ComputeSlice, finish - start + 1 cannot overflow when finish >= start.

Found while checking the bound arithmetic for RFC FS-1351 (fsharp/fslang-design#849). SDK 11.0.100-rc.1.26420.103.

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions