GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Honour the needle's reader index in ByteBufUtil.indexOf(ByteBuf, ByteBuf) by allthingssecurity · Pull Request #17682 · netty/netty

Conversation

@allthingssecurity

@allthingssecurity allthingssecurity commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Motivation:

ByteBufUtil.indexOf(ByteBuf needle, ByteBuf haystack) searches for the readable bytes of needle. Since the Two-Way implementation (#11367, 4.1.66) it can miss a match when needle.readerIndex() is not 0:

ByteBuf needle = Unpooled.copiedBuffer("abc123", CharsetUtil.US_ASCII);
needle.readerIndex(3); // "123"
ByteBufUtil.indexOf(needle, Unpooled.copiedBuffer("ab1123", CharsetUtil.US_ASCII)); // -1, expected 3

The search loops read the needle relative to its reader index (needle.getByte(i + aStartIndex)), but maxSuf(...), which computes the critical factorization, mixes indexes: j starts at the reader index (absolute) while ms starts at -1 and the loop bound m is relative. So the factorization is computed from the wrong bytes, including bytes before the reader index, or not at all when the reader index is at least m - 1. The search then skips real occurrences.

The haystack half of this regression was reported in #11963 and fixed in #11970. The existing test with a non-zero needle reader index uses a needle with all bytes distinct, where the wrong factorization happens to work. No caller in Netty passes such a needle (DelimiterBasedFrameDecoder slices its delimiters), so this only affects users of the public method.

Modification:

  • In maxSuf(...), keep j, k, ms and p relative to the needle's reader index and add it when reading bytes, like the search loops do. With reader index 0 the computation is unchanged, and the search loops are not touched.
  • New tests in ByteBufUtilTest:
    • testIndexOfNeedleWithReaderIndex: two needles with a non-zero reader index.
    • testIndexOfMatchesNaiveSearch, for needle reader index 0 to 3: 10,000 random needles and haystacks over a three-byte alphabet each, compared with a naive search.

Result:

ByteBufUtil.indexOf(needle, haystack) finds the readable bytes of needle whatever its reader index, as it did before #11367.

  • ByteBufUtilTest: 264 tests, 0 failures (15 skipped by assumptions, as before). Without the change to ByteBufUtil, testIndexOfNeedleWithReaderIndex fails (expected: <3> but was: <-1>), and testIndexOfMatchesNaiveSearch fails for needle reader index 1, 2 and 3 (missed matches, and one match reported at 11 instead of 7). Reader index 0 passes both ways.
  • buffer tests: 13944, 0 failures. ./mvnw -pl buffer -am verify -DskipTests (checkstyle and revapi) passes.

The problem was found by modelling indexOf and maxSuf in Lean 4 (a bounded check against naive search) and reproducing the counterexamples with the real class. The change was prepared with Claude Code (Claude Opus 5.5), and the commit has a Co-Authored-By trailer.

Prepared with Claude Code on behalf of allthingssecurity.

🤖 Generated with Claude Code

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@allthingssecurity @claude
Honour the needle's reader index in ByteBufUtil.indexOf(ByteBuf, Byte…
2544d64
…Buf)

Motivation:

Since the Two-Way implementation (netty#11367), ByteBufUtil.indexOf(needle,
haystack) can miss a match when needle.readerIndex() is not 0. maxSuf(...)
starts j at the reader index (absolute) while ms and the loop bound are
relative to it, so the critical factorization is computed from the wrong
bytes. netty#11970 fixed the same regression for the haystack only.

Modification:

- Keep the indexes in maxSuf(...) relative to the needle's reader index,
  like the search loops.
- Add tests to ByteBufUtilTest, including a comparison with a naive search
  for needle reader indexes 0 to 3.

Result:

indexOf(needle, haystack) finds the readable bytes of the needle whatever
its reader index.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
franz1981
franz1981 approved these changes Sep 30, 2026
View reviewed changes

@franz1981 franz1981 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Good catch!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

allthingssecurity reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@testlens-app

testlens-app Bot commented Sep 30, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

🚨 Jobs failed without executing tests 🚨

Failed Jobs without Test Failures

