Fix Witness debug display bug

Currently if the witness has zero elements or any of the individual
witnesses is empty we panic. Panic is caused by subtracting 1 from a
zero length.

Check the length is non-zero before subtracting 1, print `[]` if empty.
This commit is contained in:
Tobin C. Harding 2023-08-17 09:34:46 +10:00
parent 29af523beb
commit e96be5ee6e
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 19 additions and 13 deletions

View File

@ -68,21 +68,27 @@ fn fmt_debug(w: &Witness, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error>
f.write_str("witnesses: [")?;
let instructions = w.iter();
if instructions.len() > 0 {
let last_instruction = instructions.len() - 1;
for (i, instruction) in instructions.enumerate() {
let bytes = instruction.iter();
if bytes.len() > 0 {
let last_byte = bytes.len() - 1;
f.write_str("[")?;
for (j, byte) in bytes.enumerate() {
write!(f, "{:#04x}", byte)?;
f.write_str(comma_or_close(j, last_byte))?;
}
} else {
// This is possible because the varint is not part of the instruction (see Iter).
write!(f, "[]")?;
}
f.write_str(comma_or_close(i, last_instruction))?;
}
} else {
// Witnesses can be empty because the 0x00 var int is not stored in content.
write!(f, "]")?;
}
f.write_str(" }")
}