Skip to content

FIX: Validate driver-provided fetch sizes - #803

Open
gargsaumya wants to merge 8 commits into
saumya/native-size-validationfrom
saumya/native-fetch-validation
Open

gargsaumya wants to merge 8 commits into
saumya/native-size-validationfrom
saumya/native-fetch-validation

Conversation

@gargsaumya

@gargsaumya gargsaumya commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Work Item / Issue Reference

ADO Work Item: AB#47843


Summary

  • Validate ODBC row counts, indicators, and column-buffer layout before access.
  • Check streamed fetch growth and Arrow source/destination bounds.
  • Correct fixed-width buffer lengths and reject malformed wide-character data.

Stacked on #802 so this review contains only driver-provided fetch validation.

@github-actions github-actions Bot added the pr-size: medium Moderate update size label Sep 21, 2026
…nto saumya/native-fetch-validation

# Conflicts:
#	mssql_python/pybind/ddbc_bindings.cpp
…nto saumya/native-fetch-validation

# Conflicts:
#	mssql_python/pybind/ddbc_bindings.cpp
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)(intptr_t)fetchSize, 0);
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROWS_FETCHED_PTR, &numRowsFetched, 0);
SQLULEN numRowsFetched = 0;
FetchStateGuard fetchStateGuard(hStmt, &numRowsFetched, fetchSize);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the non-LOB fetchall() error path, this guard runs statement cleanup before Python calls check_error(). Those ODBC calls replace the failed fetch’s diagnostics, losing its SQLSTATE and driver message. A specific DataError or OperationalError can consequently become DatabaseError: Unknown DDBC error.

Please capture the diagnostics before cleanup and preserve the existing DB-API exception mapping, while retaining cleanup before the buffers are destroyed.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the driver-provided size validation is well scoped and the normal fetch and arrow paths hold up. I found one suggestion worth incorporating before merge, so requesting changes.

ret);
return ret;
}
for (SQLUSMALLINT col = 0; col < numCols; ++col) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: these checks are the only safety net when a driver returns malformed row counts, indicators, or buffer lengths, but this diff adds no tests and this stacked branch has no coverage run. the normal fetch and arrow tests pass without exercising any rejection path.

can we add a fake ODBC callback test seam for oversized row counts, odd SQLWCHAR lengths, out-of-capacity indicators, and SQL_NO_TOTAL with no progress?

return left * right;
}

size_t CheckedFetchAdd(size_t left, size_t right, const char* errorMessage) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: CheckedFetchAdd and CheckedFetchMultiply only forward to the existing checked-size helpers without adding fetch-specific behavior. using CheckedAddSize and CheckedMultiplySize directly keeps one vocabulary for the same overflow contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: medium Moderate update size

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants