diff options
author | joe <joe@jerkface.net> | 2017-07-28 05:29:37 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-28 05:29:37 -0400 |
commit | 025364434a3a57260e8149df4a532c14332aacfe (patch) | |
tree | 7fb85ed52b06f154a8174b0a3608ff04928d3d25 | |
parent | 8dd4d8caac71bbfdc598ba6d8ff35538cd8a8503 (diff) |
Reject invalid sized NodeIds.
-rw-r--r-- | Mainline.hs | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/Mainline.hs b/Mainline.hs index 0f6d7f16..5180352a 100644 --- a/Mainline.hs +++ b/Mainline.hs | |||
@@ -79,7 +79,16 @@ import qualified Data.Aeson as JSON | |||
79 | import Text.Read | 79 | import Text.Read |
80 | 80 | ||
81 | newtype NodeId = NodeId ByteString | 81 | newtype NodeId = NodeId ByteString |
82 | deriving (Eq,Ord,ByteArrayAccess, BEncode, Bits, Hashable) | 82 | deriving (Eq,Ord,ByteArrayAccess, Bits, Hashable) |
83 | |||
84 | instance BEncode NodeId where | ||
85 | fromBEncode bval = do | ||
86 | bs <- fromBEncode bval | ||
87 | if B.length bs /= 20 | ||
88 | then Left "Invalid length node id." | ||
89 | else Right $ NodeId bs | ||
90 | |||
91 | toBEncode (NodeId bs) = toBEncode bs | ||
83 | 92 | ||
84 | instance Show NodeId where | 93 | instance Show NodeId where |
85 | show (NodeId bs) = Char8.unpack $ Base16.encode bs | 94 | show (NodeId bs) = Char8.unpack $ Base16.encode bs |