GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |
GeezerVison is showing github.com — Auto-port 5.0: Honour the needle's reader index in ByteBufUtil.indexOf(ByteBuf, ByteBuf) by netty-project-bot · Pull Request #17686 · netty/netty Conversation
Contributor
Auto-port of #17682 to 5.0 Motivation:
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 ( 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 ( Modification:
Result:
The problem was found by modelling 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…Buf) (#17682) 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. (cherry picked from commit e66ce34)
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ReviewersNo reviewsUh oh!
There was an error while loading. Please reload this page. AssigneesNo one assignedLabels
None yet
ProjectsDevelopmentSuccessfully merging this pull request may close these issues. Uh oh!
There was an error while loading. Please reload this page. 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. |