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

Made fail_bounds_check more careful with strings. #12357

Merged
merged 1 commit into from
Feb 18, 2014
Merged

Made fail_bounds_check more careful with strings. #12357

merged 1 commit into from
Feb 18, 2014

Conversation

chromatic
Copy link
Contributor

Fixes #11976.

msg.with_c_str(|buf| fail_(buf as *u8, file, line))

#[inline]
fn c_str_to_static(cs: CString) -> &'static str {
Copy link
Member

Choose a reason for hiding this comment

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

Why does this need a separate function? Can't it be

let file_str = match unsafe {CString::new(file as *c_char, false)}.as_str() {
    Some(s) => ...,
    None => ...
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's much better! (Thanks for teaching me a new idiom.)

@huonw
Copy link
Member

huonw commented Feb 18, 2014

General question (not necessarily to @chromatic): is there a reason we can't emit the file names as &'static strs in trans and pass them to this function to keep everything safe?

#[inline]
fn c_str_to_static(cs: CString) -> &'static str {
match cs.as_str() {
Some(s) => unsafe { cast::transmute::<&str, &'static str>(s) },
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment for why this transmute is safe? Something along the lines of how the file is stored in rodata which is indeed static.

@alexcrichton
Copy link
Member

is there a reason we can't emit the file names as &'static strs in trans

We could certainly do that, I thought we had an issue open on that, but I can't seem to find it...

bors added a commit that referenced this pull request Feb 18, 2014
@bors bors closed this Feb 18, 2014
@bors bors merged commit 96102b3 into rust-lang:master Feb 18, 2014
@huonw
Copy link
Member

huonw commented Feb 19, 2014

(I opened #12378 about avoiding *u8.)

@chromatic chromatic deleted the gh_11976_fail_bounds_check_str branch February 27, 2014 21:52
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 25, 2022
fix: When reference searching macro inputs, don't search everything that was downmapped

Fixes rust-lang/rust-analyzer#11668
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.

fail_bounds_check incorrectly casts temporary to static
4 participants