From 58a989198ca46388bddb40cf83d13a35bb3d8244 Mon Sep 17 00:00:00 2001 From: midhun Date: Tue, 1 Apr 2025 12:35:31 +0000 Subject: [PATCH 1/2] Fix crash when Data Source starts with a comma --- .../src/Microsoft/Data/Common/AdapterUtil.cs | 15 +++++++++++++-- .../tests/FunctionalTests/SqlConnectionTest.cs | 12 ++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs index da8b65f837..f51723ace2 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs @@ -792,8 +792,19 @@ private static bool IsEndpoint(string dataSource, string prefix) length = foundIndex; } - // check for the instance name - foundIndex = dataSource.LastIndexOf('\\', length - 1, length - 1); + // Safeguard LastIndexOf call to avoid ArgumentOutOfRangeException when length is 0 + if (length > 0) + { + // check for the instance name + foundIndex = dataSource.LastIndexOf('\\', length - 1, length - 1); + } + else + { + foundIndex = -1; + } + + + if (foundIndex > 0) { length = foundIndex; diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionTest.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionTest.cs index 75fb85b384..75b19260d7 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionTest.cs @@ -1082,6 +1082,18 @@ public void ConnectionRetryForNonAzureEndpoints() Assert.Equal(1, (int)field.GetValue(cn)); } + + + [Fact] + public void ConnectionString_WithOnlyComma_ShouldNotThrow() + { + SqlConnection cn = new SqlConnection("Data Source=,;Initial Catalog=master;Integrated Security=True"); + Exception ex = Record.Exception(() => cn.Open()); + + Assert.NotNull(ex); + Assert.False(ex is ArgumentOutOfRangeException); + } + [Theory] [InlineData("myserver.database.windows.net")] [InlineData("myserver.database.cloudapi.de")] From ccbf5109823c14d457859291fa5ca4e1db8d0f32 Mon Sep 17 00:00:00 2001 From: midhun Date: Tue, 1 Apr 2025 19:30:02 +0000 Subject: [PATCH 2/2] Refine test to expect SqlException --- .../tests/FunctionalTests/SqlConnectionTest.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionTest.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionTest.cs index 75b19260d7..d3b906c6b5 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionTest.cs @@ -1085,13 +1085,15 @@ public void ConnectionRetryForNonAzureEndpoints() [Fact] - public void ConnectionString_WithOnlyComma_ShouldNotThrow() + public void ConnectionString_WithOnlyComma() { + // Test Case for https://github.com/dotnet/SqlClient/issues/3110 + // Validates that a single-comma Data Source (e.g., "Data Source=,") no longer causes ArgumentOutOfRangeException + // Instead, it should throw a SqlException indicating a connection failure + SqlConnection cn = new SqlConnection("Data Source=,;Initial Catalog=master;Integrated Security=True"); - Exception ex = Record.Exception(() => cn.Open()); + Assert.Throws(() => { cn.Open(); }); - Assert.NotNull(ex); - Assert.False(ex is ArgumentOutOfRangeException); } [Theory]