Skip to content

Commit

Permalink
Fix race condition in CharsetValidator
Browse files Browse the repository at this point in the history
The `CharsetValidator` has a static singleton instance at
`BinaryTruncator.DEFAULT_UTF8_TRUNCATOR.validator`, so it can be
accessed from multiple threads. Before the change, all threads would
operate on a shared "dummy buffer" for decoding.
  • Loading branch information
findepi committed Sep 26, 2023
1 parent 5154d04 commit 4837979
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ enum Validity {
}

private static class CharsetValidator {
private final CharBuffer dummyBuffer = CharBuffer.allocate(1024);
private final ThreadLocal<CharBuffer> dummyBuffer = ThreadLocal.withInitial(() -> CharBuffer.allocate(1024));
private final CharsetDecoder decoder;

CharsetValidator(Charset charset) {
Expand All @@ -50,11 +50,13 @@ private static class CharsetValidator {
}

Validity checkValidity(ByteBuffer buffer) {
// TODO this is currently used for UTF-8 only, so validity check could be done without copying.
CharBuffer charBuffer = dummyBuffer.get();
int pos = buffer.position();
CoderResult result = CoderResult.OVERFLOW;
while (result.isOverflow()) {
dummyBuffer.clear();
result = decoder.decode(buffer, dummyBuffer, true);
charBuffer.clear();
result = decoder.decode(buffer, charBuffer, true);
}
buffer.position(pos);
if (result.isUnderflow()) {
Expand Down

0 comments on commit 4837979

Please sign in to comment.