Skip to content

Commit

Permalink
Add SAFETY markers to code that could panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Nov 15, 2023
1 parent eb417b0 commit efbb166
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ pub fn decode_into(bytes: &[u8], encoding: &'static Encoding, buf: &mut String)
buf.reserve(
decoder
.max_utf8_buffer_length_without_replacement(bytes.len())
// SAFETY: None can be returned only if required size will overflow usize,
// but in that case String::reserve also panics
.unwrap(),
);
let (result, read) = decoder.decode_to_string_without_replacement(bytes, buf, true);
Expand All @@ -132,6 +134,7 @@ pub fn decode_into(bytes: &[u8], encoding: &'static Encoding, buf: &mut String)
Ok(())
}
DecoderResult::Malformed(_, _) => Err(Error::NonDecodable(None)),
// SAFETY: We allocate enough space above
DecoderResult::OutputFull => unreachable!(),
}
}
Expand Down

0 comments on commit efbb166

Please sign in to comment.