Skip to content

Commit a0e1d2e

Browse files
committed
Check for overflow in Script::bytes_to_asm_fmt()
This adds an overflow check in `Script::bytes_to_asm_fmt()` motivated by `electrs` issue. While it was not tested yet, I'm very confident that overflow is the cause of panic there and even if not it can cause panic becuase the public function takes unvalidated byte array and reads `data_len` from it. The `electrs` issue: romanz/electrs#490
1 parent 65d8bda commit a0e1d2e

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

src/blockdata/script.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,17 @@ impl Script {
529529
// Write any pushdata
530530
if data_len > 0 {
531531
f.write_str(" ")?;
532-
if index + data_len <= script.len() {
533-
for ch in &script[index..index + data_len] {
532+
match index.checked_add(data_len) {
533+
Some(end) if end <= script.len() => {
534+
for ch in &script[index..end] {
534535
write!(f, "{:02x}", ch)?;
535-
}
536-
index += data_len;
537-
} else {
538-
f.write_str("<push past end>")?;
539-
break;
536+
}
537+
index = end;
538+
},
539+
_ => {
540+
f.write_str("<push past end>")?;
541+
break;
542+
},
540543
}
541544
}
542545
}

0 commit comments

Comments
 (0)