Copilot commented on code in PR #3485:
URL: https://github.com/apache/brpc/pull/3485#discussion_r3844783262
##########
src/brpc/controller.cpp:
##########
@@ -1711,6 +1711,12 @@ void Controller::HandleStreamConnection(Socket
*host_socket) {
if (!FailedInline()) {
SetFailed(EREQUEST, "The server didn't accept the stream");
}
+ } else if (_remote_stream_settings->extra_stream_ids_size() !=
+ (int)stream_num - 1) {
+ SetFailed(ERESPONSE, "Server returned %d extra_stream_ids, "
+ "expected %d",
+ _remote_stream_settings->extra_stream_ids_size(),
+ (int)stream_num - 1);
Review Comment:
This comparison mixes signed (`extra_stream_ids_size()` returns `int`) and
unsigned (`stream_num` is `size_t`) and also computes an `expected` value as
`(int)stream_num - 1`, which can become negative if `stream_num == 0` and can
truncate if `stream_num > INT_MAX`. Consider computing `expected_extra_ids` in
a way that avoids negative values and truncation (e.g., handle `stream_num ==
0` explicitly, and compare using a single common type with range checks) so the
check and the error message are always correct.
##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1130,6 +1131,74 @@ class MyServiceWithExtraStream : public
test::EchoService {
int _n;
};
+class MyServiceWithMismatchedExtraStreamIds : public test::EchoService {
+public:
+ MyServiceWithMismatchedExtraStreamIds(size_t stream_count, int adjustment)
+ : _stream_count(stream_count), _adjustment(adjustment) {}
+
+ void Echo(::google::protobuf::RpcController* controller,
+ const ::test::EchoRequest* request,
+ ::test::EchoResponse* response,
+ ::google::protobuf::Closure* done) override {
+ brpc::ClosureGuard done_guard(done);
+ brpc::Controller* cntl = static_cast<brpc::Controller*>(controller);
+ response->set_message(request->message());
+
+ brpc::ControllerPrivateAccessor accessor(cntl);
+ brpc::StreamSettings* settings = accessor.remote_stream_settings();
+ if (_adjustment < 0) {
+ settings->mutable_extra_stream_ids()->RemoveLast();
+ } else {
+ settings->add_extra_stream_ids(settings->extra_stream_ids(0));
+ }
+
+ brpc::StreamIds response_streams;
+ ASSERT_EQ(0, brpc::StreamAccept(response_streams, *cntl, nullptr));
+ ASSERT_EQ((int)_stream_count + _adjustment,
+ (int)response_streams.size());
+ }
+
+private:
+ size_t _stream_count;
+ int _adjustment;
+};
+
+TEST_F(StreamingRpcTest, reject_mismatched_returned_stream_identifiers) {
+ const size_t STREAM_COUNT = 3;
+
+ for (int adjustment : {-1, 1}) {
+ brpc::Server server;
+ MyServiceWithMismatchedExtraStreamIds service(STREAM_COUNT,
adjustment);
+ ASSERT_EQ(0, server.AddService(
+ &service, brpc::SERVER_DOESNT_OWN_SERVICE));
+ ASSERT_EQ(0, server.Start(0, nullptr));
+
+ brpc::Channel channel;
+ ASSERT_EQ(0, channel.Init(server.listen_address(), nullptr));
+
+ brpc::Controller cntl;
+ brpc::StreamIds request_streams;
+ ASSERT_EQ(0, brpc::StreamCreate(request_streams, STREAM_COUNT, cntl,
+ nullptr));
+ ASSERT_EQ(STREAM_COUNT, request_streams.size());
+
+ test::EchoService_Stub stub(&channel);
+ stub.Echo(&cntl, &request, &response, nullptr);
+ ASSERT_TRUE(cntl.Failed());
+ ASSERT_EQ(brpc::ERESPONSE, cntl.ErrorCode());
+ ASSERT_NE(std::string::npos,
+ cntl.ErrorText().find("extra_stream_ids, expected 2"));
Review Comment:
The assertion hard-codes the expected value as `2`, even though it is
derived from `STREAM_COUNT - 1`. To make this test robust to changes in
`STREAM_COUNT`, consider formatting the expected substring using `STREAM_COUNT
- 1` (or asserting on structured fields / a less brittle substring) rather than
embedding a constant.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]