❌ Auto-port to 4.1 / Auto-porting to 4.1
❌ Cancel PR Jobs on Merge / cancel

🏷️ Commit: 2544d64
▶️ Tests: 0 executed
⚪️ Checks: 16/16 completed

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer normanmaurer added this to the 4.2.19.Final milestone Sep 30, 2026
@normanmaurer normanmaurer added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Sep 30, 2026
@normanmaurer
normanmaurer merged commit e66ce34 into netty:4.2 Sep 30, 2026
30 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot

netty-project-bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Could not create auto-port PR.
Got conflicts when cherry-picking onto 4.1.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot netty-project-bot mentioned this pull request Sep 30, 2026
Auto-port 5.0: Honour the needle's reader index in ByteBufUtil.indexOf(ByteBuf, ByteBuf) #17686
Merged
@netty-project-bot

netty-project-bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #17686

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Sep 30, 2026
@normanmaurer

normanmaurer commented Sep 30, 2026

Copy link
Copy Markdown
Member

Port for 4.1 : #17687

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

normanmaurer pushed a commit that referenced this pull request Sep 30, 2026
@netty-project-bot @allthingssecurity
Auto-port 5.0: Honour the needle's reader index in ByteBufUtil.indexO…
f44f36f
…f(ByteBuf, ByteBuf) (#17686)

Auto-port of #17682 to 5.0
Cherry-picked commit: e66ce34

---
Motivation:

`ByteBufUtil.indexOf(ByteBuf needle, ByteBuf haystack)` searches for the
readable bytes of `needle`. Since the Two-Way implementation (#11367,
4.1.66) it can miss a match when `needle.readerIndex()` is not 0:

```java
ByteBuf needle = Unpooled.copiedBuffer("abc123", CharsetUtil.US_ASCII);
needle.readerIndex(3); // "123"
ByteBufUtil.indexOf(needle, Unpooled.copiedBuffer("ab1123", CharsetUtil.US_ASCII)); // -1, expected 3
```

The search loops read the needle relative to its reader index
(`needle.getByte(i + aStartIndex)`), but `maxSuf(...)`, which computes
the critical factorization, mixes indexes: `j` starts at the reader
index (absolute) while `ms` starts at -1 and the loop bound `m` is
relative. So the factorization is computed from the wrong bytes,
including bytes before the reader index, or not at all when the reader
index is at least `m - 1`. The search then skips real occurrences.

The haystack half of this regression was reported in #11963 and fixed in
#11970. The existing test with a non-zero needle reader index uses a
needle with all bytes distinct, where the wrong factorization happens to
work. No caller in Netty passes such a needle
(`DelimiterBasedFrameDecoder` slices its delimiters), so this only
affects users of the public method.

Modification:

- In `maxSuf(...)`, keep `j`, `k`, `ms` and `p` relative to the needle's
reader index and add it when reading bytes, like the search loops do.
With reader index 0 the computation is unchanged, and the search loops
are not touched.
- New tests in `ByteBufUtilTest`:
- `testIndexOfNeedleWithReaderIndex`: two needles with a non-zero reader
index.
- `testIndexOfMatchesNaiveSearch`, for needle reader index 0 to 3:
10,000 random needles and haystacks over a three-byte alphabet each,
compared with a naive search.

Result:

`ByteBufUtil.indexOf(needle, haystack)` finds the readable bytes of
`needle` whatever its reader index, as it did before #11367.

Co-authored-by: allthingssecurity <[email protected]>
normanmaurer added a commit that referenced this pull request Sep 30, 2026
@normanmaurer @allthingssecurity
Port: Honour the needle's reader index in ByteBufUtil.indexOf(ByteBuf…
09f5c64
…, ByteBuf) (#17682) (#17687)

Motivation:

`ByteBufUtil.indexOf(ByteBuf needle, ByteBuf haystack)` searches for the
readable bytes of `needle`. Since the Two-Way implementation (#11367,
4.1.66) it can miss a match when `needle.readerIndex()` is not 0:

```java
ByteBuf needle = Unpooled.copiedBuffer("abc123", CharsetUtil.US_ASCII);
needle.readerIndex(3); // "123"
ByteBufUtil.indexOf(needle, Unpooled.copiedBuffer("ab1123", CharsetUtil.US_ASCII)); // -1, expected 3
```

The search loops read the needle relative to its reader index
(`needle.getByte(i + aStartIndex)`), but `maxSuf(...)`, which computes
the critical factorization, mixes indexes: `j` starts at the reader
index (absolute) while `ms` starts at -1 and the loop bound `m` is
relative. So the factorization is computed from the wrong bytes,
including bytes before the reader index, or not at all when the reader
index is at least `m - 1`. The search then skips real occurrences.

The haystack half of this regression was reported in #11963 and fixed in
needle with all bytes distinct, where the wrong factorization happens to
work. No caller in Netty passes such a needle
(`DelimiterBasedFrameDecoder` slices its delimiters), so this only
affects users of the public method.

Modification:

- In `maxSuf(...)`, keep `j`, `k`, `ms` and `p` relative to the needle's
reader index and add it when reading bytes, like the search loops do.
With reader index 0 the computation is unchanged, and the search loops
are not touched.
- New tests in `ByteBufUtilTest`:
- `testIndexOfNeedleWithReaderIndex`: two needles with a non-zero reader
index.
- `testIndexOfMatchesNaiveSearch`, for needle reader index 0 to 3:
10,000 random needles and haystacks over a three-byte alphabet each,
compared with a naive search.

Result:

`ByteBufUtil.indexOf(needle, haystack)` finds the readable bytes of
`needle` whatever its reader index, as it did before #11367.

Co-authored-by: allthingssecurity <[email protected]>
@allthingssecurity
allthingssecurity deleted the fix-bytebufutil-indexof-needle-reader-index branch October 1, 2026 02:54
@normanmaurer normanmaurer removed the needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. label Oct 16, 2026
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
@normanmaurer @allthingssecurity @lltopk
Port: Honour the needle's reader index in ByteBufUtil.indexOf(ByteBuf…
ee8f755
…, ByteBuf) (netty#17682) (netty#17687)

Motivation:

`ByteBufUtil.indexOf(ByteBuf needle, ByteBuf haystack)` searches for the
readable bytes of `needle`. Since the Two-Way implementation (netty#11367,
4.1.66) it can miss a match when `needle.readerIndex()` is not 0:

```java
ByteBuf needle = Unpooled.copiedBuffer("abc123", CharsetUtil.US_ASCII);
needle.readerIndex(3); // "123"
ByteBufUtil.indexOf(needle, Unpooled.copiedBuffer("ab1123", CharsetUtil.US_ASCII)); // -1, expected 3
```

The search loops read the needle relative to its reader index
(`needle.getByte(i + aStartIndex)`), but `maxSuf(...)`, which computes
the critical factorization, mixes indexes: `j` starts at the reader
index (absolute) while `ms` starts at -1 and the loop bound `m` is
relative. So the factorization is computed from the wrong bytes,
including bytes before the reader index, or not at all when the reader
index is at least `m - 1`. The search then skips real occurrences.

The haystack half of this regression was reported in netty#11963 and fixed in
needle with all bytes distinct, where the wrong factorization happens to
work. No caller in Netty passes such a needle
(`DelimiterBasedFrameDecoder` slices its delimiters), so this only
affects users of the public method.

Modification:

- In `maxSuf(...)`, keep `j`, `k`, `ms` and `p` relative to the needle's
reader index and add it when reading bytes, like the search loops do.
With reader index 0 the computation is unchanged, and the search loops
are not touched.
- New tests in `ByteBufUtilTest`:
- `testIndexOfNeedleWithReaderIndex`: two needles with a non-zero reader
index.
- `testIndexOfMatchesNaiveSearch`, for needle reader index 0 to 3:
10,000 random needles and haystacks over a three-byte alphabet each,
compared with a naive search.

Result:

`ByteBufUtil.indexOf(needle, haystack)` finds the readable bytes of
`needle` whatever its reader index, as it did before netty#11367.

Co-authored-by: allthingssecurity <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@franz1981 franz1981 franz1981 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.2.19.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@allthingssecurity @netty-project-bot @normanmaurer @franz1981
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.