code-423n4 / 2021-08-gravitybridge-findings

1 stars 0 forks source link

`Vec::new()` instead of `Iterator::collect()` #17

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

nascent

Vulnerability details

[I-01] Vec::new() instead of Iterator::collect()

Severity: Low

Ex: fn encode_logic_call_confirm() in message_signatures.rs

 fn foo(vals: &[Bar]) -> Vec<Bar> {
    let mut new_array = Vec::new();
    for val in vals.iter() {
        new_array.push(val.clone())
    }
    new_array
}

Recommendation

To avoid mutability and instantiating/resizing a Vec, .collect() an Iterator into an immutable Vec directly:

 fn foo(vals: &[Bar]) -> Vec<Bar> {
     vals.iter().cloned().collect()
 }
jkilpatr commented 2 years ago

This is a good suggestion and moderately more efficient.

loudoguno commented 2 years ago

reopening as per judges assessment as "primary issue" on findings sheet