Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit Copies/Conversions #34

Open
jasoncolburne opened this issue Jan 26, 2023 · 1 comment
Open

Audit Copies/Conversions #34

jasoncolburne opened this issue Jan 26, 2023 · 1 comment
Assignees
Milestone

Comments

@jasoncolburne
Copy link
Collaborator

jasoncolburne commented Jan 26, 2023

We want to minimize these throughout the library, for security reasons if nothing else.

An example of a bad pattern accepting a String or a Vec<u8> as a parameter to a function. Instead, one can always coerce into &str or &[u8] with a simple & from a String or Vec<u8>, so I've found the minimizing application code pattern to be accepting parameters as &str and &[u8] (and &Matter etc). Values returned from functions are typically then the non-borrowed, juiced up version of the types. Here is a concrete example:

Good:

fn trim(input: &[u8], length: usize) -> Vec<u8> {
    input[..length].to_vec()
}

fn string_to_vec(input: &str) -> Vec<u8> {
    input.as_bytes().to_vec()
}

// example usage
fn exercise() {
    let v = trim(&string_to_vec("stuff"), 3);
}

Bad:

fn foo(input: Vec<u8>) -> Vec<u8> {
    input[..len]
}

fn foo(input: String) -> Vec<u8> {
    input
}

// example usage, notice how we need another .to_string() in the
// calling code, and we lose the & on the vec param
fn exercise() {
    let v = trim(string_to_vec("stuff".to_string()), 3);
}

In addition to adding complexity to the application (which we must aim to minimize), the longer one can leave variables as borrowed primitive entities, the more one opens up the door to making efficiency gains from using data in place. It's also less zeroing memory to worry about, as there will be less copies of sensitive information in memory in general.

@m00sey m00sey added this to the v1.0.0 milestone Jan 27, 2023
@kentbull
Copy link
Contributor

Meeting comment 03-30-23

Possibly have a jam session at IIW to review this, and maybe a 1.0.0 release.

@m00sey m00sey self-assigned this Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants