-
Notifications
You must be signed in to change notification settings - Fork 1k
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
apply strict enum checking #1254
Changes from 10 commits
5626a3a
39e9042
2f79770
81a994b
6ec2df1
bd1dee2
510a06c
fb79e87
b435a5d
bab1c8f
3421ec7
19cfa32
ae0c79e
a162d00
24e7a51
5105174
450c04a
54c30e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,8 @@ void ISerializable.Deserialize(BinaryReader reader) | |
{ | ||
Flags = (MessageFlags)reader.ReadByte(); | ||
Command = (MessageCommand)reader.ReadByte(); | ||
if (!Enum.IsDefined(typeof(MessageCommand), Command)) | ||
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. We don't need to check it here. If we add new command in the future, it can be compatible with the current version. 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. The idea of strict enum checking is to fail early. Without this check here I can create a message with a fake command that
Instead with this check it will never waste any resources on decompressing and prevents it reaching 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. That is because we don't know the message format, but maybe the |
||
throw new FormatException(); | ||
_payload_compressed = reader.ReadVarBytes(PayloadMaxSize); | ||
DecompressPayload(); | ||
} | ||
|
@@ -152,6 +154,8 @@ internal static int TryDeserialize(ByteString data, out Message msg) | |
Command = (MessageCommand)header[1], | ||
_payload_compressed = length <= 0 ? new byte[0] : data.Slice(payloadIndex, (int)length).ToArray() | ||
}; | ||
if (!Enum.IsDefined(typeof(MessageCommand), msg.Command)) | ||
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. We don't need to check it here. If we add new command in the future, it can be compatible with the current version. |
||
throw new FormatException(); | ||
msg.DecompressPayload(); | ||
|
||
return payloadIndex + (int)length; | ||
|
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.
We don't need to check it here. If we add new reason in the future, it can be compatible with the current version.
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.
it's been a very long time that i looked at the consensus code, but what prevents me from sending a
ConsensusPayload
with an invalid message that gets processed unnecessarily (see #1254 (comment) what I mean with unnecessary processing)?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.
The consensus message doesn't need to be dropped even if I don't know the changeview reason. So this check is unnecessary because we may add new reasons in the future.