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 — Auto-port 5.0: Fix Snappy.encode(...) for input with a non-zero reade… · netty/netty@855623a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-compression/src
    • main/java/io/netty/handler/codec/compression
      • Snappy.java
    • test/java/io/netty/handler/codec/compression
      • SnappyTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-compression/src/main/java/io/netty/handler/codec/compression/Snappy.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/Snappy.java
+7-6Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public void encode(final ByteBuf in, final ByteBuf out, final int length) {
9393

9494
int inIndex = in.readerIndex();
9595
final int baseIndex = inIndex;
96+
final int endIndex = baseIndex + length;
9697

9798
int hashTableSize = MathUtil.findNextPositivePowerOfTwo(length);
9899
hashTableSize = Math.min(hashTableSize, MAX_HT_SIZE);
@@ -101,7 +102,7 @@ public void encode(final ByteBuf in, final ByteBuf out, final int length) {
101102

102103
int nextEmit = inIndex;
103104

104-
if (length - inIndex >= MIN_COMPRESSIBLE_BYTES) {
105+
if (length >= MIN_COMPRESSIBLE_BYTES) {
105106
int nextHash = hash(in, ++inIndex, shift);
106107
outer: while (true) {
107108
int skip = 32;
@@ -115,7 +116,7 @@ public void encode(final ByteBuf in, final ByteBuf out, final int length) {
115116
nextIndex = inIndex + bytesBetweenHashLookups;
116117

117118
// We need at least 4 remaining bytes to read the hash
118-
if (nextIndex > length - 4) {
119+
if (nextIndex > endIndex - 4) {
119120
break outer;
120121
}
121122

@@ -134,14 +135,14 @@ public void encode(final ByteBuf in, final ByteBuf out, final int length) {
134135
int insertTail;
135136
do {
136137
int base = inIndex;
137-
int matched = 4 + findMatchingLength(in, candidate + 4, inIndex + 4, length);
138+
int matched = 4 + findMatchingLength(in, candidate + 4, inIndex + 4, endIndex);
138139
inIndex += matched;
139140
int offset = base - candidate;
140141
encodeCopy(out, offset, matched);
141142
in.readerIndex(in.readerIndex() + matched);
142143
insertTail = inIndex - 1;
143144
nextEmit = inIndex;
144-
if (inIndex >= length - 4) {
145+
if (inIndex >= endIndex - 4) {
145146
break outer;
146147
}
147148

@@ -159,8 +160,8 @@ public void encode(final ByteBuf in, final ByteBuf out, final int length) {
159160
}
160161

161162
// If there are any remaining characters, write them out as a literal
162-
if (nextEmit < length) {
163-
encodeLiteral(in, out, length - nextEmit);
163+
if (nextEmit < endIndex) {
164+
encodeLiteral(in, out, endIndex - nextEmit);
164165
}
165166
}
166167

Collapse file

‎codec-compression/src/test/java/io/netty/handler/codec/compression/SnappyTest.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/SnappyTest.java
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,35 @@ public void encodeAndDecodeLongTextUsesCopy() throws Exception {
247247
outDecoded.release();
248248
}
249249

250+
@Test
251+
public void encodeWithNonZeroReaderIndex() throws Exception {
252+
String srcStr = "Netty has been designed carefully with the experiences " +
253+
"earned from the implementation of a lot of protocols " +
254+
"such as FTP, SMTP, HTTP, and various binary and " +
255+
"text-based legacy protocols";
256+
byte[] src = srcStr.getBytes(StandardCharsets.US_ASCII);
257+
ByteBuf expectedIn = Unpooled.wrappedBuffer(src);
258+
ByteBuf expected = Unpooled.buffer();
259+
snappy.encode(expectedIn, expected, src.length);
260+
261+
ByteBuf in = Unpooled.buffer().writeZero(16).writeBytes(src);
262+
in.skipBytes(16);
263+
ByteBuf out = Unpooled.buffer();
264+
snappy.encode(in, out, in.readableBytes());
265+
assertEquals(expected, out, "Encoded result must not depend on the reader index");
266+
267+
ByteBuf outDecoded = Unpooled.buffer();
268+
snappy.decode(out, outDecoded);
269+
assertEquals(CharBuffer.wrap(srcStr),
270+
CharBuffer.wrap(outDecoded.getCharSequence(0, outDecoded.writerIndex(), CharsetUtil.US_ASCII)));
271+
272+
expectedIn.release();
273+
expected.release();
274+
in.release();
275+
out.release();
276+
outDecoded.release();
277+
}
278+
250279
@Test
251280
public void testCalculateChecksum() {
252281
ByteBuf input = Unpooled.wrappedBuffer(new byte[] {

0 commit comments

Comments
0 (0)
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.