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

Length mismatch in array init causes buffer overflow #693

Closed
PatrickNorton opened this issue May 4, 2021 · 5 comments · Fixed by #694
Closed

Length mismatch in array init causes buffer overflow #693

PatrickNorton opened this issue May 4, 2021 · 5 comments · Fixed by #694

Comments

@PatrickNorton
Copy link
Contributor

When initializing an array, the value put in the length field is equal to the total number of allocated bytes, not the number of elements the array can hold.

Example:

use std::mem::MaybeUninit;
use crossbeam::epoch::*;

pub fn main() {
	let owned = Owned::<[MaybeUninit<usize>]>::init(10);
	let arr: &[MaybeUninit<usize>] = &*owned;
	println!("{}", arr.len());
}

This should print 10, but prints 88. In particular, it has only allocated space for 10 elements of data, so creation of arr in the example is UB.

@PatrickNorton
Copy link
Contributor Author

I think the issue is in <[MaybeUninit<T>] as Pointable>::init(): (*ptr).size = size sets the length to the allocated size (in bytes) instead of the actual size (in elements).

@taiki-e
Copy link
Member

taiki-e commented May 4, 2021

Thanks! I filed #694 to fix this.

@PatrickNorton
Copy link
Contributor Author

Beat me to it by a few seconds! I also filed #695.

@taiki-e
Copy link
Member

taiki-e commented May 4, 2021

Sorry, I didn't notice you were writing a patch to fix this...

@PatrickNorton
Copy link
Contributor Author

No worries, I like your patch better than mine anyways :).

@bors bors bot closed this as completed in 399a92f May 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants