Skip to content

FIX: Validate driver-provided fetch sizes - #803

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

gargsaumya wants to merge 61 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
Comment thread mssql_python/pybind/ddbc_bindings.cpp Outdated

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.

Comment thread mssql_python/pybind/ddbc_bindings.cpp Outdated
Comment thread mssql_python/pybind/ddbc_bindings.cpp Outdated
gargsaumya and others added 2 commits October 7, 2026 11:41
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added pr-size: large Substantial code update and removed pr-size: medium Moderate update size labels Oct 7, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added pr-size: medium Moderate update size and removed pr-size: large Substantial code update labels Oct 7, 2026
gargsaumya and others added 2 commits October 7, 2026 11:46
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

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.

Copilot review overview

🟡 Changes recommended

Successful zero-row fetches can loop indefinitely, and malformed indicators or the global test hook can cause resource and concurrency problems.

Review effort: Balanced
Findings: 2 High severity · 2 Medium severity

Open (4)
What changed in this PR

Adds defensive validation for driver-provided fetch metadata in the native ODBC layer.

Changes:

  • Validates row counts, indicators, wide-character lengths, and Arrow bounds.
  • Hardens streamed variable-length fetching and buffer growth.
  • Adds native regression scenarios and subprocess coverage.
File Description
mssql_python/​pybind/​ddbc_bindings.cpp Implements fetch validation and test hooks.
mssql_python/​pybind/​ddbc_bindings.h Rejects malformed wide-character lengths.
tests/​test_010_pybind_functions.py Tests malformed driver responses and streaming progress.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread mssql_python/pybind/ddbc_bindings.cpp
Comment thread mssql_python/pybind/ddbc_bindings.cpp Outdated
Comment thread mssql_python/pybind/ddbc_bindings.cpp Outdated
Comment thread mssql_python/pybind/ddbc_bindings.cpp
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

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.

Copilot review overview

🔵 Needs a closer look

The test hook unsafely replaces a process-wide ODBC function pointer while concurrent fetches may access it without the GIL.

Review effort: Balanced
Findings: 2 High severity · 2 Medium severity

Open (4)

gargsaumya and others added 2 commits October 7, 2026 12:08
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@gargsaumya
gargsaumya requested a balanced review from Copilot October 7, 2026 06:41

Copilot AI left a comment

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.

🟡 Changes recommended

Direct LOB streaming still rejects valid SQL_NO_DATA completion after streamed progress.

1 open finding
2 resolved since last review

🧠 Review effort: Balanced

Comment thread mssql_python/pybind/ddbc_bindings.cpp
gargsaumya and others added 2 commits October 12, 2026 12:25
Treat SQL_NO_DATA as normal completion only after a direct LOB fetch has returned payload.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

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.

🔵 Needs a closer look

The broad native ODBC changes require final cross-platform CI and human verification.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

Copilot AI left a comment

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.

🟡 Changes recommended

Reused or zero-initialized indicators can still accept omitted driver outputs and expose stale or incorrect results.

2 open findings

🧠 Review effort: Balanced

Comment thread mssql_python/pybind/ddbc_bindings.cpp
Comment thread mssql_python/pybind/ddbc_bindings.cpp
gargsaumya and others added 2 commits October 12, 2026 12:48
Seed direct and bound fetch indicators with invalid sentinels so omitted driver writes cannot expose empty or stale data.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

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.

🟡 Changes recommended

Several direct-fetch indicators remain initialized unsafely, allowing malformed successful responses to trigger undefined behavior or evade validation.

1 open finding
2 resolved since last review

🧠 Review effort: Balanced

Comment thread mssql_python/pybind/ddbc_bindings.cpp
gargsaumya and others added 2 commits October 12, 2026 15:03
Seed every direct SQLGetData output with the invalid indicator sentinel before driver calls.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

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.

🟡 Changes recommended

Some malformed diagnostic and indicator paths still fail open or silently return None.

3 open findings
1 resolved since last review

🧠 Review effort: Balanced

Comment thread mssql_python/pybind/ddbc_bindings.cpp Outdated
Comment thread mssql_python/pybind/ddbc_bindings.cpp
Comment thread mssql_python/pybind/ddbc_bindings.cpp Outdated
Reject unreadable truncation diagnostics, missing variant probe indicators, and SQL_NO_TOTAL for fixed-width columns.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

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.

🔵 Needs a closer look

The direct decimal path still accepts a missing driver indicator and silently returns None.

0 open findings

3 resolved since last review

🧠 Review effort: Balanced

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.

Reviewed the exact current head for correctness, native safety, data integrity, diagnostics, cleanup, Arrow/LOB bounds, test coverage, and the applicable ODBC length/indicator contract.

One Medium-severity issue remains: the directly changed DECIMAL/NUMERIC fetch path can accept an unwritten driver indicator and silently expose malformed non-NULL output as Python None. The remaining reviewed fetch paths had no actionable findings.

Recommendation: Request changes.

if (indicator < 0) {
ThrowStdException("Unexpected negative data length");
}
ValidateDecimalDataLength(static_cast<uint64_t>(indicator));

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.

This new decimal length check still accepts an unwritten indicator because indicator starts at 0. A successful driver call that leaves it untouched bypasses both guards; the empty zero-initialized buffer then fails Decimal parsing and is silently converted to None. Initialize it with INVALID_FETCH_INDICATOR, reject the sentinel (and zero for a non-NULL decimal) before parsing, and add missing/zero-indicator tests so malformed driver output cannot become SQL NULL.

@gargsaumya

Copy link
Copy Markdown
Contributor Author

Copilot resolve the merge conflicts in this pull request

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

Labels

pr-size: large Substantial code update

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants