Skip to content

fix: resolve a Binary length of 0 or NaN from the value - #1817

Merged
arthurschreiber merged 10 commits into
tediousjs:masterfrom
cheenamalhotra:dev/cheena/fix-binary-explicit-length
Nov 5, 2026
Merged

arthurschreiber merged 10 commits into
tediousjs:masterfrom
cheenamalhotra:dev/cheena/fix-binary-explicit-length

Conversation

@cheenamalhotra

@cheenamalhotra cheenamalhotra commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Since 20.3.0, a Binary parameter with an explicit length: 0 or NaN is rejected by the server (#1812).

Fixes #1812

Cause

#1774 moved length resolution into resolveParameter(), which keeps any explicit length (!= null), including 0. Before, a falsy length fell back to resolveLength(). Binary.resolveLength() ignores parameter.length, so Binary was the one type whose wire bytes changed: TYPE_INFO said length 0 while declaration() (still truthiness-based) said binary(2).

Changes

  • Binary gets a native resolve() that treats a falsy length as unspecified, restoring pre-20.3.0 parameter behavior. It reuses resolveLength(), which now infers 1 for an empty buffer rather than the invalid length 0.
  • Parameter truncation happens during resolution. writeValue() sends the actual byte count and rejects values longer than the declared length, so bulk-load and TVP rows cannot silently lose data. Shorter values are padded by the server.
  • writeValue() treats both null and undefined lengths as unspecified (!= null).
  • Unit tests cover parameter truncation, empty/default/null lengths, oversized values, and the actual bulk-load and TVP error wrappers. Integration tests cover RPC parameters, shorter/exact/empty/null row values, oversized row rejection, connection reuse after rejection, and bulk-load padding to the default length of 8000.
  • Binary parameter integration tests reuse execSql(), extended to compare Buffers with deepEqual, instead of maintaining a duplicate connection helper.

Notes for review

  • A null parameter still resolves to TYPE_INFO length 8000 while its declaration is binary(1); the server accepts this existing behavior. The inaccurate comment claiming all TYPE_INFO lengths match declarations has been removed.
  • Binary treats length: 0 as unset, while VarBinary/NVarChar keep an explicit 0. This is deliberate: it matches Binary's own declaration, and NaN currently selects max/PLP for those types, so changing them would be a separate behavior change.
  • Negative, fractional and oversized lengths, clearer errors for zero/NaN bulk-load lengths, and the related Char/NChar issues remain out of scope, as discussed in review.

Verification

  • The original implementation was compared against Microsoft.Data.SqlClient on SQL Server 2025 by capturing its RPC bytes and replaying the bytes generated by this branch through a TDS proxy. Non-null parameter cases matched SqlClient: length 0/NaN/2 returned 0x4A50, length 4 returned 0x4A500000, length 1 returned 0x4A, and an empty value returned 0x00. Those parameter semantics are preserved by the review fixes.
  • After the review fixes: 84 targeted unit tests pass, covering Binary contracts, RPC payloads, and bulk load; npm run lint (including TypeScript checking) and npm run build pass.
  • The targeted integration run was blocked before executing tests because the configured SQL Server host mssql:1433 does not resolve locally (ENOTFOUND). The new integration cases require CI verification.

cc @saurabh500 @David-Engel @arthurschreiber

cheenamalhotra and others added 3 commits October 8, 2026 20:53
Since tediousjs#1774, an explicit Binary length of 0 or NaN was sent as a zero TYPE_INFO length while the declaration still said binary(n), so the server rejected the request. Treat a falsy length as unspecified, as Binary did before 20.3.0 and as its declaration does.

Fixes tediousjs#1812

Co-authored-by: Copilot App <[email protected]>
An empty Binary value now declares binary(1) instead of a zero length, which the server rejects. A declared length larger than the value now writes the value's actual byte count, so the stream is not corrupted; the server pads the value. Both match Microsoft.Data.SqlClient's wire format.

Co-authored-by: Copilot App <[email protected]>
Covers tediousjs#1812 (a length of 0 or NaN), padding, truncation and an empty value.

Co-authored-by: Copilot App <[email protected]>
@codecov

codecov Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.88%. Comparing base (446c0f9) to head (bf6595d).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1817      +/-   ##
==========================================
- Coverage   84.76%   83.88%   -0.89%     
==========================================
  Files          96       96              
  Lines        4720     4728       +8     
  Branches      884      888       +4     
==========================================
- Hits         4001     3966      -35     
- Misses        455      509      +54     
+ Partials      264      253      -11     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Collaborator

Thanks @cheenamalhotra, this is a good find. I'm happy with the direction: treating a Binary length of 0/NaN as "not given" restores what every release did up to 20.2.3. It also matches SqlClient, where Size = 0 means "not set". There is one problem that has to be fixed before merging, plus a few smaller points.

For context, I ran the same cases against SQL Server 2025 with tedious 14.7.0 through 20.3.5 and with this branch, covering request parameters, bulk load and TVPs:

Case (Buffer.from('JP') unless noted) 14.7.0 – 20.2.3 20.3.0 – 20.3.5 this PR
param Binary, length 0 / NaN 0x4A50 server: invalid data length 0x4A50
param Binary, length 4 server: stream ended same 0x4A500000
param Binary, empty value server: invalid data length same 0x00
bulk / TVP Binary, length 4 server error same 0x4A500000
bulk / TVP Binary, length 3, rows A, ABC, ABCDE server error same 0x410000, 0x414243, 0x414243

Must fix: writeValue now silently truncates bulk-load and TVP rows

writeValue is also used for every bulk-load and TVP cell. With this branch, a Binary column declared as length 3 stores ABCDE as ABC, with no error. Before, the whole request failed. Truncating a request parameter to its declared length is long-standing behaviour (and SqlClient does it too), but rows shouldn't lose data silently.

A way to keep both behaviours: do the parameter truncation in resolve, and have writeValue reject a value that is too long.

resolve(parameter) {
  let value = this.validate(parameter.value, undefined);
  const length = parameter.length || (value != null ? value.length || 1 : this.maximumLength);
  if (value != null && value.length > length) {
    value = value.subarray(0, length); // a request parameter is truncated to its declared length, as before
  }
  return { value, length };
},

writeValue(buffer, parameter) {
  // ...
  const length = parameter.length != null ? parameter.length : this.maximumLength;
  if (value.length > length) {
    throw new TypeError(`Binary value of ${value.length} bytes exceeds the declared length of ${length}.`);
  }
  buffer.writeUInt16LE(value.length);
  buffer.writeBuffer(value);
}

I tried this on top of your branch against SQL Server 2025:

  • Request parameters behave exactly as they do on your branch, including truncation (length 1 still gives 0x4A), and your tests pass.
  • Rows that are too long now fail with an InputError: "Column 'c' could not be serialized" for bulk load, "Input parameter 't' could not be validated" for a TVP. The detail is in cause.
  • Side effect: a bulk-load Binary column with length: 0 now fails instead of silently storing a single 0x00 byte, which it has done in every version since at least 14.x.

Please add a bulk-load and a TVP integration test for both a shorter and a longer value: the new integration tests only cover RPC parameters, but this change affects the bytes of every row.

Smaller points

  • !== undefined should be != null in writeValue. A TVP column with length: null currently becomes Math.min(..., null, ...) === 0, and the value is sent as zero bytes. The rest of the code base uses != null.
  • Bulk load without a length. A Binary column with no length is declared binary(8000), so a 2-byte value is now stored padded to 8000 bytes. It used to fail. Char has always padded like this, so I'm fine with it, but please add a test so it's intentional.
  • The comment on resolve is inaccurate. It says TYPE_INFO matches the declared binary(n), but a null input value resolves to 8000 while declaration() emits binary(1), as your unit test asserts. Either align the two or adjust the comment.
  • Duplicated length logic. resolve now repeats the length rules from declaration() and resolveLength(), and resolveLength() (still used by bulk load) returns 0 for an empty buffer where resolve returns 1. Could resolve call resolveLength, with resolveLength changed to value.length || 1?
  • Test helper. selectBinary duplicates most of execSql. It drops the errorMessage logging and the TEDIOUS_DEBUG hook, and doesn't close the connection if connect fails. Could execSql compare Buffers with deepEqual instead?

Out of scope here

Some related problems are older than 20.3.0 and we'll handle them separately: rejecting negative, fractional and oversized lengths with a clear error, a clear error for length: 0/NaN on bulk-load columns (with the change above they no longer store 0x00, but the error doesn't say what's wrong), and the matching problems with Char/NChar and TVP columns.


Generated by Claude Code

@cheenamalhotra

Copy link
Copy Markdown
Contributor Author

writeValue now silently truncates bulk-load and TVP rows

Addressed in bf6595d: parameter truncation happens in resolve; oversized bulk-load and TVP cells fail with InputError. The null-length handling, shared length resolution, test helper, and requested row regressions are also covered.

The current HEAD passes the SQL Server CI matrix, including the integration tests. Local targeted regressions, lint and build also pass.

@David-Engel David-Engel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@arthurschreiber
arthurschreiber merged commit 8b15dba into tediousjs:master Nov 5, 2026
29 of 30 checks passed
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 20.3.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Binary parameters with length 0 or NaN are rejected by the server since 20.3.0

3 participants