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

Small clean-ups (including fix for UB) #616

Merged
merged 6 commits into from
Jun 28, 2022
Merged

Conversation

niklasf
Copy link
Contributor

@niklasf niklasf commented Mar 3, 2022

Most of these are trivial. I'll highlight some important bits with inline comments.

let cb = &mut *(raw_cb as *mut TransformCallback);
let key = slice::from_raw_parts(raw_key as *const u8, key_len as usize);
cb.in_domain_fn
.map_or(0xff, |in_domain| in_domain(key) as u8)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Returning 0xff here was somewhat questionable (as a bool is expected in C++ land), but not a problem as far as I can tell after reading the current implementation in RocksDB.

src/db.rs Outdated Show resolved Hide resolved
ffi_try!(ffi::rocksdb_open_column_families_with_ttl(
opts.inner,
cpath.as_ptr(),
cfs_v.len() as c_int,
cfnames.as_ptr(),
cfopts.as_ptr(),
cfhandles.as_mut_ptr(),
&(ttl.as_secs() as c_int) as *const _,
ttls_v.as_ptr(),
Copy link
Contributor Author

@niklasf niklasf Mar 3, 2022

Choose a reason for hiding this comment

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

I believe this was undefined behavior because the temporary c_int would be out of scope before the pointer is used in the called function.

Aditionally, there would be an out-of-bounds read if cfs_v.len() > 1, because the implementation expects a pointer to cfs_v.len() consecutive entries.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps I'm missing something, but where can out-of-bound read occur? cfnames? But it is an array, so everything should be OK. Am I missing something?

Copy link
Contributor Author

@niklasf niklasf Mar 6, 2022

Choose a reason for hiding this comment

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

I mean the ttls array. Previously we were passing a pointer to a single integer value (&(ttl.as_secs() as c_int) as *const _), but cfs_v.len() integer values are expected (https://github.com/facebook/rocksdb/blob/f20b674796ffd7ca32471705876fc651b8e246db/db/c.cc#L827-L828), one for each column family.

@niklasf niklasf mentioned this pull request Mar 3, 2022
@niklasf
Copy link
Contributor Author

niklasf commented Mar 5, 2022

Oh, looks like the pointers to temporaries are fine, actually:

The drop scope of the temporary is usually the end of the enclosing statement.

https://doc.rust-lang.org/stable/reference/expressions.html?highlight=Tempo#temporaries

That leaves only the out of bounds read as UB, with all other changes being cosmetic.

ffi_try!(ffi::rocksdb_open_column_families_with_ttl(
opts.inner,
cpath.as_ptr(),
cfs_v.len() as c_int,
cfnames.as_ptr(),
cfopts.as_ptr(),
cfhandles.as_mut_ptr(),
&(ttl.as_secs() as c_int) as *const _,
ttls_v.as_ptr(),
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps I'm missing something, but where can out-of-bound read occur? cfnames? But it is an array, so everything should be OK. Am I missing something?

@niklasf niklasf changed the title Make clippy happy (including fixes for UB) Make clippy happy (including fix for UB) Mar 10, 2022
@niklasf niklasf changed the title Make clippy happy (including fix for UB) Small clean-ups (including fix for UB) Apr 30, 2022
@niklasf
Copy link
Contributor Author

niklasf commented Apr 30, 2022

Rebased since #627 got merged first.

@niklasf
Copy link
Contributor Author

niklasf commented May 11, 2022

Prepared rustsec advisory rustsec/advisory-db#1237.

The changes in this PR are related, because they all adressed clippy lints, but I can split it, if that would be useful. The commits can also be viewed separately.

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

Successfully merging this pull request may close these issues.

None yet

3 participants