-
Notifications
You must be signed in to change notification settings - Fork 210
Throw ProtocolError on packet decoding failures #564
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| #include "columns/factory.h" | ||
|
|
||
| #include <cassert> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <sstream> | ||
| #include <system_error> | ||
|
|
@@ -157,7 +158,6 @@ struct ProfileEvents { | |
| struct EndOfStream { | ||
| }; | ||
| using DecodedPacket = std::variant< | ||
| std::monostate, | ||
| Block, | ||
| ServerException, | ||
| Profile, | ||
|
|
@@ -247,7 +247,17 @@ class Client::Impl { | |
| bool ReceiveData(Block & block); | ||
|
|
||
| /// Reads exception packet form input stream. | ||
| bool ReceiveException(bool rethrow = false, ServerError * error = nullptr); | ||
| bool ReceiveException(ServerError & error); | ||
|
|
||
| bool ReceiveProfileInfo(Profile & profile); | ||
|
|
||
| bool ReceiveProgress(Progress & progress); | ||
|
|
||
| bool ReceiveLog(Block & block); | ||
|
|
||
| bool ReceiveTableColumns(); | ||
|
|
||
| bool ReceiveProfileEvents(Block & block); | ||
|
|
||
| void WriteBlock(const Block& block, OutputStream& output); | ||
|
|
||
|
|
@@ -386,7 +396,9 @@ std::optional<Block> Client::Impl::NextBlock() { | |
| return {std::move(block)}; | ||
| } | ||
| case VariantIndex<ServerError, decltype(packet)>(): | ||
| case VariantIndex<std::monostate, decltype(packet)>(): | ||
| // Normally `ReceivePacket()` throws on server exceptions, but it can be | ||
| // suppressed by `ClientOptions::SetRethrowException`. In that case the | ||
| // error marks the end of the response. | ||
| case VariantIndex<EndOfStream, decltype(packet)>(): | ||
| ResetState(); | ||
| return std::nullopt; | ||
|
|
@@ -674,7 +686,7 @@ DecodedPacket Client::Impl::ReceivePacket(uint64_t* server_packet) { | |
| uint64_t packet_type = 0; | ||
|
|
||
| if (!WireFormat::ReadVarint64(*input_, &packet_type)) { | ||
| return {}; | ||
| throw ProtocolError{"can't read packet type from input stream"}; | ||
| } | ||
| if (server_packet) { | ||
| *server_packet = packet_type; | ||
|
|
@@ -684,76 +696,37 @@ DecodedPacket Client::Impl::ReceivePacket(uint64_t* server_packet) { | |
| case ServerCodes::Data: { | ||
| Block ret{}; | ||
| if (!ReceiveData(ret)) { | ||
| throw ProtocolError("can't read data packet from input stream"); | ||
| throw ProtocolError{"can't read data packet from input stream"}; | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| case ServerCodes::Exception: { | ||
| ServerError ret{std::make_shared<Exception>()}; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is okay for this rare exception path. |
||
| if (!ReceiveException(false, &ret)) { | ||
| throw ProtocolError("can't read exception packet from input stream"); | ||
| if (!ReceiveException(ret)) { | ||
| throw ProtocolError{"server reported an error, but the exception packet could not be decoded (error details lost)"}; | ||
| } | ||
|
|
||
| if (options_.rethrow_exceptions) { | ||
| throw ret; | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| case ServerCodes::ProfileInfo: { | ||
| Profile ret{}; | ||
|
|
||
| if (!WireFormat::ReadUInt64(*input_, &ret.rows)) { | ||
| return {}; | ||
| } | ||
| if (!WireFormat::ReadUInt64(*input_, &ret.blocks)) { | ||
| return {}; | ||
| } | ||
| if (!WireFormat::ReadUInt64(*input_, &ret.bytes)) { | ||
| return {}; | ||
| } | ||
| if (!WireFormat::ReadFixed(*input_, &ret.applied_limit)) { | ||
| return {}; | ||
| if (!ReceiveProfileInfo(ret)) { | ||
| throw ProtocolError{"can't read profile info packet from input stream"}; | ||
| } | ||
| if (!WireFormat::ReadUInt64(*input_, &ret.rows_before_limit)) { | ||
| return {}; | ||
| } | ||
| if (!WireFormat::ReadFixed(*input_, &ret.calculated_rows_before_limit)) { | ||
| return {}; | ||
| } | ||
|
|
||
| if (events_) { | ||
| events_->OnProfile(ret); | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| case ServerCodes::Progress: { | ||
| Progress ret{}; | ||
|
|
||
| if (!WireFormat::ReadUInt64(*input_, &ret.rows)) { | ||
| return {}; | ||
| } | ||
| if (!WireFormat::ReadUInt64(*input_, &ret.bytes)) { | ||
| return {}; | ||
| } | ||
| if constexpr(DMBS_PROTOCOL_REVISION >= DBMS_MIN_REVISION_WITH_TOTAL_ROWS_IN_PROGRESS) { | ||
| if (!WireFormat::ReadUInt64(*input_, &ret.total_rows)) { | ||
| return {}; | ||
| } | ||
| } | ||
| if (server_info_.revision >= DBMS_MIN_REVISION_WITH_CLIENT_WRITE_INFO) | ||
| { | ||
| if (!WireFormat::ReadUInt64(*input_, &ret.written_rows)) { | ||
| return {}; | ||
| } | ||
| if (!WireFormat::ReadUInt64(*input_, &ret.written_bytes)) { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| if (events_) { | ||
| events_->OnProgress(ret); | ||
| if (!ReceiveProgress(ret)) { | ||
| throw ProtocolError{"can't read progress packet from input stream"}; | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -773,63 +746,40 @@ DecodedPacket Client::Impl::ReceivePacket(uint64_t* server_packet) { | |
| } | ||
|
|
||
| case ServerCodes::Log: { | ||
| // log tag | ||
| if (!WireFormat::SkipString(*input_)) { | ||
| return {}; | ||
| } | ||
| Log ret; | ||
|
|
||
| // Use uncompressed stream since log blocks usually contain only one row | ||
| if (!ReadBlock(*input_, &ret.block)) { | ||
| return {}; | ||
| } | ||
|
|
||
| if (events_) { | ||
| events_->OnServerLog(ret.block); | ||
| if (!ReceiveLog(ret.block)) { | ||
| throw ProtocolError{"can't read log packet from input stream"}; | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| case ServerCodes::TableColumns: { | ||
| // external table name | ||
| if (!WireFormat::SkipString(*input_)) { | ||
| return {}; | ||
| } | ||
|
|
||
| // columns metadata | ||
| if (!WireFormat::SkipString(*input_)) { | ||
| return {}; | ||
| if (!ReceiveTableColumns()) { | ||
| throw ProtocolError{"can't read table columns packet from input stream"}; | ||
| } | ||
| return TableColumns{}; | ||
| } | ||
|
|
||
| case ServerCodes::ProfileEvents: { | ||
| if (!WireFormat::SkipString(*input_)) { | ||
| return {}; | ||
| } | ||
|
|
||
| ProfileEvents ret; | ||
| if (!ReadBlock(*input_, &ret.block)) { | ||
| return {}; | ||
| } | ||
|
|
||
| if (events_) { | ||
| events_->OnProfileEvents(ret.block); | ||
| if (!ReceiveProfileEvents(ret.block)) { | ||
| throw ProtocolError{"can't read profile events packet from input stream"}; | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| default: | ||
| throw UnimplementedError("unimplemented " + std::to_string((int)packet_type)); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| bool Client::Impl::ProcessPacket(uint64_t* server_packet) { | ||
| auto packet = ReceivePacket(server_packet); | ||
| switch (packet.index()) { | ||
| case VariantIndex<ServerError, decltype(packet)>(): | ||
| case VariantIndex<std::monostate, decltype(packet)>(): | ||
| // Normally `ReceivePacket()` throws on server exceptions, but it can be | ||
| // suppressed by `ClientOptions::SetRethrowException`. In that case the | ||
| // error marks the end of the response. | ||
| case VariantIndex<EndOfStream, decltype(packet)>(): | ||
| return false; | ||
| default: | ||
|
|
@@ -946,29 +896,97 @@ bool Client::Impl::ReceiveData(Block & block) { | |
| return true; | ||
| } | ||
|
|
||
| bool Client::Impl::ReceiveException(bool rethrow, ServerError * error) { | ||
| bool Client::Impl::ReceiveException(ServerError & error) { | ||
| std::shared_ptr<Exception> e(new Exception); | ||
|
|
||
| bool has_nested = false; // obsolete: https://github.com/ClickHouse/ClickHouse/blob/ef11941cf5a/src/IO/ReadHelpers.cpp#L2017 | ||
|
|
||
| bool exception_received = | ||
| WireFormat::ReadFixed(*input_, &e->code) | ||
| && WireFormat::ReadString(*input_, &e->name) | ||
| && WireFormat::ReadString(*input_, &e->display_text) | ||
| && WireFormat::ReadString(*input_, &e->stack_trace) | ||
| && WireFormat::ReadFixed(*input_, &has_nested); | ||
| if (!WireFormat::ReadFixed(*input_, &e->code) || | ||
| !WireFormat::ReadString(*input_, &e->name) || | ||
| !WireFormat::ReadString(*input_, &e->display_text) || | ||
| !WireFormat::ReadString(*input_, &e->stack_trace) || | ||
| !WireFormat::ReadFixed(*input_, &has_nested)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (events_) { | ||
| events_->OnServerException(*e); | ||
| } | ||
|
|
||
| if (rethrow || options_.rethrow_exceptions) { | ||
| throw ServerError(e); | ||
| error = ServerError(e); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool Client::Impl::ReceiveProfileInfo(Profile & profile) { | ||
| if (!WireFormat::ReadUInt64(*input_, &profile.rows) || | ||
| !WireFormat::ReadUInt64(*input_, &profile.blocks) || | ||
| !WireFormat::ReadUInt64(*input_, &profile.bytes) || | ||
| !WireFormat::ReadFixed(*input_, &profile.applied_limit) || | ||
| !WireFormat::ReadUInt64(*input_, &profile.rows_before_limit) || | ||
| !WireFormat::ReadFixed(*input_, &profile.calculated_rows_before_limit)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (exception_received && error != nullptr) { | ||
| *error = ServerError(e); | ||
| if (events_) { | ||
| events_->OnProfile(profile); | ||
| } | ||
| return exception_received; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool Client::Impl::ReceiveProgress(Progress & progress) { | ||
| if (!WireFormat::ReadUInt64(*input_, &progress.rows) || | ||
| !WireFormat::ReadUInt64(*input_, &progress.bytes)) { | ||
| return false; | ||
| } | ||
|
|
||
| if constexpr(DMBS_PROTOCOL_REVISION >= DBMS_MIN_REVISION_WITH_TOTAL_ROWS_IN_PROGRESS) { | ||
| if (!WireFormat::ReadUInt64(*input_, &progress.total_rows)) { | ||
| return false; | ||
| } | ||
| } | ||
| if (server_info_.revision >= DBMS_MIN_REVISION_WITH_CLIENT_WRITE_INFO) | ||
| { | ||
| if (!WireFormat::ReadUInt64(*input_, &progress.written_rows) || | ||
| !WireFormat::ReadUInt64(*input_, &progress.written_bytes)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (events_) { | ||
| events_->OnProgress(progress); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool Client::Impl::ReceiveLog(Block & block) { | ||
| if (!WireFormat::SkipString(*input_) || // log tag | ||
| !ReadBlock(*input_, &block)) { // Use uncompressed stream since log blocks usually contain only one row | ||
| return false; | ||
| } | ||
| if (events_) { | ||
| events_->OnServerLog(block); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool Client::Impl::ReceiveTableColumns() { | ||
| if (!WireFormat::SkipString(*input_) || // external table name | ||
| !WireFormat::SkipString(*input_)) { // columns metadata | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool Client::Impl::ReceiveProfileEvents(Block & block) { | ||
| if (!WireFormat::SkipString(*input_) || !ReadBlock(*input_, &block)) { | ||
| return false; | ||
| } | ||
| if (events_) { | ||
| events_->OnProfileEvents(block); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| void Client::Impl::SendCancel() { | ||
|
|
@@ -1213,8 +1231,11 @@ bool Client::Impl::ReceiveHello() { | |
|
|
||
| return true; | ||
| } else if (packet_type == ServerCodes::Exception) { | ||
| ReceiveException(true); | ||
| return false; | ||
| ServerError ret{std::make_shared<Exception>()}; | ||
| if (!ReceiveException(ret)) { | ||
| throw ProtocolError{"server rejected the connection, but its exception packet could not be decoded"}; | ||
| } | ||
| throw ret; | ||
| } | ||
|
Comment on lines
1233
to
1239
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello happens only during the handshake, and yes, in that case the client throws another exception. |
||
|
|
||
| return false; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be use different messages to mark point where it is failing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually everything looks ok - never mind.
I usually like to see "read failed: packet type" - so I can grep on "read failed" and see what steps passed.