This is an automated email from the ASF dual-hosted git repository.
wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git
The following commit(s) were added to refs/heads/master by this push:
new 74963326 bound grpc-timeout value before converting to microseconds
(#3591)
74963326 is described below
commit 74963326c2c581d36bdf55c799a5a5c513187c3f
Author: sahvx655-wq <[email protected]>
AuthorDate: Sun Oct 18 08:08:06 2026 +0530
bound grpc-timeout value before converting to microseconds (#3591)
* bound grpc-timeout value before converting to microseconds
* clarify what the grpc-timeout bound rejects
---
src/brpc/grpc.cpp | 13 +++++++++++++
test/brpc_grpc_protocol_unittest.cpp | 10 +++++++++-
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/src/brpc/grpc.cpp b/src/brpc/grpc.cpp
index bde72a2f..c58b14e3 100644
--- a/src/brpc/grpc.cpp
+++ b/src/brpc/grpc.cpp
@@ -169,6 +169,11 @@ void PercentDecode(const std::string& str, std::string*
str_out) {
}
}
+// The gRPC wire format limits TimeoutValue to "an ASCII string of at most 8
+// digits", so this is the largest value a conforming peer can send. See
+// doc/PROTOCOL-HTTP2.md in grpc/grpc.
+static const int64_t MAX_GRPC_TIMEOUT_VALUE = 99999999;
+
int64_t ConvertGrpcTimeoutToUS(const std::string* grpc_timeout) {
if (!grpc_timeout || grpc_timeout->empty()) {
return -1;
@@ -186,6 +191,14 @@ int64_t ConvertGrpcTimeoutToUS(const std::string*
grpc_timeout) {
if ((size_t)(endptr - grpc_timeout->data()) != grpc_timeout->size() - 1) {
return -1;
}
+ // A value outside this range is not a valid timeout, and a large one makes
+ // the conversions below overflow int64_t, so treat it as no deadline like
+ // any other malformed value. strtol() saturates at LONG_MAX/LONG_MIN for
+ // input that does not fit, which is rejected here as well. Zero keeps
+ // going through and yields an already-expired deadline, as before.
+ if (timeout_value < 0 || timeout_value > MAX_GRPC_TIMEOUT_VALUE) {
+ return -1;
+ }
switch (*endptr) {
case 'H':
return timeout_value * 3600 * 1000000;
diff --git a/test/brpc_grpc_protocol_unittest.cpp
b/test/brpc_grpc_protocol_unittest.cpp
index 6136dda7..41107b6e 100644
--- a/test/brpc_grpc_protocol_unittest.cpp
+++ b/test/brpc_grpc_protocol_unittest.cpp
@@ -233,7 +233,15 @@ TEST_F(GrpcTest, GrpcTimeOut) {
"HHHH", "-1",
"112", "-1",
"H999m", "-1",
- "", "-1"
+ "", "-1",
+ // out of range: more digits than the gRPC wire format allows, or
+ // negative. The large ones overflowed the conversion to microseconds.
+ "100000000S", "-1",
+ "18446744073710S", "-1",
+ "2562047789H", "-1",
+ "9223372036854775807u", "-1",
+ "9223372036854775807n", "-1",
+ "-1S", "-1"
};
// test all timeout format
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]