rphmeier / insert_many Goto Github PK
View Code? Open in Web Editor NEWinsert_many optimization for contiguous-memory backed containers in rust
License: MIT License
insert_many optimization for contiguous-memory backed containers in rust
License: MIT License
Hi there, we (Rust group @sslab-gatech) are scanning crates on crates.io for potential soundness bugs. We noticed a potential issue in the provided insert_many
implementation / impl_veclike!
macro. Namely, impl_veclike
pushes elements over for insertion using ptr::copy
here:
Lines 37 to 47 in 697ec9f
This can duplicate items in the Vec, after which it calls iter.next()
, which can potentially panic. This means that during this panic the copied elements can be dropped twice. Here's a quick example of this issue:
#![forbid(unsafe_code)]
use insert_many::InsertMany;
struct DropDetector(u32);
impl Drop for DropDetector {
fn drop(&mut self) {
println!("Dropping {}", self.0);
}
}
// A type with an iterator that panics.
// -----
struct MyCollection();
impl IntoIterator for MyCollection {
type Item = DropDetector;
type IntoIter = PanickingIterator;
fn into_iter(self) -> Self::IntoIter { PanickingIterator() }
}
struct PanickingIterator();
impl Iterator for PanickingIterator {
type Item = DropDetector;
fn next(&mut self) -> Option<Self::Item> { panic!("Iterator panicked"); }
}
impl ExactSizeIterator for PanickingIterator {
fn len(&self) -> usize { 1 }
}
// -----
fn main() {
let mut v = vec![DropDetector(1), DropDetector(2)];
// Inserting many elements from a panicking iterator will cause a double-drop.
v.insert_many(0, MyCollection());
}
This outputs:
thread 'main' panicked at 'Iterator panicked', src/main.rs:43:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Dropping 1
Dropping 1
Return code 101
What's happening here is:
[DropDetector(1), DropDetector(2)]
, length = 2[DropDetector(1), DropDetector(2), RESERVED]
, length = 2ptr::copy(start_ptr, start_ptr.offset(num_items as isize), len - index as usize);
[DropDetector(1), DropDetector(1), DropDetector(2)]
, length = 2iter.next()
panicsDropDetector(1)
gets dropped twice.We'd recommend wrapping the vector in ManuallyDrop
while performing these operations to avoid this issue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.