Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Avoid MemoryMarshal.Cast when transcoding from UTF-16 to UTF-8 while escaping in Utf8JsonWriter. #40996

Merged
merged 5 commits into from
Sep 11, 2019

Conversation

ahsonkhan
Copy link

Fixes https://github.com/dotnet/corefx/issues/40979 in master.

This is meant to be a targeted fix to be ported to 3.0.

cc @steveharter, @GrabYourPitchforks, @pranavkm, @ericstj

fixed (char* ptr = value)
{
idx = encoder.FindFirstCharacterToEncode(ptr, value.Length);
}
goto Return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For v5 we may want to add a overload of FindFirstCharacterToEncode(ReadOnlySpan<char) to S.T.Encoding.Web so consumers don't have to use unsafe pinning code.

Copy link
Member

@GrabYourPitchforks GrabYourPitchforks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to address the null check, otherwise LGTM.

idx = encoder.FindFirstCharacterToEncodeUtf8(MemoryMarshal.Cast<char, byte>(value));
fixed (char* ptr = value)
{
idx = encoder.FindFirstCharacterToEncode(ptr, value.Length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some implementations of the FindFirstCharacterToEncode method may not accept null pointers. We should special-case value.IsEmpty at the beginning of this method and bail early.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix this and add a buggy javascriptencoder implementation as a test.


using (var writer = new Utf8JsonWriter(output))
{
writer.WriteStringValue("\u6D4B\u8A6611");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

U+6D4B and U+8A66 might be allowed through unescaped in a future version, which could break this unit test. If you're looking for something stable that's highly unlikely to ever be allowed through unescaped, consider something from the range U+E000..U+F8FF (inclusive). That block is permanently reserved for private use and I highly doubt even the "relaxed" escaper will ever allow those to pass through unescaped.

If the test doesn't rely on this being output escaped or unescaped, you're good to go. :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be allowed through unescaped in a future version

We have a few writer tests that ensure the default behavior escapes certain characters. If we change that behavior, we would have to/should change those tests as well, so I would prefer the tests break at that time.

@ahsonkhan
Copy link
Author

ahsonkhan commented Sep 11, 2019

MacOS Build x64_Debug test failures are unrelated (same as #40997 (comment)):
https://dev.azure.com/dnceng/public/_build/results?buildId=348666&view=ms.vss-test-web.build-test-results-tab

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.072s

https://helix.dot.net/api/2019-06-17/jobs/0dbc5fbf-e89f-423c-953b-735db1747ed7/workitems/System.Security.Cryptography.OpenSsl.Tests/console

@ahsonkhan ahsonkhan merged commit ee9995f into dotnet:master Sep 11, 2019
@ahsonkhan ahsonkhan deleted the FixEscapingStringsInWriter branch September 11, 2019 00:49
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…escaping in Utf8JsonWriter. (dotnet/corefx#40996)

* Avoid MemoryMarshal.Cast when transcoding from UTF-16 to UTF-8 while
escaping in Utf8JsonWriter.

* Fix white space typo in the test expected string.

* 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.


Commit migrated from dotnet/corefx@ee9995f
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[System.Text.Json] UnsafeRelaxedJsonEscaping can result in StackOverflowException
3 participants