Skip to content

Commit

Permalink
Fix misaligned buffer allocation method #1367
Browse files Browse the repository at this point in the history
  • Loading branch information
dmandalidis authored and mp911de committed Sep 4, 2020
1 parent ad1bac1 commit ec37e79
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/main/java/io/lettuce/core/codec/StringCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,17 @@ public void encode(String str, ByteBuf target) {
int sizeOf(String value, boolean estimate) {

if (estimate) {
return (int) (averageBytesPerChar * value.length());
return (int) averageBytesPerChar * value.length();
}

return (int) (maxBytesPerChar * value.length());
if (utf8) {
return ByteBufUtil.utf8MaxBytes(value);
}

if (ascii) {
return value.length();
}

return (int) maxBytesPerChar * value.length();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import io.lettuce.core.protocol.LettuceCharsets;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;

/**
Expand Down Expand Up @@ -125,7 +126,7 @@ void estimateSize() {
void sizeOf() {

assertThat(new StringCodec(StandardCharsets.UTF_8).sizeOf(teststring, false))
.isEqualTo(teststring.length() * 3);
.isEqualTo(ByteBufUtil.utf8MaxBytes(teststring));
assertThat(new StringCodec(StandardCharsets.US_ASCII).sizeOf(teststring, false))
.isEqualTo(teststring.length());
assertThat(new StringCodec(StandardCharsets.ISO_8859_1).sizeOf(teststring, false))
Expand Down

0 comments on commit ec37e79

Please sign in to comment.