Skip to content

PowerShell v7 (7.5.4) mis-handles [Int128] overflow #26677

@mrenton999

Description

@mrenton999

Prerequisites

Steps to reproduce

For [Int32] and [Int64] types, overflow is promoted to [Double] (arguably, should be promoted to longer Int types as [Int16] and [Byte] are).
But for [Int128], overflow results in wrong numbers with no type conversion and no overflow error thrown (arguably should be promoted to [BigInt]).
Positive [Int128] overflows to erroneous result (sometimes less than [Int128]::MaxValue, sometimes negative).
Negative [Int128] sometimes overflows to zero, sometimes to [Int128]::MinValue.
("Easy" fix: promote to [Double] as [Int32] and [Int64].)
Demo code:

$PSVersionTable
 [Byte]::MaxValue+[Byte]::MaxValue
([Byte]::MaxValue+[Byte]::MaxValue).GetType().Name
 [Int16]::MaxValue+[Int16]::MaxValue
([Int16]::MaxValue+[Int16]::MaxValue).GetType().Name
 [Int32]::MaxValue+[Int32]::MaxValue
([Int32]::MaxValue+[Int32]::MaxValue).GetType().Name
 [Int64]::MaxValue+[Int64]::MaxValue
([Int64]::MaxValue+[Int64]::MaxValue).GetType().Name
 [Int128]::MaxValue+[Int128]::MaxValue
([Int128]::MaxValue+[Int128]::MaxValue).GetType().Name
[Int128]::MaxValue*10
[Int128]::MaxValue
([Int128]::MaxValue/2)*10
[Int128]::MinValue+[Int128]::MinValue
[Int128]::MinValue
[Int128]::MinValue*5
[Int128]::MinValue*-5
[Int128]::MinValue*-10
Image

Expected behavior

[Int128] overflow converted to [Double] or [BigInt] or overflow error thrown.

Actual behavior

Incorrect mathematical result is returned for [Int128] overflow.

Error details

N/A

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.4
PSEdition                      Core
GitCommitId                    7.5.4
OS                             Microsoft Windows 10.0.26200
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1, 6.0, 7.0}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

(Screenshot in "Steps to reproduce" section.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Enginecore PowerShell engine, interpreter, and runtimeWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type
    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