Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ByteProcessor shouldn't throw checked exception #10767

Merged
merged 1 commit into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 6 additions & 27 deletions buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -1262,58 +1262,37 @@ public int bytesBefore(int index, int length, byte value) {
@Override
public int forEachByte(ByteProcessor processor) {
ensureAccessible();
try {
return forEachByteAsc0(readerIndex, writerIndex, processor);
} catch (Exception e) {
PlatformDependent.throwException(e);
return -1;
}
return forEachByteAsc0(readerIndex, writerIndex, processor);
}

@Override
public int forEachByte(int index, int length, ByteProcessor processor) {
checkIndex(index, length);
try {
return forEachByteAsc0(index, index + length, processor);
} catch (Exception e) {
PlatformDependent.throwException(e);
return -1;
}
return forEachByteAsc0(index, index + length, processor);
}

int forEachByteAsc0(int start, int end, ByteProcessor processor) throws Exception {
int forEachByteAsc0(int start, int end, ByteProcessor processor) {
for (; start < end; ++start) {
if (!processor.process(_getByte(start))) {
return start;
}
}

return -1;
}

@Override
public int forEachByteDesc(ByteProcessor processor) {
ensureAccessible();
try {
return forEachByteDesc0(writerIndex - 1, readerIndex, processor);
} catch (Exception e) {
PlatformDependent.throwException(e);
return -1;
}
return forEachByteDesc0(writerIndex - 1, readerIndex, processor);
}

@Override
public int forEachByteDesc(int index, int length, ByteProcessor processor) {
checkIndex(index, length);
try {
return forEachByteDesc0(index + length - 1, index, processor);
} catch (Exception e) {
PlatformDependent.throwException(e);
return -1;
}
return forEachByteDesc0(index + length - 1, index, processor);
}

int forEachByteDesc0(int rStart, final int rEnd, ByteProcessor processor) throws Exception {
int forEachByteDesc0(int rStart, final int rEnd, ByteProcessor processor) {
for (; rStart >= rEnd; --rStart) {
if (!processor.process(_getByte(rStart))) {
return rStart;
Expand Down
4 changes: 2 additions & 2 deletions buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ public Iterator<ByteBuf> iterator() {
}

@Override
protected int forEachByteAsc0(int start, int end, ByteProcessor processor) throws Exception {
protected int forEachByteAsc0(int start, int end, ByteProcessor processor) {
if (end <= start) {
return -1;
}
Expand All @@ -686,7 +686,7 @@ protected int forEachByteAsc0(int start, int end, ByteProcessor processor) throw
}

@Override
protected int forEachByteDesc0(int rStart, int rEnd, ByteProcessor processor) throws Exception {
protected int forEachByteDesc0(int rStart, int rEnd, ByteProcessor processor) {
if (rEnd > rStart) { // rStart *and* rEnd are inclusive
return -1;
}
Expand Down
10 changes: 5 additions & 5 deletions buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2284,7 +2284,7 @@ public void testForEachByte() {
int i = CAPACITY / 4;

@Override
public boolean process(byte value) throws Exception {
public boolean process(byte value) {
assertThat(value, is((byte) (i + 1)));
lastIndex.set(i);
i ++;
Expand All @@ -2307,7 +2307,7 @@ public void testForEachByteAbort() {
int i = CAPACITY / 3;

@Override
public boolean process(byte value) throws Exception {
public boolean process(byte value) {
assertThat(value, is((byte) (i + 1)));
if (i == stop) {
return false;
Expand Down Expand Up @@ -4608,7 +4608,7 @@ public void testForEachByteDesc2() {
private int index = bytes.length - 1;

@Override
public boolean process(byte value) throws Exception {
public boolean process(byte value) {
bytes[index--] = value;
return true;
}
Expand All @@ -4631,7 +4631,7 @@ public void testForEachByte2() {
private int index;

@Override
public boolean process(byte value) throws Exception {
public boolean process(byte value) {
bytes[index++] = value;
return true;
}
Expand Down Expand Up @@ -4804,7 +4804,7 @@ public void close() {

private static final class TestByteProcessor implements ByteProcessor {
@Override
public boolean process(byte value) throws Exception {
public boolean process(byte value) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io.netty.handler.codec.ValueConverter;
import io.netty.util.AsciiString;
import io.netty.util.ByteProcessor;
import io.netty.util.internal.PlatformDependent;

import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -50,14 +49,10 @@ public class DefaultHttpHeaders extends HttpHeaders {
};
static final NameValidator<CharSequence> HttpNameValidator = name -> {
if (name == null || name.length() == 0) {
throw new IllegalArgumentException("empty headers are not allowed [" + name + "]");
throw new IllegalArgumentException("empty headers are not allowed [" + name + ']');
}
if (name instanceof AsciiString) {
try {
((AsciiString) name).forEachByte(HEADER_NAME_VALIDATOR);
} catch (Exception e) {
PlatformDependent.throwException(e);
}
((AsciiString) name).forEachByte(HEADER_NAME_VALIDATOR);
} else {
// Go through each character in the name
for (int index = 0; index < name.length(); ++index) {
Expand All @@ -73,7 +68,7 @@ public DefaultHttpHeaders() {
}

/**
* <b>Warning!</b> Setting <code>validate</code> to <code>false</code> will mean that Netty won't
* <b>Warning!</b> Setting {@code validate} to {@code false} will mean that Netty won't
* validate & protect against user-supplied header values that are malicious.
* This can leave your server implementation vulnerable to
* <a href="https://cwe.mitre.org/data/definitions/113.html">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ public void reset() {
}

@Override
public boolean process(byte value) throws Exception {
public boolean process(byte value) {
char nextByte = (char) (value & 0xFF);
if (nextByte == HttpConstants.LF) {
int len = seq.length();
Expand Down Expand Up @@ -983,7 +983,7 @@ public AppendableCharSequence parse(ByteBuf buffer) {
}

@Override
public boolean process(byte value) throws Exception {
public boolean process(byte value) {
if (currentState == State.SKIP_CONTROL_CHARS) {
char c = (char) (value & 0xFF);
if (Character.isISOControl(c) || Character.isWhitespace(c)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ public void removeHttpDataFromClean(InterfaceHttpData data) {

private static final class UrlEncodedDetector implements ByteProcessor {
@Override
public boolean process(byte value) throws Exception {
public boolean process(byte value) {
return value != '%' && value != '+';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void finish() {
}

@Override
public boolean process(byte b) throws Exception {
public boolean process(byte b) {
byte type = TYPES[b & 0xFF];

codep = state != UTF8_ACCEPT ? b & 0x3f | codep << 6 : 0xff >> type & b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,7 @@ public class DefaultHttp2Headers
"empty headers are not allowed [%s]", name));
}
if (name instanceof AsciiString) {
final int index;
try {
index = ((AsciiString) name).forEachByte(HTTP2_NAME_VALIDATOR_PROCESSOR);
} catch (Http2Exception e) {
PlatformDependent.throwException(e);
return;
} catch (Throwable t) {
PlatformDependent.throwException(connectionError(PROTOCOL_ERROR, t,
"unexpected error. invalid header name [%s]", name));
return;
}

int index = ((AsciiString) name).forEachByte(HTTP2_NAME_VALIDATOR_PROCESSOR);
if (index != -1) {
PlatformDependent.throwException(connectionError(PROTOCOL_ERROR,
"invalid header name [%s]", name));
Expand Down Expand Up @@ -109,7 +98,7 @@ public DefaultHttp2Headers(boolean validate, int arraySizeHint) {

@Override
public Http2Headers clear() {
this.firstNonPseudo = head;
firstNonPseudo = head;
return super.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ public void encode(ByteBuf out, CharSequence data) {
requireNonNull(out, "out");
if (data instanceof AsciiString) {
AsciiString string = (AsciiString) data;
encodeProcessor.out = out;
try {
encodeProcessor.out = out;
string.forEachByte(encodeProcessor);
} catch (Exception e) {
PlatformDependent.throwException(e);
} finally {
encodeProcessor.end();
}
Expand Down Expand Up @@ -118,14 +116,9 @@ private void encodeSlowPath(ByteBuf out, CharSequence data) {
int getEncodedLength(CharSequence data) {
if (data instanceof AsciiString) {
AsciiString string = (AsciiString) data;
try {
encodedLengthProcessor.reset();
string.forEachByte(encodedLengthProcessor);
return encodedLengthProcessor.length();
} catch (Exception e) {
PlatformDependent.throwException(e);
return -1;
}
encodedLengthProcessor.reset();
string.forEachByte(encodedLengthProcessor);
return encodedLengthProcessor.length();
} else {
return getEncodedLengthSlowPath(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,23 +427,17 @@ private static CharSequenceMap<AsciiString> toLowercaseMap(Iterator<? extends Ch

while (valuesIter.hasNext()) {
AsciiString lowerCased = AsciiString.of(valuesIter.next()).toLowerCase();
try {
int index = lowerCased.forEachByte(FIND_COMMA);
if (index != -1) {
int start = 0;
do {
result.add(lowerCased.subSequence(start, index, false).trim(), EMPTY_STRING);
start = index + 1;
} while (start < lowerCased.length() &&
(index = lowerCased.forEachByte(start, lowerCased.length() - start, FIND_COMMA)) != -1);
result.add(lowerCased.subSequence(start, lowerCased.length(), false).trim(), EMPTY_STRING);
} else {
result.add(lowerCased.trim(), EMPTY_STRING);
}
} catch (Exception e) {
// This is not expect to happen because FIND_COMMA never throws but must be caught
// because of the ByteProcessor interface.
throw new IllegalStateException(e);
int index = lowerCased.forEachByte(FIND_COMMA);
if (index != -1) {
int start = 0;
do {
result.add(lowerCased.subSequence(start, index, false).trim(), EMPTY_STRING);
start = index + 1;
} while (start < lowerCased.length() &&
(index = lowerCased.forEachByte(start, lowerCased.length() - start, FIND_COMMA)) != -1);
result.add(lowerCased.subSequence(start, lowerCased.length(), false).trim(), EMPTY_STRING);
} else {
result.add(lowerCased.trim(), EMPTY_STRING);
}
}
return result;
Expand Down Expand Up @@ -489,27 +483,21 @@ public static void toHttp2Headers(HttpHeaders inHeaders, Http2Headers out) {
AsciiString value = AsciiString.of(entry.getValue());
// split up cookies to allow for better compression
// https://tools.ietf.org/html/rfc7540#section-8.1.2.5
try {
int index = value.forEachByte(FIND_SEMI_COLON);
if (index != -1) {
int start = 0;
do {
out.add(COOKIE, value.subSequence(start, index, false));
// skip 2 characters "; " (see https://tools.ietf.org/html/rfc6265#section-4.2.1)
start = index + 2;
} while (start < value.length() &&
(index = value.forEachByte(start, value.length() - start, FIND_SEMI_COLON)) != -1);
if (start >= value.length()) {
throw new IllegalArgumentException("cookie value is of unexpected format: " + value);
}
out.add(COOKIE, value.subSequence(start, value.length(), false));
} else {
out.add(COOKIE, value);
int index = value.forEachByte(FIND_SEMI_COLON);
if (index != -1) {
int start = 0;
do {
out.add(COOKIE, value.subSequence(start, index, false));
// skip 2 characters "; " (see https://tools.ietf.org/html/rfc6265#section-4.2.1)
start = index + 2;
} while (start < value.length() &&
(index = value.forEachByte(start, value.length() - start, FIND_SEMI_COLON)) != -1);
if (start >= value.length()) {
throw new IllegalArgumentException("cookie value is of unexpected format: " + value);
}
} catch (Exception e) {
// This is not expect to happen because FIND_SEMI_COLON never throws but must be caught
// because of the ByteProcessor interface.
throw new IllegalStateException(e);
out.add(COOKIE, value.subSequence(start, value.length(), false));
} else {
out.add(COOKIE, value);
}
} else {
out.add(aName, entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private static final class ToPositiveLongProcessor implements ByteProcessor {
private long result;

@Override
public boolean process(byte value) throws Exception {
public boolean process(byte value) {
if (value < '0' || value > '9') {
throw new RedisCodecException("bad byte in number: " + value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ AppendableCharSequence charSequence() {
}

@Override
public boolean process(byte nextByte) throws Exception {
public boolean process(byte nextByte) {
if (nextByte == StompConstants.CR) {
++lineLength;
return true;
Expand Down Expand Up @@ -353,7 +353,7 @@ boolean parseHeader(StompHeaders headers, ByteBuf buf) {
}

@Override
public boolean process(byte nextByte) throws Exception {
public boolean process(byte nextByte) {
if (nextByte == StompConstants.COLON) {
if (name == null) {
AppendableCharSequence charSeq = charSequence();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ ByteBuf decode(ByteBuf src, int off, int len, ByteBufAllocator allocator, Base64
}

@Override
public boolean process(byte value) throws Exception {
public boolean process(byte value) {
if (value > 0) {
byte sbiDecode = decodabet[value];
if (sbiDecode >= WHITE_SPACE_ENC) { // White space, Equals sign or better
Expand Down