Fix SQLLEN implicit conversion overflow for -1 values from ODBC drivers with defensive validation #121032
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes #115389 - System.Data.Odbc SQLLEN does not handle -1 correctly
This PR fixes an
OverflowExceptionthat occurs when querying ODBC databases (such as Filemaker Pro) that return-1as a field length indicator on 64-bit platforms, while preventing potential data corruption from truly out-of-range values.Problem
Some ODBC drivers represent field lengths as
-1(unknown/variable length) using the value0x00000000FFFFFFFF. On 64-bit platforms:IntPtras4294967295SQLLENcalledToInt32()to convert tointToInt32()throwsOverflowExceptionbecause4294967295 > Int32.MaxValue(2147483647)This issue does not affect Java ODBC connectors, which handle this conversion differently.
Solution
Updated the implicit conversion operator in
SQLLENstruct to use a defensive approach:Specific Sentinel Value Handling: Explicitly checks for common ODBC sentinel values when they are improperly represented without sign extension on 64-bit platforms:
0x00000000FFFFFFFF→-1(SQL_NULL_DATA)0x00000000FFFFFFFD→-3(SQL_NTS)0x00000000FFFFFFFC→-4(SQL_NO_TOTAL)Range Validation: For all other values, uses
checked((int)longValue)which throwsOverflowExceptionif the value is outside the Int32 range.This approach:
Testing
Added comprehensive unit tests (
SQLLENTests.cs) that verify:0x00000000FFFFFFFF,0x00000000FFFFFFFD, and0x00000000FFFFFFFCon 64-bit platforms correctly convert to their respective negative values0x0000000100000000) still throwOverflowExceptionas expectedValidation
Created a demonstration program confirming:
OverflowExceptionwhen encountering0x00000000FFFFFFFFThis is a surgical fix that resolves the issue while maintaining data integrity and backward compatibility.
Fixes #115389
Original prompt
Fixes #115389
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.