This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
[release/3.0] Avoid MemoryMarshal.Cast when transcoding from UTF-16 to UTF-8 while escaping in Utf8JsonWriter. #40997
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
escaping in Utf8JsonWriter.
ericstj
approved these changes
Sep 10, 2019
might not handle null ptrs correctly.
GrabYourPitchforks
approved these changes
Sep 10, 2019
MacOS Build x64_Debug test failures are unrelated: System.Security.Cryptography.OpenSsl.Tests on netcoreapp-OSX-Debug-x64-OSX.1014.Amd64.Open System.Security.Cryptography.OpenSsl.Tests Total: 649, Errors: 0, Failed: 565, Skipped: 14, Time: 1.083s https://github.com/dotnet/corefx/issues/40262 Example test failure:
|
ViktorHofer
pushed a commit
that referenced
this pull request
Sep 13, 2019
* Disable SDL validation (#40903) SDL validation is too expensive to run on a per-build basis. Disable for now * [release/3.0] Update dependencies from dotnet/standard (#40911) * Update dependencies from https://github.com/dotnet/standard build 20190907.2 - NETStandard.Library - 2.1.0-prerelease.19457.2 * Update dependencies from https://github.com/dotnet/standard build 20190907.1 - NETStandard.Library - 2.1.0-prerelease.19457.1 * [release/3.0] Update dependencies from 3 repositories (#40915) * Update dependencies from https://github.com/dotnet/core-setup build 20190907.02 - Microsoft.NETCore.App - 3.0.0-rc2-19457-02 - Microsoft.NETCore.DotNetHost - 3.0.0-rc2-19457-02 - Microsoft.NETCore.DotNetHostPolicy - 3.0.0-rc2-19457-02 * Update dependencies from https://github.com/dotnet/arcade build 20190906.10 - Microsoft.DotNet.XUnitExtensions - 2.4.1-beta.19456.10 - Microsoft.DotNet.XUnitConsoleRunner - 2.5.1-beta.19456.10 - Microsoft.DotNet.VersionTools.Tasks - 1.0.0-beta.19456.10 - Microsoft.DotNet.ApiCompat - 1.0.0-beta.19456.10 - Microsoft.DotNet.Arcade.Sdk - 1.0.0-beta.19456.10 - Microsoft.DotNet.Build.Tasks.Configuration - 1.0.0-beta.19456.10 - Microsoft.DotNet.Build.Tasks.Feed - 2.2.0-beta.19456.10 - Microsoft.DotNet.Build.Tasks.Packaging - 1.0.0-beta.19456.10 - Microsoft.DotNet.CodeAnalysis - 1.0.0-beta.19456.10 - Microsoft.DotNet.CoreFxTesting - 1.0.0-beta.19456.10 - Microsoft.DotNet.GenAPI - 1.0.0-beta.19456.10 - Microsoft.DotNet.GenFacades - 1.0.0-beta.19456.10 - Microsoft.DotNet.Helix.Sdk - 2.0.0-beta.19456.10 - Microsoft.DotNet.RemoteExecutor - 1.0.0-beta.19456.10 * Update dependencies from https://github.com/dotnet/standard build 20190907.5 - NETStandard.Library - 2.1.0-prerelease.19457.5 * Disable ToolboxBitmatAttribute test in netfx (#40901) (#40908) * [release/3.0] Update dependencies from 4 repositories (#40929) * Update dependencies from https://github.com/dotnet/core-setup build 20190907.15 - Microsoft.NETCore.App - 3.0.0-rc2-19457-15 - Microsoft.NETCore.DotNetHost - 3.0.0-rc2-19457-15 - Microsoft.NETCore.DotNetHostPolicy - 3.0.0-rc2-19457-15 * Update dependencies from https://github.com/dotnet/arcade build 20190907.1 - Microsoft.DotNet.XUnitExtensions - 2.4.1-beta.19457.1 - Microsoft.DotNet.XUnitConsoleRunner - 2.5.1-beta.19457.1 - Microsoft.DotNet.VersionTools.Tasks - 1.0.0-beta.19457.1 - Microsoft.DotNet.ApiCompat - 1.0.0-beta.19457.1 - Microsoft.DotNet.Arcade.Sdk - 1.0.0-beta.19457.1 - Microsoft.DotNet.Build.Tasks.Configuration - 1.0.0-beta.19457.1 - Microsoft.DotNet.Build.Tasks.Feed - 2.2.0-beta.19457.1 - Microsoft.DotNet.Build.Tasks.Packaging - 1.0.0-beta.19457.1 - Microsoft.DotNet.CodeAnalysis - 1.0.0-beta.19457.1 - Microsoft.DotNet.CoreFxTesting - 1.0.0-beta.19457.1 - Microsoft.DotNet.GenAPI - 1.0.0-beta.19457.1 - Microsoft.DotNet.GenFacades - 1.0.0-beta.19457.1 - Microsoft.DotNet.Helix.Sdk - 2.0.0-beta.19457.1 - Microsoft.DotNet.RemoteExecutor - 1.0.0-beta.19457.1 * Update dependencies from https://dev.azure.com/dnceng/internal/_git/dotnet-optimization build 20190908.1 - optimization.windows_nt-x64.IBC.CoreFx - 99.99.99-master-20190908.1 * Update dependencies from https://github.com/dotnet/standard build 20190908.3 - NETStandard.Library - 2.1.0-prerelease.19458.3 * [release/3.0] Update dependencies from 4 repositories (#40940) * Update dependencies from https://github.com/dotnet/core-setup build 20190908.11 - Microsoft.NETCore.App - 3.0.0-rc2-19458-11 - Microsoft.NETCore.DotNetHost - 3.0.0-rc2-19458-11 - Microsoft.NETCore.DotNetHostPolicy - 3.0.0-rc2-19458-11 * Update dependencies from https://github.com/dotnet/arcade build 20190908.2 - Microsoft.DotNet.XUnitExtensions - 2.4.1-beta.19458.2 - Microsoft.DotNet.XUnitConsoleRunner - 2.5.1-beta.19458.2 - Microsoft.DotNet.VersionTools.Tasks - 1.0.0-beta.19458.2 - Microsoft.DotNet.ApiCompat - 1.0.0-beta.19458.2 - Microsoft.DotNet.Arcade.Sdk - 1.0.0-beta.19458.2 - Microsoft.DotNet.Build.Tasks.Configuration - 1.0.0-beta.19458.2 - Microsoft.DotNet.Build.Tasks.Feed - 2.2.0-beta.19458.2 - Microsoft.DotNet.Build.Tasks.Packaging - 1.0.0-beta.19458.2 - Microsoft.DotNet.CodeAnalysis - 1.0.0-beta.19458.2 - Microsoft.DotNet.CoreFxTesting - 1.0.0-beta.19458.2 - Microsoft.DotNet.GenAPI - 1.0.0-beta.19458.2 - Microsoft.DotNet.GenFacades - 1.0.0-beta.19458.2 - Microsoft.DotNet.Helix.Sdk - 2.0.0-beta.19458.2 - Microsoft.DotNet.RemoteExecutor - 1.0.0-beta.19458.2 * Update dependencies from https://dev.azure.com/dnceng/internal/_git/dotnet-optimization build 20190909.1 - optimization.windows_nt-x64.IBC.CoreFx - 99.99.99-master-20190909.1 * Update dependencies from https://github.com/dotnet/standard build 20190909.3 - NETStandard.Library - 2.1.0-prerelease.19459.3 * Add missing IAsyncDisposable interfaces to System.Data (#40872) Part of #35012 * Update dependencies from https://github.com/dotnet/coreclr build 20190909.3 (#40956) - Microsoft.NET.Sdk.IL - 3.0.0-rc2.19459.3 - Microsoft.NETCore.ILAsm - 3.0.0-rc2.19459.3 - Microsoft.NETCore.Runtime.CoreCLR - 3.0.0-rc2.19459.3 * Fix TypeConverter for IComponent (#40837) (#40883) * .NET Core 3.0 Prev9 Intellisense nupkg version bump (#40963) (#40965) * [release/3.0] Update dependencies from 4 repositories (#40951) * Update dependencies from https://github.com/dotnet/standard build 20190909.4 - NETStandard.Library - 2.1.0-prerelease.19459.4 * Update dependencies from https://github.com/dotnet/core-setup build 20190909.40 - Microsoft.NETCore.App - 3.0.0-rc2-19459-40 - Microsoft.NETCore.DotNetHost - 3.0.0-rc2-19459-40 - Microsoft.NETCore.DotNetHostPolicy - 3.0.0-rc2-19459-40 * Update dependencies from https://github.com/dotnet/arcade build 20190909.10 - Microsoft.DotNet.XUnitExtensions - 2.4.1-beta.19459.10 - Microsoft.DotNet.XUnitConsoleRunner - 2.5.1-beta.19459.10 - Microsoft.DotNet.VersionTools.Tasks - 1.0.0-beta.19459.10 - Microsoft.DotNet.ApiCompat - 1.0.0-beta.19459.10 - Microsoft.DotNet.Arcade.Sdk - 1.0.0-beta.19459.10 - Microsoft.DotNet.Build.Tasks.Configuration - 1.0.0-beta.19459.10 - Microsoft.DotNet.Build.Tasks.Feed - 2.2.0-beta.19459.10 - Microsoft.DotNet.Build.Tasks.Packaging - 1.0.0-beta.19459.10 - Microsoft.DotNet.CodeAnalysis - 1.0.0-beta.19459.10 - Microsoft.DotNet.CoreFxTesting - 1.0.0-beta.19459.10 - Microsoft.DotNet.GenAPI - 1.0.0-beta.19459.10 - Microsoft.DotNet.GenFacades - 1.0.0-beta.19459.10 - Microsoft.DotNet.Helix.Sdk - 2.0.0-beta.19459.10 - Microsoft.DotNet.RemoteExecutor - 1.0.0-beta.19459.10 * Update dependencies from https://dev.azure.com/dnceng/internal/_git/dotnet-optimization build 20190910.1 - optimization.windows_nt-x64.IBC.CoreFx - 99.99.99-master-20190910.1 * Add test for IComponent typeconverter register in TypeDescriptor (#40959) (#40977) * Update dependencies from https://github.com/dotnet/coreclr build 20190910.2 (#40984) - Microsoft.NET.Sdk.IL - 3.0.0-rc2.19460.2 - Microsoft.NETCore.ILAsm - 3.0.0-rc2.19460.2 - Microsoft.NETCore.Runtime.CoreCLR - 3.0.0-rc2.19460.2 * Update dependencies from https://github.com/dotnet/coreclr build 20190910.4 (#41006) - Microsoft.NET.Sdk.IL - 3.0.0-rc2.19460.4 - Microsoft.NETCore.ILAsm - 3.0.0-rc2.19460.4 - Microsoft.NETCore.Runtime.CoreCLR - 3.0.0-rc2.19460.4 * [release/3.0] Update dependencies from dotnet/arcade dotnet/standard (#40986) * Update dependencies from https://github.com/dotnet/arcade build 20190910.3 - Microsoft.DotNet.XUnitExtensions - 2.4.1-beta.19460.3 - Microsoft.DotNet.XUnitConsoleRunner - 2.5.1-beta.19460.3 - Microsoft.DotNet.VersionTools.Tasks - 1.0.0-beta.19460.3 - Microsoft.DotNet.ApiCompat - 1.0.0-beta.19460.3 - Microsoft.DotNet.Arcade.Sdk - 1.0.0-beta.19460.3 - Microsoft.DotNet.Build.Tasks.Configuration - 1.0.0-beta.19460.3 - Microsoft.DotNet.Build.Tasks.Feed - 2.2.0-beta.19460.3 - Microsoft.DotNet.Build.Tasks.Packaging - 1.0.0-beta.19460.3 - Microsoft.DotNet.CodeAnalysis - 1.0.0-beta.19460.3 - Microsoft.DotNet.CoreFxTesting - 1.0.0-beta.19460.3 - Microsoft.DotNet.GenAPI - 1.0.0-beta.19460.3 - Microsoft.DotNet.GenFacades - 1.0.0-beta.19460.3 - Microsoft.DotNet.Helix.Sdk - 2.0.0-beta.19460.3 - Microsoft.DotNet.RemoteExecutor - 1.0.0-beta.19460.3 * Update dependencies from https://github.com/dotnet/standard build 20190910.4 - NETStandard.Library - 2.1.0-prerelease.19460.4 * Update dependencies from https://github.com/dotnet/standard build 20190910.5 - NETStandard.Library - 2.1.0-prerelease.19460.5 * [release/3.0] Avoid MemoryMarshal.Cast when transcoding from UTF-16 to UTF-8 while escaping in Utf8JsonWriter. (#40997) * Avoid MemoryMarshal.Cast when transcoding from UTF-16 to UTF-8 while escaping in Utf8JsonWriter. * Fix a typo in spacing within the test. * Guard against empty spans where an implementation of JavascriptEncoder might not handle null ptrs correctly. * Cleanup tests to avoid some duplication. * Some more test clean up. * Update dependencies from https://github.com/dotnet/coreclr build 20190910.8 (#41011) - Microsoft.NET.Sdk.IL - 3.0.0-rc2.19460.8 - Microsoft.NETCore.ILAsm - 3.0.0-rc2.19460.8 - Microsoft.NETCore.Runtime.CoreCLR - 3.0.0-rc2.19460.8 * Update dependencies from https://github.com/dotnet/coreclr build 20190910.11 (#41014) - Microsoft.NET.Sdk.IL - 3.0.0-rc2.19460.11 - Microsoft.NETCore.ILAsm - 3.0.0-rc2.19460.11 - Microsoft.NETCore.Runtime.CoreCLR - 3.0.0-rc2.19460.11 * [release/3.0] Update dependencies from 3 repositories (#41022) * Update dependencies from https://github.com/dotnet/core-setup build 20190910.02 - Microsoft.NETCore.App - 3.0.0-rc2-19460-02 - Microsoft.NETCore.DotNetHost - 3.0.0-rc2-19460-02 - Microsoft.NETCore.DotNetHostPolicy - 3.0.0-rc2-19460-02 * Update dependencies from https://dev.azure.com/dnceng/internal/_git/dotnet-optimization build 20190911.1 - optimization.windows_nt-x64.IBC.CoreFx - 99.99.99-master-20190911.1 * Update dependencies from https://github.com/dotnet/standard build 20190911.3 - NETStandard.Library - 2.1.0-prerelease.19461.3 * Update dependencies from https://github.com/dotnet/coreclr build 20190911.3 (#41035) - Microsoft.NET.Sdk.IL - 3.0.0-rc2.19461.3 - Microsoft.NETCore.ILAsm - 3.0.0-rc2.19461.3 - Microsoft.NETCore.Runtime.CoreCLR - 3.0.0-rc2.19461.3 * adding version suffix as non empty for building release package versions * Update dependencies from https://github.com/dotnet/coreclr build 20190911.5 (#41045) - Microsoft.NET.Sdk.IL - 3.0.0-rc2.19461.5 - Microsoft.NETCore.ILAsm - 3.0.0-rc2.19461.5 - Microsoft.NETCore.Runtime.CoreCLR - 3.0.0-rc2.19461.5 * [release/3.0] Update dependencies from 3 repositories (#41052) * Update dependencies from https://github.com/dotnet/arcade build 20190911.7 - Microsoft.DotNet.XUnitExtensions - 2.4.1-beta.19461.7 - Microsoft.DotNet.XUnitConsoleRunner - 2.5.1-beta.19461.7 - Microsoft.DotNet.VersionTools.Tasks - 1.0.0-beta.19461.7 - Microsoft.DotNet.ApiCompat - 1.0.0-beta.19461.7 - Microsoft.DotNet.Arcade.Sdk - 1.0.0-beta.19461.7 - Microsoft.DotNet.Build.Tasks.Configuration - 1.0.0-beta.19461.7 - Microsoft.DotNet.Build.Tasks.Feed - 2.2.0-beta.19461.7 - Microsoft.DotNet.Build.Tasks.Packaging - 1.0.0-beta.19461.7 - Microsoft.DotNet.CodeAnalysis - 1.0.0-beta.19461.7 - Microsoft.DotNet.CoreFxTesting - 1.0.0-beta.19461.7 - Microsoft.DotNet.GenAPI - 1.0.0-beta.19461.7 - Microsoft.DotNet.GenFacades - 1.0.0-beta.19461.7 - Microsoft.DotNet.Helix.Sdk - 2.0.0-beta.19461.7 - Microsoft.DotNet.RemoteExecutor - 1.0.0-beta.19461.7 * Update dependencies from https://dev.azure.com/dnceng/internal/_git/dotnet-optimization build 20190912.1 - optimization.windows_nt-x64.IBC.CoreFx - 99.99.99-master-20190912.1 * Update dependencies from https://github.com/dotnet/standard build 20190912.2 - NETStandard.Library - 2.1.0-prerelease.19462.2 * Update dependencies from https://github.com/dotnet/standard build 20190912.4 - NETStandard.Library - 2.1.0-prerelease.19462.4 * Update dependencies from https://github.com/dotnet/coreclr build 20190912.2 (#41062) - Microsoft.NET.Sdk.IL - 3.0.0-rc2.19462.2 - Microsoft.NETCore.ILAsm - 3.0.0-rc2.19462.2 - Microsoft.NETCore.Runtime.CoreCLR - 3.0.0-rc2.19462.2 * Update dependencies from https://github.com/dotnet/standard build 20190912.5 - NETStandard.Library - 2.1.0 * Stabilize package versions (#41076) * Update dependencies from https://github.com/dotnet/coreclr build 20190912.5 (#41081) - Microsoft.NET.Sdk.IL - 3.0.0-rc2.19462.5 - Microsoft.NETCore.ILAsm - 3.0.0-rc2.19462.5 - Microsoft.NETCore.Runtime.CoreCLR - 3.0.0-rc2.19462.5
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Port of #40996 to fix https://github.com/dotnet/corefx/issues/40979
Description
Instead of using
MemoryMarshal
to re-interpret cast a span of UTF-16 chars to bytes (to pass them to APIs expecting UTF-8 data), call the JavascriptEncoder API that expects UTF-16 chars instead. Casting char to byte doesn't transode it from UTF-16 to UTF-8 which was the previous intention. Doing so results in certain invariants in the code to break since the resulting index that points to the first character to escape would be incorrect (or even out of the bounds of the original span), which results in a negative value being passed in to stackalloc (and hence a stackoverflow). The issue is in the code-path where a custom encoder is passed in (and wouldn't happen by default).Customer Impact
The bug was customer-reported where the user observed a stackoverflow in an ASP.NET WebAPI (which uses a custom encoder) when trying to serialize a string that contained non-ascii characters (for example chinese caracters). Generally, any use of the
JsonSerializer
orUtf8JsonWriter
where a custom encoder is involved for writing .NET strings as JSON is affected. It is imperative that the escaping behavior of the serializer is functionally correct.Regression?
Introduced in .NET Core 3.0 - preview 8
Risk
The risk of this change is around the escaping behavior changing when the user passes in a custom escaper. Be default, the JSON stack uses the default escaper which isn't affected by this change. However, the ASP.NET defaults to a custom escaper so end-users are more likely to be affected by this escaping behavior and fix. There is no easy workaround for the user since passing in a custom/default encoder wouldn't always work either.
Tests run / added
cc @steveharter, @GrabYourPitchforks, @pranavkm, @ericstj