Skip to content

Commit 2bbff44

Browse files
authored
RATIS-2402. Fix CallId overflow when parsing gRPC metadata. (#1344)
1 parent b940b65 commit 2bbff44

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static long getCallId(Throwable t) {
146146
final Metadata trailers = ((StatusRuntimeException)t).getTrailers();
147147
if (trailers != null) {
148148
final String callId = trailers.get(CALL_ID);
149-
return callId != null ? Integer.parseInt(callId) : -1;
149+
return callId != null ? Long.parseUnsignedLong(callId) : -1;
150150
}
151151
}
152152
return -1;

ratis-test/src/test/java/org/apache/ratis/grpc/TestCustomGrpcServices.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.ratis.test.proto.GreeterGrpc;
2929
import org.apache.ratis.test.proto.HelloReply;
3030
import org.apache.ratis.test.proto.HelloRequest;
31+
import org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException;
3132
import org.apache.ratis.thirdparty.io.grpc.ManagedChannel;
3233
import org.apache.ratis.thirdparty.io.grpc.ManagedChannelBuilder;
3334
import org.apache.ratis.thirdparty.io.grpc.netty.NettyServerBuilder;
@@ -202,4 +203,23 @@ static void sendAndAssertReply(String name, GreeterClient client, GreeterImpl gr
202203
final String expected = greeter.toReply(name);
203204
Assertions.assertEquals(expected, computed);
204205
}
206+
207+
@Test
208+
public void testGetCallIdWithLargeValue() {
209+
long largeCallId = (long) Integer.MAX_VALUE + 1L;
210+
StatusRuntimeException ex = GrpcUtil.wrapException(new IOException("test"), largeCallId);
211+
Assertions.assertEquals(largeCallId, GrpcUtil.getCallId(ex));
212+
}
213+
214+
@Test
215+
public void testGetCallIdWithMissingValue() {
216+
StatusRuntimeException ex = GrpcUtil.wrapException(new IOException("test"));
217+
Assertions.assertEquals(-1L, GrpcUtil.getCallId(ex));
218+
}
219+
220+
@Test
221+
public void testGetCallIdWithZeroValue() {
222+
StatusRuntimeException ex = GrpcUtil.wrapException(new IOException("test"), 0L);
223+
Assertions.assertEquals(-1L, GrpcUtil.getCallId(ex));
224+
}
205225
}

0 commit comments

Comments
 (0)