Skip to content

Driver Manager calls SQLGetDiagFieldW with wrong handle type for SQL_DYNAMIC_FUNCTION_CODE and SQL_DYNAMIC_FUNCTION #119

@sjoshi-simba

Description

@sjoshi-simba

Summary

When retrieving diagnostic fields such as SQL_DYNAMIC_FUNCTION_CODE or SQL_DYNAMIC_FUNCTION, the Driver Manager calls SQLGetDiagFieldW with HandleType=SQL_HANDLE_DBC (connection) even though the Handle provided is that of a statement. According to the ODBC specification, these fields are statement-level diagnostics and should only ever be requested with a statement handle.

Reference in code:
https://github.com/openlink/iODBC/blob/develop/iodbc/herr.c#L1690

Expected Behavior

When requesting the dynamic function code from a statement, the Driver Manager should call into the driver with:

  • HandleType = SQL_HANDLE_STMT
  • Handle = valid statement handle

Actual Behavior

iODBC calls the driver with:

  • HandleType = SQL_HANDLE_DBC
  • Handle = actually a statement handle
  • DiagIdentifier = SQL_DYNAMIC_FUNCTION_CODE or SQL_DYNAMIC_FUNCTION

This causes drivers to fail to find the appropriate diagnostic manager, as the handle maps to a statement but the type signals it should locate a connection object.

Impact

  • Drivers that rely on correct diagnostic retrieval for statement operations are broken, including proper error reporting when using iODBC.

Reproduction Steps

  1. Execute a statement using any ODBC driver with iODBC as the Driver Manager
  2. Request diagnostic field SQL_DYNAMIC_FUNCTION_CODE or SQL_DYNAMIC_FUNCTION via SQLGetDiagField
  3. Observe that the driver's implementation receives HandleType=SQL_HANDLE_DBC, even though the handle is not a connection

ODBC Specification Reference

See ODBC documentation for SQLGetDiagField, which prescribes which handle types are allowed/valid for a given DiagIdentifier:

SQL_DYNAMIC_FUNCTION_CODE (and SQL_DYNAMIC_FUNCTION) are only valid for statement handles.

https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgetdiagfield-function?view=sql-server-ver16

Recommendation

Please correct the iODBC Driver Manager logic to ensure that statement-specific diagnostic fields are only ever requested on statement handles using HandleType=SQL_HANDLE_STMT.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions