Skip to content

Commit e0ea838

Browse files
committed
Fix NullReferenceException when Connection isn't set.
IsValid (which checks preconditions) is called after ResetCommandTimeout.
1 parent ecf1769 commit e0ea838

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ internal IDisposable RegisterCancel(CancellationToken token)
226226
internal void ResetCommandTimeout()
227227
{
228228
var commandTimeout = CommandTimeout;
229-
Connection.Session.SetTimeout(commandTimeout == 0 ? Constants.InfiniteTimeout : commandTimeout * 1000);
229+
Connection?.Session?.SetTimeout(commandTimeout == 0 ? Constants.InfiniteTimeout : commandTimeout * 1000);
230230
}
231231

232232
private void VerifyNotDisposed()

tests/SideBySide/CommandTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using MySql.Data.MySqlClient;
3+
using Xunit;
4+
5+
namespace SideBySide
6+
{
7+
public class CommandTests : IClassFixture<DatabaseFixture>
8+
{
9+
public CommandTests(DatabaseFixture database)
10+
{
11+
m_database = database;
12+
}
13+
14+
[Fact]
15+
public void CreateCommandSetsConnection()
16+
{
17+
using (var command = m_database.Connection.CreateCommand())
18+
{
19+
Assert.Equal(m_database.Connection, command.Connection);
20+
}
21+
}
22+
23+
[Fact]
24+
public void ExecuteReaderRequiresConnection()
25+
{
26+
using (var command = new MySqlCommand())
27+
{
28+
Assert.Throws<InvalidOperationException>(() => command.ExecuteReader());
29+
}
30+
}
31+
32+
[Fact]
33+
public void ExecuteReaderRequiresOpenConnection()
34+
{
35+
using (var connection = new MySqlConnection())
36+
using (var command = connection.CreateCommand())
37+
{
38+
Assert.Throws<InvalidOperationException>(() => command.ExecuteReader());
39+
}
40+
}
41+
42+
readonly DatabaseFixture m_database;
43+
}
44+
}

0 commit comments

Comments
 (0)