Repository navigation
FIX: Validate caller-controlled native buffer sizes - #802
gargsaumya wants to merge 69 commits into
Conversation
PR Performance Report
|
| Database task | Unix / SQL Server 2022 | Unix / SQL Server 2025 |
|---|---|---|
| Insertion with explicit input sizes | 33.6% slower | 32.4% slower |
| 5,000 x 8 VARCHAR(8000) / ASCII to UTF-16 | 27.0% inconsistent | No signal |
The largest recorded phase increases for these tasks are shown below. Phase timings are supporting evidence, not root-cause proof.
1 additional inconsistent slowdown also need review.
Coverage: 2 of 2 environments completed. Advisory result; does not block merging.
Measured timings
| Environment | Database task | Before | After | Change |
|---|---|---|---|---|
| Unix / SQL Server 2022 | Insertion with explicit input sizes | 418.218 ms | 546.349 ms | +33.6% |
| Unix / SQL Server 2022 | 5,000 x 8 VARCHAR(8000) / ASCII to UTF-16 | 44.626 ms | 45.966 ms | +27.0% |
| Unix / SQL Server 2025 | Insertion with explicit input sizes | 423.004 ms | 548.371 ms | +32.4% |
Performance diagnostics
Phase times are inclusive diagnostics and must not be added together. They identify where measured time changed, not why it changed.
Unix / SQL Server 2022
Insertion with explicit input sizes: py::execute::cpp_call +24.287 ms; ddbc::SQLExecute_wrap +23.604 ms; ddbc::BindParameters::SQLBindParameter_call +0.346 ms.
5,000 x 8 VARCHAR(8000) / ASCII to UTF-16: ddbc::FetchBatchData +3.622 ms; ddbc::FetchBatchData::SQLFetchScroll_call +2.728 ms; py::fetchall::cpp_call +1.365 ms.
Unix / SQL Server 2025
Insertion with explicit input sizes: py::execute::cpp_call +20.328 ms; ddbc::SQLExecute_wrap +19.767 ms; ddbc::BindParameters +1.784 ms.
All database tasks and timings
Unix / SQL Server 2022
| Database task | Before | After | Paired change | Result |
|---|---|---|---|---|
| Connection opening | 10.518 ms | 10.571 ms | +0.5% | no signal |
| SELECT queries | 1.342 ms | 1.490 ms | +15.7% | no signal |
| Row insertion | 31.922 ms | 32.847 ms | +1.9% | no signal |
| Executemany inserts | 158.664 ms | 161.033 ms | +1.8% | no signal |
| Fetch-all queries | 120.589 ms | 122.864 ms | +1.1% | no signal |
| Row-by-row fetching | 6.068 ms | 6.380 ms | +6.0% | no signal |
| Batched row fetching | 122.221 ms | 118.980 ms | -3.6% | no signal |
| Transaction commit and rollback | 110.137 ms | 108.371 ms | -0.5% | no signal |
| Arrow row fetching | 91.329 ms | 100.663 ms | +8.9% | no signal |
| 100,000-row insertion | 358.458 ms | 376.420 ms | +5.0% | no signal |
| Row fetching in batches of 100 | 122.737 ms | 125.902 ms | +3.8% | no signal |
| Row fetching in batches of 10,000 | 122.438 ms | 124.631 ms | +2.5% | no signal |
| Repeated positional queries | 33.994 ms | 33.825 ms | -0.2% | no signal |
| Repeated named-parameter queries | 34.019 ms | 34.117 ms | +3.2% | no signal |
| Legacy 100,000-row insertion | 294.419 ms | 273.162 ms | -8.1% | no signal |
| Insertion with explicit input sizes | 418.218 ms | 546.349 ms | +33.6% | consistent slowdown |
| Joined aggregation queries | 180.935 ms | 181.659 ms | +0.3% | no signal |
| Large joined-result fetching | 191.005 ms | 190.181 ms | +0.2% | no signal |
| 1.2-million-row fetching | 3531.966 ms | 3565.024 ms | +0.9% | no signal |
| Common table expression queries | 5.642 ms | 5.590 ms | +0.0% | no signal |
| 256 KiB VARCHAR(MAX) / fetchall() | 1.343 ms | 1.323 ms | -2.0% | no signal |
| 10,000 scalar values / fetchval() (debug disabled) | 19.416 ms | 19.659 ms | +1.3% | no signal |
| 10,000 output-converted rows / fetchall() | 20.193 ms | 20.222 ms | +0.1% | no signal |
| 10,000 output-converted rows / fetchmany(1000) | 25.018 ms | 24.415 ms | -1.3% | no signal |
| 10,000 INT/NVARCHAR rows / fetchmany(1) | 27.971 ms | 28.108 ms | -0.7% | no signal |
| 5,000 x 8 VARCHAR(8000) / ASCII to UTF-16 | 44.626 ms | 45.966 ms | +27.0% | inconsistent slowdown |
| 5,000 x 8 VARCHAR(8000) / CP1252 to UTF-16 | 58.809 ms | 58.881 ms | +0.3% | no signal |
| 5,000 x 8 VARCHAR(8000) / UTF-8 to UTF-16 (astral) | 111.502 ms | 113.340 ms | +7.8% | no signal |
| 5,000 x 8 NVARCHAR(4000) / UTF-16 (astral) | 87.533 ms | 92.247 ms | -2.6% | no signal |
| 5,000 x 8 VARCHAR(8000) / short ASCII | 10.301 ms | 10.211 ms | -3.1% | no signal |
| 200 x 8 VARCHAR(8000) / full-width ASCII | 45.419 ms | 44.154 ms | -13.8% | no signal |
| 5,000 x 8 VARCHAR(8000) / short CP1252 | 12.388 ms | 12.762 ms | +2.6% | no signal |
| 200 x 8 VARCHAR(8000) / full-width CP1252 | 43.457 ms | 43.208 ms | +1.0% | no signal |
| 5,000 x 8 VARCHAR(8000) / short UTF-8 (astral) | 15.801 ms | 15.938 ms | -1.3% | no signal |
| 200 x 8 VARCHAR(8000) / full-width UTF-8 (astral) | 45.937 ms | 48.047 ms | +10.7% | no signal |
| 5,000 x 8 NVARCHAR(4000) / short UTF-16 (astral) | 14.402 ms | 14.321 ms | -0.6% | no signal |
| 200 x 8 NVARCHAR(4000) / full-width UTF-16 (astral) | 61.950 ms | 61.109 ms | -4.0% | no signal |
| 200,000 x 8 VARCHAR(32) / fetchall control | 551.018 ms | 522.603 ms | -4.8% | no signal |
| 200,000 x 8 INT / fetchall control | 281.601 ms | 283.987 ms | +1.2% | no signal |
| 10,000 rows x 32 columns / dict(row._mapping) (warm) | 31.589 ms | 31.203 ms | -0.8% | no signal |
Unix / SQL Server 2025
| Database task | Before | After | Paired change | Result |
|---|---|---|---|---|
| Connection opening | 81.126 ms | 81.888 ms | +0.5% | no signal |
| SELECT queries | 1.292 ms | 1.306 ms | +1.4% | no signal |
| Row insertion | 32.772 ms | 32.516 ms | +0.3% | no signal |
| Executemany inserts | 154.560 ms | 153.921 ms | -1.3% | no signal |
| Fetch-all queries | 121.244 ms | 123.995 ms | +2.4% | no signal |
| Row-by-row fetching | 6.069 ms | 6.187 ms | +0.5% | no signal |
| Batched row fetching | 118.276 ms | 120.328 ms | +1.3% | no signal |
| Transaction commit and rollback | 112.553 ms | 111.830 ms | -2.3% | no signal |
| Arrow row fetching | 91.674 ms | 100.614 ms | +9.6% | no signal |
| 100,000-row insertion | 367.889 ms | 389.208 ms | +5.5% | no signal |
| Row fetching in batches of 100 | 124.876 ms | 128.618 ms | +2.2% | no signal |
| Row fetching in batches of 10,000 | 123.287 ms | 128.951 ms | +4.6% | no signal |
| Repeated positional queries | 34.027 ms | 35.056 ms | +2.8% | no signal |
| Repeated named-parameter queries | 34.233 ms | 34.621 ms | +1.1% | no signal |
| Legacy 100,000-row insertion | 295.086 ms | 282.485 ms | -0.4% | no signal |
| Insertion with explicit input sizes | 423.004 ms | 548.371 ms | +32.4% | consistent slowdown |
| Joined aggregation queries | 165.532 ms | 165.415 ms | +0.4% | no signal |
| Large joined-result fetching | 186.226 ms | 190.455 ms | +1.5% | no signal |
| 1.2-million-row fetching | 3519.942 ms | 3526.912 ms | +1.0% | no signal |
| Common table expression queries | 5.406 ms | 5.222 ms | -2.5% | no signal |
| 256 KiB VARCHAR(MAX) / fetchall() | 1.401 ms | 1.660 ms | +25.7% | no signal |
| 10,000 scalar values / fetchval() (debug disabled) | 19.813 ms | 19.499 ms | -1.0% | no signal |
| 10,000 output-converted rows / fetchall() | 19.802 ms | 19.545 ms | -1.6% | no signal |
| 10,000 output-converted rows / fetchmany(1000) | 23.901 ms | 24.103 ms | +4.0% | no signal |
| 10,000 INT/NVARCHAR rows / fetchmany(1) | 28.098 ms | 28.285 ms | +0.6% | no signal |
| 5,000 x 8 VARCHAR(8000) / ASCII to UTF-16 | 44.351 ms | 50.536 ms | +17.8% | no signal |
| 5,000 x 8 VARCHAR(8000) / CP1252 to UTF-16 | 57.683 ms | 62.094 ms | +14.6% | no signal |
| 5,000 x 8 VARCHAR(8000) / UTF-8 to UTF-16 (astral) | 102.218 ms | 120.019 ms | +16.2% | no signal |
| 5,000 x 8 NVARCHAR(4000) / UTF-16 (astral) | 124.113 ms | 128.046 ms | +1.5% | no signal |
| 5,000 x 8 VARCHAR(8000) / short ASCII | 10.359 ms | 10.226 ms | -0.7% | no signal |
| 200 x 8 VARCHAR(8000) / full-width ASCII | 53.998 ms | 50.855 ms | -4.0% | no signal |
| 5,000 x 8 VARCHAR(8000) / short CP1252 | 12.925 ms | 12.610 ms | -1.7% | no signal |
| 200 x 8 VARCHAR(8000) / full-width CP1252 | 50.161 ms | 49.999 ms | -0.4% | no signal |
| 5,000 x 8 VARCHAR(8000) / short UTF-8 (astral) | 15.998 ms | 16.192 ms | -1.1% | no signal |
| 200 x 8 VARCHAR(8000) / full-width UTF-8 (astral) | 52.909 ms | 54.475 ms | +2.7% | no signal |
| 5,000 x 8 NVARCHAR(4000) / short UTF-16 (astral) | 14.407 ms | 14.738 ms | +1.3% | no signal |
| 200 x 8 NVARCHAR(4000) / full-width UTF-16 (astral) | 75.658 ms | 76.344 ms | -1.0% | no signal |
| 200,000 x 8 VARCHAR(32) / fetchall control | 525.136 ms | 511.492 ms | +0.5% | no signal |
| 200,000 x 8 INT / fetchall control | 271.474 ms | 267.197 ms | -0.5% | no signal |
| 10,000 rows x 32 columns / dict(row._mapping) (warm) | 31.390 ms | 31.120 ms | -2.1% | no signal |
Build and measurement details
PR head: 81dc350b08aad46354cb564ae14f275e816ca95e
Base: 239f79fd87f01b7b5716762f943c7b143fdf8e1b
Measured merge: 8efde89821b3522929d8523f74bd8dca7258df66
- Unix / SQL Server 2022: Python 3.12.3, x86_64, SQL 16.0.4325.4; 5 paired comparisons and 1 warmup.
- Unix / SQL Server 2025: Python 3.12.3, x86_64, SQL 17.0.5065.9; 5 paired comparisons and 1 warmup.
A consistent change requires more than 20% median paired movement, at least 1 ms between the median runtimes, and at least 80% of pairs exceeding the relative threshold in the same direction. A slowdown without enough pair agreement is reported as inconsistent.
The displayed change is the median of paired before-and-after ratios. It is not recalculated from the two displayed median runtimes.
Both revisions use profiling-enabled builds on the same agent and database, with alternating order and discarded warmups. Results are diagnostic and do not represent production-wheel latency.
Raw samples and logs are attached to the ADO run as profiler-* artifacts.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Unresolved row-count and binary validation gaps remain, and an existing Arrow batch-size test requires reconciliation.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
Open (2)
What changed in this PR
Hardens Python-to-native boundary validation for row counts, allocations, metadata, and binary inputs.
Changes:
- Adds row-count, boolean, and input-size validation.
- Adds native allocation overflow and metadata checks.
- Tightens binary and wide-character handling with regression tests.
| File | Description |
|---|---|
tests/test_024_bulkcopy_arrow.py |
Tests boolean batch-size rejection. |
tests/test_004_cursor.py |
Tests cursor size and metadata validation. |
mssql_python/pybind/ddbc_bindings.cpp |
Adds native allocation and parameter validation. |
mssql_python/cursor.py |
Validates row counts and input sizes. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Native paths still permit dangerous negative or extremely large allocations, and arrow_reader() does not reject invalid sizes synchronously.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 4
📊 Code Coverage Report
Diff CoverageDiff: main...HEAD, staged and unstaged changes
Summary
mssql_python/cursor.pyLines 93-113 Lines 3139-3147 mssql_python/pybind/connection/connection.cppLines 339-347 mssql_python/pybind/ddbc_bindings.cppLines 328-337 Lines 338-347 Lines 345-354 Lines 362-382 Lines 382-391 Lines 395-404 Lines 420-428 Lines 426-434 Lines 448-457 Lines 494-503 Lines 514-524 Lines 526-549 Lines 547-556 Lines 639-647 Lines 682-691 Lines 694-703 Lines 712-721 Lines 722-731 Lines 737-746 Lines 745-754 Lines 998-1006 Lines 2488-2496 Lines 2729-2738 Lines 2777-2785 Lines 2831-2839 Lines 2854-2863 Lines 2996-3004 Lines 3302-3310 Lines 3374-3385 Lines 3406-3415 Lines 3440-3458 Lines 3871-3881 Lines 3882-3892 Lines 3891-3901 Lines 3908-3917 Lines 4023-4032 Lines 4063-4071 Lines 4240-4249 Lines 4247-4256 Lines 4266-4275 Lines 4304-4313 Lines 4321-4329 Lines 4359-4367 Lines 4762-4770 Lines 5146-5154 Lines 5236-5244 Lines 5775-5784 Lines 5823-5834 Lines 5896-5905 Lines 5928-5937 Lines 5956-5965 Lines 6242-6251 Lines 6252-6261 Lines 6317-6326 Lines 6416-6424 Lines 6569-6577 Lines 6595-6606 Lines 6613-6622 Lines 6936-6945 Lines 6948-6957 Lines 6955-6964 Lines 6980-6994 Lines 7022-7030 Lines 7028-7047 Lines 7061-7107 Lines 7144-7153 Lines 7286-7294 Lines 7400-7408 Lines 7413-7423 Lines 7490-7499 Lines 7554-7562 Lines 7642-7651 Lines 7743-7751 Lines 7903-7916 Lines 8331-8343 mssql_python/pybind/ddbc_bindings.hmssql_python/pybind/native_fetch.hppLines 734-742 mssql_python/pybind/param_detect.hppLines 155-172 Lines 251-266 Lines 289-304 Lines 312-321 Lines 333-342 Lines 342-357 Lines 357-366 Lines 370-379 Lines 533-542 📋 Files Needing Attention📉 Files with overall lowest coverage (click to expand)🔗 Quick Links
|
…nto saumya/native-size-validation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Critical native buffer-copy and allocation risks, plus incomplete public batch-size validation, remain unresolved.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 2
There was a problem hiding this comment.
🟡 Changes recommended
A critical active finding remains: the destructor can dereference an optional null SQLCancel function pointer during DAE cleanup, causing a crash instead of preserving the original exception.
1 open issue
1 resolved since last review
Review effort: Lite
Restore empty-versus-NULL semantics for TVP cells and streamed Unicode/binary values. Add direct native DAE regression cases for empty text, bytes, bytearray and SQL NULL. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Always reset parameter bindings while preserving the original exception when a provider omits SQLCancel. Exercise missing-function cleanup, owner release and statement reuse in a subprocess regression. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
A critical active finding remains: DAE cleanup unconditionally calls the optional SQLCancel symbol, which can crash during error unwinding for drivers that do not provide it.
No open issues 🎉
1 resolved since last review
Review effort: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
Broad security-sensitive changes to native fetch, Arrow pending-row ownership, ODBC cleanup, and DAE recovery require human cross-platform runtime validation of cancellation, fault, and lifetime behavior before approval.
No open issues 🎉
Review effort: Lite
Compare the requested mode with the driver-reported mode in dedicated and generic setters, preserve the proven-clean pooling fast path, and check contiguous IDs across quota boundaries in both modes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…uity Retain per-call column references, validate lengths before the row loop, and cover exact length-check counts with real DAE execution. Preserve pending rows for metadata-only statement changes and strengthen mixed Arrow fetch regressions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because the change contains an unchecked driver-reported row count that could cause out-of-bounds reads, along with misleading cancellation-error output on a supported path.
2 open issues
Review effort: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because the change has critical hidden findings affecting pool reset state, GIL-safe cleanup of retained table parameters, and complete DAE coverage for automatically detected time values.
No open issues 🎉
2 resolved since last review
Review effort: Lite
### Work Item / Issue Reference > ADO Work Item: [AB#47843](https://sqlclientdrivers.visualstudio.com/mssql-python/_workitems/edit/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. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Probe driver autocommit only when pending Arrow rows need a continuity decision. Keep empty setters and forced pool checkout within their existing ODBC call contracts, and exercise both setter paths and modes with stale cached state. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Fixed the native pool sanitation CI regression in 78cd769, published on this PR at da685c0 (including the intervening #803 merge). The regression came from the idempotent-autocommit fix in 92c2b29 on #802: it queried driver mode even during forced pool checkout and empty setters, changing the required ODBC call sequence from Validation on the final combined branch:
An earlier Windows Arrow-reader subprocess timed out; that exact case passed unchanged on rerun, and the final combined selection passed cleanly. New Azure validation build 195519 is queued; this comment does not claim remote CI is already green. |
Use concrete bytes and bytearray lengths for all executemany binary scans. Measure automatic time normalization in UTF-16 units and apply DAE selection after normalization. Cover binary subclass round trips and normalized time threshold boundaries. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because a critical hidden finding indicates the new mixed ASCII/non-ASCII batch behavior can fail its added test and mis-encode data due to an inconsistent text type update.
No open issues 🎉
2 resolved since last review
Review effort: Lite
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Retry terminator-only zero-length truncation probes with payload space, while rejecting stalled payload reads. Reuse complete per-call diagnostic classification for continuation checks instead of querying warning records twice; preserve the reader fallback when classification is unavailable. Enable existing isolated diagnostic assertions with Windows test interposition controls. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Fixed the current build 195521 failures in d8faafe, with latest main (#924, 239f79f) merged normally. All completed Linux x86_64 jobs had the same six test failures. These came from the fetch-size guards merged from #803 (fba5aad), not the latest binary/time review correction:
I made the existing isolated diagnostic tests runnable with Windows test-interposition controls, keeping their original call-count assertions. This reproduced all five failures locally instead of skipping them. The sixth failure was reproduced through the real native variable-length helper with the driver's zero-length truncation sequence. After the correction:
The coverage publishing error was downstream: pytest failed before the unified coverage output was generated. No coverage bypass or weaker assertion was added. New remote CI still needs to finish; these are local validation results, not a claim that all platforms are green already. |
Allow a payload slot after exactly filled text truncation chunks while retaining underfilled-chunk and binary no-progress validation. Reproduce the Linux CP1252-to-UTF8 fetch sequence in native tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Fixed the remaining Linux CI failure in 81dc350 ( I reproduced the failing commit under Ubuntu/GCC with the actual Linux ODBC driver, not just a synthetic fixture. For a CP1252 value converted to UTF-8, SQLGetData returned The fix adds one payload element when a text truncation chunk exactly fills the available payload, so completion can be confirmed while preserving the prefix. Underfilled/stalled chunks, malformed indicators, binary no-progress checks and allocation/budget guards remain enforced. Added a native regression matching the observed sequence plus underfilled-chunk rejection; the original Unicode round-trip assertion is unchanged. Validation:
The two new review threads were also independently checked and resolved with evidence: relative scroll preserves the expected rows after both scalar and array fetches; Python's legacy SQL_C_CHAR constant already aliases wide ODBC binding (-8), and the exact automatic-wide-binding test passes independently. The branch includes latest main and was updated by normal push. New CI is pending; this is not a claim that the new all-platform run is already green. |
There was a problem hiding this comment.
🔵 Needs a closer look
A critical active issue causes oversized Arrow allocations before budget reduction, violating the documented batch-size behavior and triggering erroneous 256 MiB failures.
No open issues 🎉
2 resolved since last review
Review effort: Lite


Work Item / Issue Reference
Summary
No tracking issue exists for this security hardening work.