Skip to content

Pwsh crash on JsonReaderException #11964

Closed

Description

@anmenaga

Steps to reproduce

$ConfigPath = New-TemporaryFile
$mypath = Join-Path $env:SystemDrive MyDir
'{ "PSModulePath": "'+$mypath+'" }' | Out-File -Force $ConfigPath
pwsh -NoProfile -NonInteractive -settingsFile $ConfigPath -c "1+1"

Expected behavior

Should not crash the process.

Actual behavior

Process terminated. Bad JSON escape sequence: \M. Path 'PSModulePath', line 1, position 23.
   at System.Environment.FailFast(System.String, System.Exception)
   at Microsoft.PowerShell.UnmanagedPSEntry.Start(System.String, System.String[], Int32)
   at Microsoft.PowerShell.ManagedPSEntry.Main(System.String[])
Newtonsoft.Json.JsonReaderException: Bad JSON escape sequence: \M. Path 'PSModulePath', line 1, position 23.
   at Newtonsoft.Json.JsonTextReader.ReadStringIntoBuffer(Char quote)
   at Newtonsoft.Json.JsonTextReader.ParseValue()
   at Newtonsoft.Json.JsonTextReader.Read()
   at Newtonsoft.Json.JsonWriter.WriteToken(JsonReader reader, Boolean writeChildren, Boolean writeDateConstructorAsDate, Boolean writeComments)
   at Newtonsoft.Json.Linq.JTokenWriter.WriteToken(JsonReader reader, Boolean writeChildren, Boolean writeDateConstructorAsDate, Boolean writeComments)
   at Newtonsoft.Json.JsonWriter.WriteToken(JsonReader reader, Boolean writeChildren)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateJToken(JsonReader reader, JsonContract contract)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)
   at System.Management.Automation.Configuration.PowerShellConfig.ReadValueFromFile[T](ConfigScope scope, String key, T defaultValue)
   at System.Management.Automation.Configuration.PowerShellConfig.GetPowerShellPolicies(ConfigScope scope)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Management.Automation.Utils.GetPolicySettingFromConfigFile[T](ConfigScope[] preferenceOrder)
   at System.Management.Automation.Utils.GetPolicySetting[T](ConfigScope[] preferenceOrder)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.GetModuleLoggingInformation(IEnumerable`1& moduleNames)
   at System.Management.Automation.PSSnapInReader.SetSnapInLoggingInformation(PSSnapInInfo psSnapInInfo)
   at System.Management.Automation.PSSnapInReader.ReadCoreEngineSnapIn()
   at System.Management.Automation.Runspaces.InitialSessionState.ImportCorePSSnapIn()
   at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2()
   at Microsoft.PowerShell.UnmanagedPSEntry.Start(String consoleFilePath, String[] args, Int32 argc)

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-rc.3
PSEdition                      Core
GitCommitId                    7.0.0-rc.3
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Feb 26, 2020
  2. vexx32 commented on Feb 26, 2020

    @vexx32
    Collaborator

    I wonder if that's an issue with the file encoding?

    What's the default encoding for Out-File in this context? And what encoding is pwsh expecting on startup?

  3. rjmholt commented on Feb 26, 2020

    @rjmholt
    Collaborator

    Is this because the backslashes in the PSModulePath aren't escaped?

  4. rjmholt commented on Feb 26, 2020

    @rjmholt
    Collaborator

    "C:\MyDir" isn't a valid JSON string; it needs to be "C:\\MyDir"

  5. rjmholt commented on Feb 26, 2020

    @rjmholt
    Collaborator

    But we should fix the crash

  6. rjmholt commented on Feb 26, 2020

    @rjmholt
    Collaborator

    (As in, PowerShell shouldn't crash quite as hard)

  7. daxian-dbw commented on Feb 26, 2020

    @daxian-dbw
    Member

    I remember talking to Travis Plunk (@TravisEz13) about the crash when the setting.json file is bad, and we believe pwsh should terminate in that case for good. But it definitely should be terminated in a more graceful way :)

  8. added
    WG-Enginecore PowerShell engine, interpreter, and runtime
    Issue-Enhancementthe issue is more of a feature request than a bug
    and removed
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Feb 28, 2020
  9. adityapatwardhan commented on Aug 20, 2020

    @adityapatwardhan
    Member

    We should get a better error message about shutting down as the settings file is corrupted and we cannot determined the security policies in the settings file.

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

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions