Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::engine::DEFAULT_ENGINE;
use crate::engine::{Config, Engine};
use crate::PAD_BYTE;
#[cfg(any(feature = "alloc", feature = "std", test))]
use alloc::{string::String, vec};
use alloc::string::String;

///Encode arbitrary octets as base64 using the [default engine](DEFAULT_ENGINE).
///Returns a `String`.
Expand Down Expand Up @@ -56,11 +56,14 @@ pub fn encode<T: AsRef<[u8]>>(input: T) -> String {
pub fn encode_engine<E: Engine, T: AsRef<[u8]>>(input: T, engine: &E) -> String {
let encoded_size = encoded_len(input.as_ref().len(), engine.config().encode_padding())
.expect("integer overflow when calculating buffer size");
let mut buf = vec![0; encoded_size];
let mut buf = Vec::with_capacity(encoded_size);
unsafe {
buf.set_len(encoded_size);
Copy link

@jonasbb jonasbb Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The safety documentation of set_len is pretty clear that this is UB. Namely it states

The elements at old_len..new_len must be initialized.

I think it might be safe to call after encode_with_padding. If you need uninitialized memory you probably want to use MaybeUninit.

}

encode_with_padding(input.as_ref(), &mut buf[..], engine, encoded_size);

String::from_utf8(buf).expect("Invalid UTF8")
unsafe { String::from_utf8_unchecked(buf) }
}

///Encode arbitrary octets as base64.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
variant_size_differences,
warnings
)]
#![forbid(unsafe_code)]
// #![forbid(unsafe_code)]
#![cfg_attr(not(any(feature = "std", test)), no_std)]

#[cfg(all(feature = "alloc", not(any(feature = "std", test))))]
Expand Down