System.Data.SQLite

Ticket Change Details
Login
Overview

Artifact ID: dedb1b7197321ffc63e9cd3a47d0eded0a155aa0
Ticket: 070b05bef9352a8657ce9c1e49fad0c935848cd8
SQLiteDataReader.IsDBNull(-1) should throw an exception
User & Date: anonymous 2021-01-12 11:50:41
Changes

  1. foundin changed to: "1.0.112"
  2. icomment:
    Calling SQLiteDataReader.IsDBNull with an invalid index (such as -1 or index >= reader.FieldCount) returns true instead of throwing an exception (ADO.NET throws an IndexOutOfRangeException e.g.).
    
    Let's assume we have a piece of code such as:
    //Method:
    int SafeToInt(DbDataReader reader, string colName) {
        if (reader.IsDBNull(reader.GetOrdinal(colName))
            return someDefaultValue;
        else
            return reader.GetInt32(reader.GetOrdinal(colName));
    }
    
    //Caller:
    int myValue = SafeToInt(reader, "ColumnNameWithTypo");    //Oops, the programmer made a typo with a column name..
    
    
    Since GetOrdinal returns -1 if it couldn't find a column name (ADO.NET throws an exception in this case as well) this is directly passed to IsDBNull which evaluates to true and the default value is used. It's not obvious to the developer that he accidentially mistyped the column name. This is an annoying bug in the program and potentially hard to track down. So I think an exception is more appropriate when calling IsDBNull with an invalid index.
    
  3. login: "anonymous"
  4. mimetype: "text/plain"
  5. private_contact changed to: "c3d125c79a6da6ef9a4a8ece10fe39237fc3ff5d"
  6. severity changed to: "Important"
  7. status changed to: "Open"
  8. title changed to:
    SQLiteDataReader.IsDBNull(-1) should throw an exception
    
  9. type changed to: "Feature_Request"