Skip to content

Invalid binary and octal literals for 64-bit integers are accepted #5729

Closed

Description

@inosik

When using binary or octal literals for 64-bit integers (both signed and unsigned) the compiler doesn't check for overflows and accepts more than 64 bits.

This also applies to LanguagePrimitives.ParseInt64 and LanguagePrimitives.ParseUInt64.

I noticed that after I ported that portion of the code from the F# compiler to an app of mine.

This is the code in the compiler:

https://github.com/Microsoft/visualfsharp/blob/fead0aac540485683f694524eadad79983ec28d9/src/fsharp/lex.fsl#L69-L75

This is the code in FSharp.Core:

https://github.com/Microsoft/visualfsharp/blob/92247b886e4c3f8e637948de84b6d10f97b2b894/src/fsharp/FSharp.Core/prim-types.fs#L2363-L2369

Repro steps

let N = 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1L
// Actually 65 bits
// ---------------------------------------------------------------------------------------^

I prepared a repro project here: repro.zip

Expected behavior

The compiler should emit FS1149 and FS1150 for signed and unsigned long integers, respectively. The ParseInt64 and ParseUInt64 functions should fail with an OverflowException.

Actual behavior

The compiler accepts more than 64 bits in binary and octal literals. The numbers end up being equal to -1 for int64 and UInt64.MaxValue for uint64.

Known workarounds

Use decimal or hexadecimal literals.

Related information

I can reproduce that on Windows 10 with VS 2017 and on macOS with the .NET Core SDK 2.1.402.

Activity

  1. added this to the 16.0 milestone on Oct 17, 2018
  2. modified the milestones: 16.0, 16.1 on Feb 21, 2019
  3. modified the milestones: 16.1, 16.2 on Apr 23, 2019
  4. modified the milestones: 16.2, Backlog on Apr 30, 2019
  5. modified the milestones: Backlog, 16.5 on Jan 17, 2020
  6. added a commit that references this issue on Feb 23, 2021
    2e27225
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions