Skip to content

Commit e0176e1

Browse files
authored
fix null ref in SqlDataReader (dotnet#4159)
1 parent ed1661f commit e0176e1

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2137,7 +2137,7 @@ override public long GetChars(int i, long dataIndex, char[] buffer, int bufferIn
21372137
// if bad buffer index, throw
21382138
if ((bufferIndex < 0) || (buffer != null && bufferIndex >= buffer.Length))
21392139
{
2140-
throw ADP.InvalidDestinationBufferIndex(buffer.Length, bufferIndex, nameof(bufferIndex));
2140+
throw ADP.InvalidDestinationBufferIndex(buffer?.Length ?? 0, bufferIndex, nameof(bufferIndex));
21412141
}
21422142

21432143
// if there is not enough room in the buffer for data

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataReaderTest/DataReaderTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,22 @@ DROP TABLE IF EXISTS [{tableName}]
741741
}
742742
}
743743

744+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
745+
public static async Task GetCharsSequentialAccess_NullBufferNegativeBufferIndex_ThrowsArgumentOutOfRange()
746+
{
747+
using var connection = new SqlConnection(DataTestUtility.TCPConnectionString);
748+
await connection.OpenAsync();
749+
750+
using var command = connection.CreateCommand();
751+
command.CommandText = "SELECT CONVERT(NVARCHAR(MAX), 'test')";
752+
753+
using var reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess);
754+
Assert.True(await reader.ReadAsync());
755+
756+
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => reader.GetChars(0, 0, null, -1, 0));
757+
Assert.Equal("bufferIndex", ex.ParamName);
758+
}
759+
744760
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
745761
public static async Task CanGetCharsSequentially()
746762
{

0 commit comments

Comments
 (0)