Bug #100522 MySqlCommand.Parameters.Insert(-1) succeeds but should fail
Submitted: 13 Aug 2020 20:32 Modified: 24 Nov 2020 18:01
Reporter: Bradley Grainger (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:8.0.21 OS:Windows (10)
Assigned to: CPU Architecture:Any

[13 Aug 2020 20:32] Bradley Grainger
Description:
DbParameterCollection.Insert(index, value) is documented as inserting the parameter at the index given by 'index'. Like List<T>.Insert, it's expected that supplying an out-of-range index will throw an ArgumentOutOfRangeException.

MySqlParameterCollection.Insert(-2, p) does throw an ArgumentOutOfRangeException, but MySqlParameterCollection.Insert(-1, p) does not; instead it appends the parameter to the end of the collection.

You can see from this chart that this behaviour is specific to MySql.Data and is not shared by other ADO.NET providers: https://mysql-net.github.io/AdoNetResults/#ParameterCollection_Insert_throws_for_negative_...

How to repeat:
Run the following C# code:

var command = new MySqlCommand();
command.Parameters.Insert(0, new MySqlParameter("test0", "test0"));
command.Parameters.Insert(-1, new MySqlParameter("test-1", "test-1")); // expected should throw
Console.WriteLine(command.Parameters.Count); // unexpected: 2
Console.WriteLine(command.Parameters[0].ParameterName); // test0
Console.WriteLine(command.Parameters[1].ParameterName); // test-1

Suggested fix:
The call to "command.Parameters.Insert(-1, x);" should throw an ArgumentOutOfRangeException.
[14 Aug 2020 6:20] MySQL Verification Team
Hello Bradley,

Thank you for the report and test case.
Verified as described.

regards,
Umesh
[14 Aug 2020 6:20] MySQL Verification Team
-- VS 2019, Connector/NET 8.0.21

using MySql.Data.MySqlClient;
using System;

namespace Bug100522
{
    class Program
    {
        static void Main(string[] args)
        {
			Console.WriteLine("Hello World!");
			using (var connection = new MySqlConnection("server=localhost;port=3306;user id=ushastry;password=mysql123;database=test;IgnorePrepare=false"))
			{
				connection.Open();
				var command = new MySqlCommand();
				command.Parameters.Insert(0, new MySqlParameter("test0", "test0"));
				command.Parameters.Insert(-1, new MySqlParameter("test-1", "test-1")); // expected should throw
				Console.WriteLine(command.Parameters.Count); // unexpected: 2
				Console.WriteLine(command.Parameters[0].ParameterName); // test0
				Console.WriteLine(command.Parameters[1].ParameterName); // test-1
			}

		}
	}
    
}
-
Hello World!
2
test0
test-1

C:\Work\MySQLNet\Bug100522\Bug100522\bin\Debug\netcoreapp3.1\Bug100522.exe (process 13916) exited with code 0.
Press any key to close this window . . .
[24 Nov 2020 18:01] Christine Cole
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/NET 8.0.23 release, and here's the proposed changelog entry from the documentation team:

Connector/NET used the value of -1 internally to ensure that a parameter
without an index was added to the end of the parameter list. However, if
an index with an actual value of -1 was passed in, the collection was
interpreted as having no index and the argument did not generate an
out-of-range exception.

Thank you for the bug report.