Skip to content

Commit

Permalink
Improve exceptions for NULL values. Fixes #1092
Browse files Browse the repository at this point in the history
Fixes the bounds check for out-of-range ordinal values.

Signed-off-by: Bradley Grainger <[email protected]>
  • Loading branch information
bgrainger committed Nov 28, 2021
1 parent 904a80a commit bd3906c
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/MySqlConnector/Core/Row.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public Row Clone()

public object GetValue(int ordinal)
{
if (ordinal < 0 || ordinal > ResultSet.ColumnDefinitions!.Length)
throw new ArgumentOutOfRangeException(nameof(ordinal), "value must be between 0 and {0}.".FormatInvariant(ResultSet.ColumnDefinitions!.Length));
if (ordinal < 0 || ordinal >= ResultSet.ColumnDefinitions!.Length)
throw new ArgumentOutOfRangeException(nameof(ordinal), "value must be between 0 and {0}.".FormatInvariant(ResultSet.ColumnDefinitions!.Length - 1));

if (m_dataOffsets[ordinal] == -1)
return DBNull.Value;
Expand Down Expand Up @@ -171,7 +171,7 @@ public Guid GetGuid(int ordinal)
if (value is byte[] { Length: 16 } bytes)
return CreateGuidFromBytes(Connection.GuidFormat, bytes);

throw new InvalidCastException("The value could not be converted to a GUID: {0}".FormatInvariant(value));
return (Guid) value;
}

public short GetInt16(int ordinal)
Expand Down Expand Up @@ -203,11 +203,8 @@ public short GetInt16(int ordinal)

public int GetInt32(int ordinal)
{
if (ordinal < 0 || ordinal > ResultSet.ColumnDefinitions!.Length)
throw new ArgumentOutOfRangeException(nameof(ordinal), "value must be between 0 and {0}.".FormatInvariant(ResultSet.ColumnDefinitions!.Length));

if (m_dataOffsets[ordinal] == -1)
throw new InvalidCastException();
if (ordinal < 0 || ordinal >= ResultSet.ColumnDefinitions!.Length || m_dataOffsets[ordinal] == -1)
return (int) GetValue(ordinal);

var columnDefinition = ResultSet.ColumnDefinitions[ordinal];
if (columnDefinition.ColumnType is ColumnType.Decimal or ColumnType.NewDecimal)
Expand Down Expand Up @@ -443,6 +440,8 @@ public MySqlGeometry GetMySqlGeometry(int ordinal)

public MySqlDecimal GetMySqlDecimal(int ordinal)
{
if (IsDBNull(ordinal))
return (MySqlDecimal) GetValue(ordinal);
var data = m_data.Slice(m_dataOffsets[ordinal], m_dataLengths[ordinal]).Span;
var columnType = ResultSet.ColumnDefinitions![ordinal].ColumnType;
if (columnType is ColumnType.NewDecimal or ColumnType.Decimal)
Expand Down

0 comments on commit bd3906c

Please sign in to comment.