-
Notifications
You must be signed in to change notification settings - Fork 341
Fix precision of rescaled zero decimal parameters #4721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
saurabh500
merged 4 commits into
main
from
dev/saurabh/issue-4715-argumentexception-after-updating-microso-f33eb7
Sep 22, 2026
+268
−6
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7fede26
Fix precision of rescaled zero decimal parameters
saurabh500 b737a4d
Reduce redundant async decimal integration cases
saurabh500 39242a4
Handle zero in shared decimal RPC precision validation
saurabh500 c0f34ab
Remove decimal zero documentation additions
saurabh500 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
156 changes: 156 additions & 0 deletions
156
src/Microsoft.Data.SqlClient/tests/UnitTests/TdsParserDecimalTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Data; | ||
| using System.Data.SqlTypes; | ||
| using System.Globalization; | ||
| using System.Reflection; | ||
| using Microsoft.Data.SqlClient.Tests.Common; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.UnitTests | ||
| { | ||
| /// <summary> | ||
| /// Verifies decimal scale adjustment and RPC precision validation preserve representable values. | ||
| /// </summary> | ||
| [Collection(AppContextSwitchTestCollection.Name)] | ||
| public class TdsParserDecimalTests | ||
| { | ||
| /// <summary> | ||
| /// CLR and SQL zero must serialize when precision equals scale, regardless of input scale or sign. | ||
| /// </summary> | ||
| [Theory] | ||
| [InlineData(false, false)] | ||
| [InlineData(false, true)] | ||
| [InlineData(true, false)] | ||
| [InlineData(true, true)] | ||
| public void RpcDecimalParameter_ZeroFitsPrecisionEqualToScale(bool isSqlDecimal, bool truncate) | ||
| { | ||
| using LocalAppContextSwitchesHelper switches = new(); | ||
| switches.TruncateScaledDecimal = truncate; | ||
|
|
||
| for (byte oldScale = 0; oldScale <= 28; oldScale++) | ||
| { | ||
| foreach (bool negative in new[] { false, true }) | ||
| { | ||
| decimal zero = new(0, 0, 0, negative, oldScale); | ||
| object value = isSqlDecimal ? (object)new SqlDecimal(zero) : zero; | ||
| for (byte scale = 1; scale <= 38; scale++) | ||
| { | ||
| WriteDecimalParameter(value, scale, scale); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Excess SqlDecimal precision must not reject signed zero or a value that rounds/truncates to zero. | ||
| /// </summary> | ||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public void RpcDecimalParameter_ZeroWithExcessPrecisionFits(bool negative) | ||
| { | ||
| using LocalAppContextSwitchesHelper switches = new(); | ||
| foreach (bool truncate in new[] { false, true }) | ||
| { | ||
| switches.TruncateScaledDecimal = truncate; | ||
| WriteDecimalParameter(new SqlDecimal(38, 3, !negative, 0, 0, 0, 0), 3, 3); | ||
| WriteDecimalParameter(SqlDecimal.ConvertToPrecScale( | ||
| new SqlDecimal(new decimal(0, 0, 0, negative, 3)), 38, 3), 3, 3); | ||
| WriteDecimalParameter(new SqlDecimal(38, 4, !negative, 1, 0, 0, 0), 3, 3); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Exempting zero must not permit nonzero CLR or SQL values with insufficient parameter precision. | ||
| /// </summary> | ||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public void RpcDecimalParameter_NonzeroExceedingPrecisionThrows(bool isSqlDecimal) | ||
| { | ||
| foreach (decimal value in new[] { -1m, 1m }) | ||
| { | ||
| object parameterValue = isSqlDecimal ? (object)new SqlDecimal(value) : value; | ||
| TargetInvocationException exception = Assert.Throws<TargetInvocationException>( | ||
| () => WriteDecimalParameter(parameterValue, 3, 3)); | ||
| Assert.IsType<ArgumentException>(exception.InnerException); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Scale adjustment must retain the rounding switch and precision needed for nonzero values. | ||
| /// </summary> | ||
| [Theory] | ||
| [InlineData("0.001", 3, "0.001", "0.001", 3)] | ||
| [InlineData("0.01", 3, "0.010", "0.010", 3)] | ||
| [InlineData("1", 3, "1.000", "1.000", 4)] | ||
| [InlineData("-1", 3, "-1.000", "-1.000", 4)] | ||
| [InlineData("1.005", 2, "1.01", "1.00", 3)] | ||
| [InlineData("-1.005", 2, "-1.01", "-1.00", 3)] | ||
| public void AdjustDecimalScale_NonzeroPreservesValueAndPrecision( | ||
| string value, int scale, string rounded, string truncated, int precision) | ||
| { | ||
| using LocalAppContextSwitchesHelper switches = new(); | ||
| foreach (bool truncate in new[] { false, true }) | ||
| { | ||
| switches.TruncateScaledDecimal = truncate; | ||
| SqlDecimal adjusted = TdsParser.AdjustDecimalScale( | ||
| decimal.Parse(value, CultureInfo.InvariantCulture), scale); | ||
|
|
||
| Assert.Equal(truncate ? truncated : rounded, adjusted.ToString()); | ||
| Assert.Equal(scale, adjusted.Scale); | ||
| Assert.Equal(precision, adjusted.Precision); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Rescaling the CLR decimal limits must not reintroduce the overflow fixed by PR #4443. | ||
| /// </summary> | ||
| [Theory] | ||
| [InlineData(2)] | ||
| [InlineData(9)] | ||
| public void AdjustDecimalScale_LargeValuesRemainSqlDecimal(int scale) | ||
| { | ||
| foreach (decimal value in new[] { decimal.MinValue, decimal.MaxValue }) | ||
| { | ||
| SqlDecimal adjusted = TdsParser.AdjustDecimalScale(value, scale); | ||
|
|
||
| Assert.Equal(value.ToString(CultureInfo.InvariantCulture) + "." + new string('0', scale), adjusted.ToString()); | ||
| Assert.Equal(scale, adjusted.Scale); | ||
| Assert.Equal(29 + scale, adjusted.Precision); | ||
| Assert.Throws<OverflowException>(() => adjusted.Value); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Serializes one RPC parameter into a fresh parser buffer without connecting to a server. | ||
| /// </summary> | ||
| /// <param name="value">The CLR decimal or SqlDecimal parameter value.</param> | ||
| /// <param name="precision">The declared parameter precision.</param> | ||
| /// <param name="scale">The declared parameter scale.</param> | ||
| private static void WriteDecimalParameter(object value, byte precision, byte scale) | ||
| { | ||
| const BindingFlags Flags = BindingFlags.Instance | BindingFlags.NonPublic; | ||
| TdsParser parser = new(false, false); | ||
| object state = typeof(TdsParser).GetField("_physicalStateObj", Flags)!.GetValue(parser)!; | ||
| SqlParameter parameter = new("@Value", SqlDbType.Decimal) | ||
| { | ||
| Value = value, | ||
| Precision = precision, | ||
| Scale = scale | ||
| }; | ||
| parameter.Validate(0, false); | ||
|
|
||
| using SqlCommand command = new(); | ||
| MethodInfo write = typeof(TdsParser).GetMethod("TDSExecuteRPCAddParameter", Flags)!; | ||
| Assert.Null(write.Invoke(parser, new object[] | ||
| { | ||
| state, parameter, parameter.InternalMetaType, (byte)0, command, false | ||
| })); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.