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 — Honour the needle's reader index in ByteBufUtil.indexOf(ByteBuf, ByteBuf) by allthingssecurity · Pull Request #17682 · netty/netty Conversation
Contributor
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) 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
approved these changes
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. All reactions
|
There was an error while loading. Please reload this page.
🚨 Jobs failed without executing tests 🚨Failed Jobs without Test Failures❌ Auto-port to 4.1 / Auto-porting to 4.1 🏷️ Commit: 2544d64 Rerun ControlsClick the checkbox to trigger a rerun:
Learn more about TestLens at testlens.app/docs. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Could not create auto-port PR. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Auto-port PR for 5.0: #17686 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Port for 4.1 : #17687 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…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]>
…, 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]>
…, 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]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.