AVRO-3569: [csharp] Fix infinite recursion in AvroDecimal IConvertible conversions - #3951
Open
iemejia wants to merge 1 commit into
Open
AVRO-3569: [csharp] Fix infinite recursion in AvroDecimal IConvertible conversions#3951iemejia wants to merge 1 commit into
iemejia wants to merge 1 commit into
Conversation
…e conversions Every explicit IConvertible.ToXxx(IFormatProvider) implementation on AvroDecimal called Convert.ToXxx(this, provider). Because 'this' is an IConvertible, Convert.ToXxx(IConvertible, provider) calls back into the same IConvertible.ToXxx(provider) method, recursing until a StackOverflowException (which is uncatchable and crashes the process). Reproducer: Convert.ToByte(new AvroDecimal(0)). Route the 12 numeric/boolean conversions through IConvertible.ToType(type, provider) (the one method that performs the real conversion via Convert.ChangeType on the underlying decimal), and make ToString(provider) use the public ToString() override. Adds a test exercising all conversions.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Every explicit
IConvertible.ToXxx(IFormatProvider)implementation onAvroDecimalcalledConvert.ToXxx(this, provider). Sincethisis anIConvertible,Convert.ToXxx(IConvertible, provider)calls straight back into the sameIConvertible.ToXxx(provider)method → unbounded recursion →StackOverflowException, which is uncatchable in .NET and crashes the process.Reproducer (from the issue):
This affected
ToBoolean, ToByte, ToDecimal, ToDouble, ToInt16/32/64, ToSByte, ToSingle, ToUInt16/32/64andToString.Fix
IConvertible.ToType(Type, provider)is the only implementation that does real work (converts the unscaled/scaled value to adecimaland callsConvert.ChangeType). Route the 12 numeric/boolean conversions through it (preserving theIFormatProvider), and makeToString(provider)use the publicToString()override. NoConvert.To*(this, provider)self-calls remain.Tests
Adds
TestAvroDecimalIConvertibleDoesNotRecurseexercising all conversions (each would previously overflow the stack). Full C# suite passes (1528 tests, 0 failures).JIRA: https://issues.apache.org/jira/browse/AVRO-3569