Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arithmetic operation resulted in an overflow in GetSqlType when using ODBC driver on 64-bit Windows #207

Open
cfeltz-isia opened this issue Jan 9, 2025 · 0 comments

Comments

@cfeltz-isia
Copy link

I am encountering a System.OverflowException when executing the System.Data.Odbc.OdbcDataReader.GetSqlType(int i) method in my code. The error occurs specifically when the result of the GetColAttribute method is a negative integer, which happens with the 64-bit ODBC driver used to access an HFSql database (PC Soft).

Here is the exception I am encountering:

Arithmetic operation resulted in an overflow.
System.OverflowException: Arithmetic operation resulted in an overflow.
   at System.IntPtr.ToInt32()
   at System.Data.Odbc.SQLLEN.op_Implicit(SQLLEN value)
   at System.Data.Odbc.OdbcDataReader.GetSqlType(Int32 i)
   at System.Data.Odbc.OdbcDataReader.GetValue(Int32 i)
   at System.Data.Odbc.OdbcDataReader.GetValues(Object[] values)
Problem: The issue seems to occur when the GetColAttribute method returns a negative integer, which is then interpreted as a 32-bit integer in the GetSqlType method, causing an overflow. On a 64-bit Windows system with a 64-bit ODBC driver, certain calls to GetColAttribute return negative integers (e.g., -5), which triggers the exception.

Proposed Solution: To resolve this issue, I suggest interpreting the value returned by GetColAttribute as a 64-bit integer before casting it to a 32-bit integer, as shown below:

Replace :
info._dbtype = unchecked((ODBC32.SQL_TYPE)(int)GetColAttribute(i, ODBC32.SQL_DESC.CONCISE_TYPE, ODBC32.SQL_COLUMN.TYPE, ODBC32.HANDLER.THROW));
By :
info._dbtype = unchecked((ODBC32.SQL_TYPE)(int)(long)GetColAttribute(i, ODBC32.SQL_DESC.CONCISE_TYPE, ODBC32.SQL_COLUMN.TYPE, ODBC32.HANDLER.THROW));

This prevents the exception by ensuring that the negative value is correctly converted.

Additional Details:

Operating System: 64-bit Windows
ODBC Driver: 64-bit for HFSql (PC Soft)
Environment: Using OdbcCommand and OdbcDataReader to access an HFSql database
Expected Behavior: The expected behavior is that the GetSqlType method works correctly without causing an integer overflow exception, even when the value returned by GetColAttribute is negative.

Thank you for your help in resolving this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant