Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Fix decoding surrogates on error - #36159

Merged
buyaa-n merged 7 commits into
dotnet:masterfrom
buyaa-n:Fix_DecodingSurrogatesOnError
Apr 5, 2019
Merged

buyaa-n merged 7 commits into
dotnet:masterfrom
buyaa-n:Fix_DecodingSurrogatesOnError

Conversation

@buyaa-n

@buyaa-n buyaa-n commented Mar 19, 2019

Copy link
Copy Markdown

Fixes #35073

@buyaa-n
buyaa-n requested a review from krwq March 19, 2019 17:15
@stephentoub

Copy link
Copy Markdown
Member

cc: @GrabYourPitchforks, @tarekgh


// if that's all that was requested -> return
if (charCount == 0)
if (charCount + surrogateCount == 0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems like the entirety of the fix could have been encapsulated by a one-line change:

if (charCount <= 0)

Is it really expected that charCount could ever be negative?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

That would work for this scenario, but as I don't know all possible cases so added this new variable just to avoid from broking anything else.

Is it really expected that charCount could ever be negative?

I assume the person who wrote it wasn't expecting that, but it is becaming negative in case of surrogate chars

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I will update with your suggestion and run tests

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right - I agree with your assessment that I don't think this number was intended to go negative. But with the proposed fix we still allow this to happen. If this behavior is indeed intentional, then a check for negative values should be sufficient to fix the bug. If the number's not intended to go negative, then it seems we need to tweak the fix further so that this condition doesn't occur.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Seems main intention of this condition was only expected number of chars converted successfully. But the creator of this code didn't count surrogate chars case, so we might better to check if (i == 1 || i == 2) instead of if (charCount <= 0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Spoke with @buyaa-n offline. This type subclasses Decoder, but it doesn't properly fulfill the contract of Decoder.Convert and related methods. For example, it sets completed = true in some scenarios even when there's still unconsumed state in the decoder. The existing implementation of this type requires a much deeper investigation than this small handful of changes.

@karelz karelz added this to the 3.0 milestone Apr 1, 2019
@buyaa-n
buyaa-n merged commit 004b31d into dotnet:master Apr 5, 2019

@krwq krwq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@buyaa-n
buyaa-n deleted the Fix_DecodingSurrogatesOnError branch November 1, 2019 20:40
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Thank you @GrabYourPitchforks diggin into it and helping to find better solution

Some test falilures on Linux seems not related merging


Commit migrated from dotnet/corefx@004b31d
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.

6 participants