Skip to content

Enable buffer bounds checks by default#6491

Open
NinoFloris wants to merge 5 commits intomainfrom
enable-buffer-bounds-checks
Open

Enable buffer bounds checks by default#6491
NinoFloris wants to merge 5 commits intomainfrom
enable-buffer-bounds-checks

Conversation

@NinoFloris
Copy link
Copy Markdown
Member

@NinoFloris NinoFloris commented Mar 12, 2026

And remove buffered requirement check. This can be done because any buffered converter that will actually attempt to read data that wasn't properly buffered will now just fail due to the buffer bounds checks instead.

From a perf perspective I couldn't find a regression in the types we benchmark, though I assume it might slightly impact complex structures like TsVector, which may do a lot of individual reads. For single read buffered converters it's actually an improvement because the bounds check is much cheaper than the requirement check.

Copilot AI review requested due to automatic review settings March 12, 2026 08:56
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enables buffer bounds checking unconditionally in the low-level read buffer and simplifies buffered converter read/write paths by removing the explicit “buffer requirement respected” guard, relying on the new bounds checks to fail when attempting to read beyond what’s currently buffered.

Changes:

  • Make NpgsqlReadBuffer bounds checks always-on (no longer gated by assertion toggles), and use them in additional read helpers.
  • Move the “read scope” (field-size) bounds-check toggle into PgReader and rename CheckBoundsCheckScopeBounds.
  • Remove PgBufferedConverter<T>’s upfront “buffer requirement” enforcement checks for reads/writes.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Npgsql/Internal/PgReader.cs Introduces CheckReadScopeBounds, renames/updates scope bounds checks, and adjusts some read helpers to use buffer APIs.
src/Npgsql/Internal/PgBufferedConverter.cs Removes explicit buffered requirement checks from Read/Write.
src/Npgsql/Internal/NpgsqlReadBuffer.cs Makes bounds checks unconditional and replaces some Debug.Assert calls with runtime checks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@NinoFloris NinoFloris force-pushed the enable-buffer-bounds-checks branch from 2a9fe13 to 4d22736 Compare March 12, 2026 09:03
Copilot AI review requested due to automatic review settings March 12, 2026 16:10
@NinoFloris NinoFloris force-pushed the enable-buffer-bounds-checks branch 2 times, most recently from 97a5c07 to 1afcfce Compare March 12, 2026 16:11
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@NinoFloris NinoFloris force-pushed the enable-buffer-bounds-checks branch from 1afcfce to 2c5ed17 Compare March 12, 2026 16:31
@NinoFloris
Copy link
Copy Markdown
Member Author

NinoFloris commented Mar 12, 2026

Vibe coded some faster read buffer bookkeeping so we can also enable field bounds checks. This adds minimal cost compared to a direct call to the read buffer (5% on an int read vs GetInt32()).

Compared to the first iteration of this PR (just buffer bounds checks), we actually improved perf in absolute sense due to the faster bookkeeping.

Benchmark used: https://gist.github.com/NinoFloris/7ecb7b966a66bff916502fa8f6614564

EDIT: removed accidental local commit working around SDK 10.0.200 issues.

Copilot AI review requested due to automatic review settings March 13, 2026 00:58
@NinoFloris NinoFloris force-pushed the enable-buffer-bounds-checks branch from 2c5ed17 to 94a35ab Compare March 13, 2026 00:58
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@NinoFloris NinoFloris force-pushed the enable-buffer-bounds-checks branch from 94a35ab to b9a740d Compare March 25, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants