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 — Merge commit from fork · Raghuvardhan-Reddy78/netty@e9608b4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-redis/src
    • main/java/io/netty/handler/codec/redis
      • RedisDecoder.java
    • test/java/io/netty/handler/codec/redis
      • RedisDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-redis/src/main/java/io/netty/handler/codec/redis/RedisDecoder.java‎

Copy file name to clipboardExpand all lines: codec-redis/src/main/java/io/netty/handler/codec/redis/RedisDecoder.java
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,13 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
129129
}
130130
}
131131
} catch (RedisCodecException e) {
132+
// Let's discard everything
133+
in.skipBytes(in.readableBytes());
132134
resetDecoder();
133135
throw e;
134136
} catch (Exception e) {
137+
// Let's discard everything
138+
in.skipBytes(in.readableBytes());
135139
resetDecoder();
136140
throw new RedisCodecException(e);
137141
}
@@ -169,6 +173,16 @@ private boolean decodeInline(ByteBuf in, List<Object> out) throws Exception {
169173
private boolean decodeLength(ByteBuf in, List<Object> out) throws Exception {
170174
ByteBuf lineByteBuf = readLine(in);
171175
if (lineByteBuf == null) {
176+
int readableBytes = in.readableBytes();
177+
if (readableBytes <= RedisConstants.POSITIVE_LONG_MAX_LENGTH) {
178+
// fast-path
179+
return false;
180+
}
181+
boolean isNegative = in.getByte(in.readerIndex()) == '-';
182+
int capacity = RedisConstants.POSITIVE_LONG_MAX_LENGTH + (isNegative ? 1 : 0) + 1;
183+
if (readableBytes > capacity) {
184+
throw new RedisCodecException("too many characters to be a valid RESP Integer: " + readableBytes);
185+
}
172186
return false;
173187
}
174188
final long length = parseRedisNumber(lineByteBuf);
Collapse file

‎codec-redis/src/test/java/io/netty/handler/codec/redis/RedisDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-redis/src/test/java/io/netty/handler/codec/redis/RedisDecoderTest.java
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,67 @@ public void testPredefinedMessagesNotEqual() {
353353
assertNotEquals(FullBulkStringRedisMessage.EMPTY_INSTANCE, FullBulkStringRedisMessage.NULL_INSTANCE);
354354
assertNotEquals(FullBulkStringRedisMessage.NULL_INSTANCE, FullBulkStringRedisMessage.EMPTY_INSTANCE);
355355
}
356+
357+
@Test
358+
public void shouldLimitIntegerTo64IntSigned() {
359+
ByteBuf buf = Unpooled.buffer();
360+
buf.writeByte('$');
361+
for (int i = 0; i <= RedisConstants.POSITIVE_LONG_MAX_LENGTH; i++) {
362+
buf.writeByte('0');
363+
}
364+
assertFalse(channel.writeInbound(buf));
365+
366+
assertThrows(DecoderException.class, new Executable() {
367+
@Override
368+
public void execute() {
369+
channel.writeInbound(byteBufOf("1"));
370+
}
371+
});
372+
}
373+
374+
@Test
375+
public void testPositiveLongWithCr() {
376+
EmbeddedChannel channel = new EmbeddedChannel(new RedisDecoder());
377+
ByteBuf buf = Unpooled.buffer();
378+
buf.writeByte('$');
379+
for (int i = 0; i < RedisConstants.POSITIVE_LONG_MAX_LENGTH; i++) {
380+
buf.writeByte('0');
381+
}
382+
buf.writeByte('\r');
383+
384+
// 19 digits + \r = 20 bytes.
385+
// It's a valid incomplete RESP number waiting for \n.
386+
assertFalse(channel.writeInbound(buf));
387+
388+
ByteBuf buf2 = Unpooled.buffer();
389+
buf2.writeByte('\n');
390+
assertFalse(channel.writeInbound(buf2));
391+
assertFalse(channel.finish());
392+
}
393+
394+
@Test
395+
public void testGiantPayloadEndingWithCrBypassesLengthCheck() {
396+
EmbeddedChannel channel = new EmbeddedChannel(new RedisDecoder());
397+
ByteBuf buf = Unpooled.buffer();
398+
buf.writeByte('$');
399+
for (int i = 0; i < RedisConstants.POSITIVE_LONG_MAX_LENGTH; i++) {
400+
buf.writeByte('1');
401+
}
402+
assertFalse(channel.writeInbound(buf));
403+
404+
// We expect that sending 1000 more bytes will exceed the maximum valid length capacity,
405+
// regardless of whether the final byte is '\r'. It should throw a DecoderException.
406+
assertThrows(DecoderException.class, new Executable() {
407+
@Override
408+
public void execute() {
409+
for (int i = 0; i < 1000; i++) {
410+
ByteBuf chunk = Unpooled.buffer();
411+
chunk.writeByte('a');
412+
chunk.writeByte('\r');
413+
channel.writeInbound(chunk);
414+
}
415+
}
416+
});
417+
assertFalse(channel.finish());
418+
}
356419
}

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.