Skip to content

Commit 7e75dd8

Browse files
fix(overflow): Fix integer overflows in BinaryEncoding and Pactus (#4592)
* fix(binary-coding): Avoid unsigned integer overflow * fix(pactus): Avoid unsigned integer overflow
1 parent 9977191 commit 7e75dd8

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

rust/chains/tw_pactus/src/types/amount.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ pub struct Amount(pub i64);
55

66
impl Encodable for Amount {
77
fn encode(&self, w: &mut dyn std::io::Write) -> Result<(), Error> {
8-
VarInt::from(self.0 as usize).encode(w)
8+
let amount: usize = match self.0.try_into() {
9+
Ok(amount) => amount,
10+
Err(_) => {
11+
return Err(Error::IoError(std::io::Error::from(
12+
std::io::ErrorKind::InvalidInput,
13+
)));
14+
},
15+
};
16+
VarInt::from(amount).encode(w)
917
}
1018

1119
fn encoded_size(&self) -> usize {

src/BinaryCoding.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ tuple<bool, string> decodeString(const Data& in, size_t& indexInOut) {
147147
if (!get<0>(lenTup)) { return make_tuple(false, ""); }
148148
const auto len = get<1>(lenTup);
149149
// read bytes into string
150-
if (in.size() < indexInOut + len) { return make_tuple(false, ""); }
150+
if (in.size() < indexInOut || in.size() - indexInOut < len) {
151+
return make_tuple(false, "");
152+
}
151153
string result(in.data() + indexInOut, in.data() + indexInOut + len);
152154
indexInOut += len;
153155
return make_tuple(true, result);

0 commit comments

Comments
 (0)