Powershell: The null-coalescing operator evaluates the left-hand operand twice.

Created on 14 May 2020  路  5Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

function A {
    Write-Host "In A"
    return "something"
}

# Here, A is called twice
$y = (A) ?? "blabla"

Expected behavior

The function A should be called only once.

In A

Actual behavior

The function A is called twice.

In A
In A

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Bug Resolution-Fixed WG-Engine

Most helpful comment

/cc @SteveL-MSFT for visibility. Double execution could cause some very dangerous side effects. May be a good candidate for inclusion in a servicing update.

All 5 comments

/cc @adityapatwardhan @rjmholt

/cc @SteveL-MSFT for visibility. Double execution could cause some very dangerous side effects. May be a good candidate for inclusion in a servicing update.

:tada:This issue was addressed in #12667, which has now been successfully released as v7.0.2.:tada:

Handy links:

:tada:This issue was addressed in #12667, which has now been successfully released as v7.1.0-preview.4.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pcgeek86 picture pcgeek86  路  3Comments

garegin16 picture garegin16  路  3Comments

andschwa picture andschwa  路  3Comments

aragula12 picture aragula12  路  3Comments

manofspirit picture manofspirit  路  3Comments