Bug #51610 Exception thrown inside Connector.NET
Submitted: 1 Mar 2010 15:52 Modified: 4 Mar 2010 16:31
Reporter: Anker Berg-Sonne Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.2.2 OS:Windows (XP SP2)
Assigned to: Reggie Burnett CPU Architecture:Any
Tags: Connector/Net, exception

[1 Mar 2010 15:52] Anker Berg-Sonne
Description:
When performing a ExecuteNonQuery call on a command object with the following query:

insert into `hourdistances` select cast(`i`.`StartTime` as date) AS `FirstDate`,cast(`o`.`StartTime` as date) AS `SecondDate`,cast(`i`.`StartTime` as time) AS `Time`,sqrt((pow((`i`.`SenTrol4` - `o`.`SenTrol4`),2)+pow((`i`.`SenTrol47` - `o`.`SenTrol47`),2)+pow((`i`.`SenTrol69` - `o`.`SenTrol69`),2))/ 3) AS `Distance` from (`hourlystatspivot` `i` left join `hourlystatspivot` `o` on(((cast(`i`.`StartTime` as time) = cast(`o`.`StartTime` as time)) and (cast(`i`.`StartTime` as date) <> cast(`o`.`StartTime` as date)) and (`i`.`StartTime` <> `o`.`StartTime`)))) where (`o`.`StartTime` is not null) order by cast(`i`.`StartTime` as date) desc,cast(`o`.`StartTime` as date) desc,cast(`i`.`StartTime` as time)

The following exception is thrown:

{"Index and length must refer to a location within the string.\r\nParameter name: length"}

The query runs fine when not passed through Connector/NET

How to repeat:
None of the tables need to exist. Just create a connection and command object and try to do a command.ExecuteNonQuery(query) and you will see it.
[2 Mar 2010 18:37] Anker Berg-Sonne
Here's another query that throws the same exception.

insert into `hourmatch` SELECT `p`.`ClientID`,`p`.`NodeID`,`h`.`Date`,`h`.`time`,`p`.`SenTrolID`,sqrt(avg(pow(`p`.`Average`-`h`.`Average`,2))) FROM `patternmembers` `p` join `hourscaled` `h` on `p`.`ClientID`=`h`.`ClientID` and `p`.`NodeID`=`h`.`NodeID` and `h`.`Time`=`p`.`Hour` and `p`.`SenTrolID`=`h`.`SenTrolID` group by `p`.`NodeID`, `PatternID`, `Date`, `Time`

I wonder what I am doing that's causing me to see these. Hmmmm.
[3 Mar 2010 18:20] Tonci Grgin
Hi Anker and thanks for your report.

To put your mind at ease, I see the same exception thrown. I'm just in no shape to deal with it now so I'll continue tomorrow.
[3 Mar 2010 18:28] Brian Evans
Simplified cases:

These 2 throw exceptions
SELECT 'ABC', (0/`QOH`) from (SELECT 1 as `QOH`) `d1`;
SELECT 'ABC', (0-`QOH`) from (SELECT 1 as `QOH`) `d1`;

These do not
SELECT 'ABC', (0+`QOH`) from (SELECT 1 as `QOH`) `d1`
SELECT 'ABC', (0*`QOH`) from (SELECT 1 as `QOH`) `d1`
SELECT 'ABC', (0/QOH) from (SELECT 1 as `QOH`) `d1`
SELECT 'ABC', (0-QOH) from (SELECT 1 as `QOH`) `d1`

Seems to relate to the quoting check on "`" and division and subtraction
[3 Mar 2010 18:38] Tonci Grgin
Verified as described with first query, code breaks in MySqlTokenizer.cs, LN136 even though all demands seems to be met:

string token =  sql.Substring(startIndex, stopIndex - startIndex).Trim();
				695		706	695

		sql.Length	705	int
[3 Mar 2010 23:36] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/102243

798 Reggie Burnett	2010-03-03
      - fixed parsing bug that was caused by special characters being jammed up beside a quoted identifier (bug #51610)
[3 Mar 2010 23:38] Reggie Burnett
fixed in 6.0.6, 6.1.4, 6.2.3, and 6.3.2+
[4 Mar 2010 0:25] Anker Berg-Sonne
Thanks guys, that was terrific!

Anker
[4 Mar 2010 16:31] Tony Bedford
An entry has been added to the 6.0.6, 6.1.4, 6.2.3, 6.3.2 changelogs:

When calling ExecuteNonQuery on a command object, the following exception occurred:

Index and length must refer to a location within the string.
Parameter name: length
[8 Mar 2010 15:08] Tonci Grgin
Reggie, please extend the fix to cover for Bug#51788 too.
[30 Mar 2010 5:29] Tonci Grgin
Bug#52408 is closed as duplicate of this report.