Bug #86974 Cannot create instance of MySqlGeometry for empty geometry collection
Submitted: 6 Jul 2017 13:43 Modified: 18 Jul 2018 22:26
Reporter: Peet Whittaker (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.9.9 OS:Any
Assigned to: CPU Architecture:Any
Tags: Collection, Geometry, spatial

[6 Jul 2017 13:43] Peet Whittaker
Description:
When trying to create an instance of the MySqlGeometry class for an empty geometry collection, using the following constructor:

  public MySqlGeometry(MySqlDbType type, byte[] val) {...}

It fails with the a ArgumentOutOfRangeException (details are appended at the bottom of this report).

This is due to the fact that the MySQL WKB representation for an empty geometry collection is only 13 bytes in length:

  SELECT HEX(ST_GeometryCollectionFromText("GEOMETRYCOLLECTION()"));

  00-00-00-00-01-07-00-00-00-00-00-00-00

Whereas the MySqlGeometry class assumes that the minimum length of the WKB that it will receive is 21 bytes (WKB for a POINT (25 bytes) minus the 4 bytes from the MySQL-specific SRID extension). Thus causing it to try and read from beyond the end of the byte array and resulting in the observed ArgumentOutOfRangeException.

System.ArgumentOutOfRangeException was unhandled by user code
  HResult=-2146233086
  Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: startIndex
  ParamName=startIndex
  Source=mscorlib
  StackTrace:
       at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
       at System.BitConverter.ToDouble(Byte[] value, Int32 startIndex)
       at MySql.Data.Types.MySqlGeometry..ctor(MySqlDbType type, Byte[] val)

How to repeat:
Call the MySqlGeometry constructor with a byte array that represents an empty collection, i.e.:

var bytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

var mySqlGeometry = new MySqlGeometry(MySqlDbType.Geometry, bytes);

^ will throw ArgumentOutOfRangeException

Suggested fix:
Update the constructor to allow for byte arrays that are only 13 elements in length.
[10 Jul 2017 13:34] Chiranjeevi Battula
Hello Peet,

Thank you for the bug report.
Please refer to the below developer forums it may help you to solve your problem:
https://forums.mysql.com/read.php?38,645461,645526#msg-645526

If issue persists, please provide repeatable test case (exact steps/sample project, create tables statements/database etc. - please make it as private if you prefer) to confirm this issue at our end?

Thanks,
Chiranjeevi.
[10 Jul 2017 14:07] Peet Whittaker
Hi Chiranjeevi,

Thank you for looking into this. However, this issue is not about how to create an empty collection in the database. It is an issue with the constructor of the MySqlGeometry class in the ADO.NET NuGet package.

I've created a very simple project that reproduces the bug. You can find it here: https://github.com/peetw/MySqlBug

Thanks
[11 Jul 2017 7:28] Chiranjeevi Battula
Hello Peet,

Thank you for the feedback and test case.
Verified this behavior on Visual Studio 2015 (C#.Net) and Connector/NET 6.9.9 version.

Thanks,
Chiranjeevi.
[11 Jul 2017 7:29] Chiranjeevi Battula
StackTrace :at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
	at System.BitConverter.ToDouble(Byte[] value, Int32 startIndex)
	at MySql.Data.Types.MySqlGeometry..ctor(MySqlDbType type, Byte[] val)
[11 Jul 2017 7:29] Chiranjeevi Battula
Screenshot

Attachment: 86974.JPG (image/jpeg, text), 156.86 KiB.

[18 Jul 2017 8:51] Peet Whittaker
I can submit a pull request to the github project if that would help?

https://github.com/mysql/mysql-connector-net

Not sure who maintains it, or how active it is... Or even which branch to base the PR off.
[19 Aug 2017 14:33] Peet Whittaker
Hi Chiranjeevi,

Any progress on this? Is there any way I can help?
[17 Nov 2017 9:36] Peet Whittaker
I have submitted a pull request containing a fix here: https://github.com/mysql/mysql-connector-net/pull/15
[27 Nov 2017 14:58] OCA Admin
Contribution submitted via Github - Fix bug #86974 
(*) Contribution by Peet Whittaker (Github peetw, mysql-connector-net/pull/15#issuecomment-346619108): I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: git_patch_153169181.txt (text/plain), 3.83 KiB.

[26 Mar 2018 19:27] Peet Whittaker
Hi, any update on this? It also affects both version 7.x and 8.x
[15 Jul 2018 19:13] Andreas Ravnestad
This is a real showstopper with the MySQL connector (affecting both 6.9.9 as specified in this bug report as well as version 8.0.11 currently available on NuGet). Please take a look at Peet's pull request, it seems to fix the problem well.
[18 Jul 2018 22:26] Christine Cole
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/NET 6.10.8 and 8.0.13 releases, and here's the changelog entry:

The MySql.Data.Types.MySqlGeometry constructor called with an array of
bytes representing an empty geometry collection generated an
ArgumentOutOfRangeException exception, rather than creating the type as
expected. Thanks to Peet Whittaker for the patch.

Thank you for the bug report.