Closed kzmi closed 6 years ago
Currently, ConversionSets.NumercialType and ConversionSets.NumericalSize use cast expression for type conversions like:
ConversionSets.NumercialType
ConversionSets.NumericalSize
new TomlConverter<TomlInt, int>((m, i) => (int)i.Value)
Such conversion leads unwanted result if overflow occurred.
For example,
X = 9999999999999
This TOML value will be parsed as TomlInt (Int64). If this X was mapped to an Int32 field, 1316134911 will be set to the field.
TomlInt
X
Generally, such result would not be acceptable. It is not easy to know whether the value on the field is an overflowed value or not.
This PR replaces cast expression with method of System.Convert. It throws System.OverflowException if overflow will occur in the conversion.
System.Convert
System.OverflowException
I changed conversions Integer-to-Integer and Float-to-Integer. Conversions Integer-to-Float and Float-to-Float still use implicit cast.
System.Convert calls Math.Round() in Float-to-Integer conversion. It may cause minor compatibility issue because the cast expression do truncation.
Math.Round()
Currently,
ConversionSets.NumercialType
andConversionSets.NumericalSize
use cast expression for type conversions like:Such conversion leads unwanted result if overflow occurred.
For example,
This TOML value will be parsed as
TomlInt
(Int64).If this
X
was mapped to an Int32 field, 1316134911 will be set to the field.Generally, such result would not be acceptable.
It is not easy to know whether the value on the field is an overflowed value or not.
This PR replaces cast expression with method of
System.Convert
.It throws
System.OverflowException
if overflow will occur in the conversion.I changed conversions Integer-to-Integer and Float-to-Integer.
Conversions Integer-to-Float and Float-to-Float still use implicit cast.
Note
System.Convert
callsMath.Round()
in Float-to-Integer conversion.It may cause minor compatibility issue because the cast expression do truncation.