add some dos protection (#8084)

This commit is contained in:
Nikolay Volf 2018-03-12 15:55:28 +03:00 committed by GitHub
parent e0a21e5aae
commit 58a1671076
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -173,12 +173,14 @@ impl<'a> Runtime<'a> {
/// Intuition about the return value sense is to aswer the question 'are we allowed to continue?' /// Intuition about the return value sense is to aswer the question 'are we allowed to continue?'
fn charge_gas(&mut self, amount: u64) -> bool { fn charge_gas(&mut self, amount: u64) -> bool {
let prev = self.gas_counter; let prev = self.gas_counter;
if prev + amount > self.gas_limit { match prev.checked_add(amount) {
// exceeds gas // gas charge overflow protection
false None => false,
} else { Some(val) if val > self.gas_limit => false,
self.gas_counter = prev + amount; Some(_) => {
true self.gas_counter = prev + amount;
true
}
} }
} }
@ -548,13 +550,13 @@ impl<'a> Runtime<'a> {
fn debug(&mut self, args: RuntimeArgs) -> Result<()> fn debug(&mut self, args: RuntimeArgs) -> Result<()>
{ {
let msg_ptr: u32 = args.nth_checked(0)?; trace!(target: "wasm", "Contract debug message: {}", {
let msg_len: u32 = args.nth_checked(1)?; let msg_ptr: u32 = args.nth_checked(0)?;
let msg_len: u32 = args.nth_checked(1)?;
let msg = String::from_utf8(self.memory.get(msg_ptr, msg_len as usize)?) String::from_utf8(self.memory.get(msg_ptr, msg_len as usize)?)
.map_err(|_| Error::BadUtf8)?; .map_err(|_| Error::BadUtf8)?
});
trace!(target: "wasm", "Contract debug message: {}", msg);
Ok(()) Ok(())
} }