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

Forbid casting to/from a pointer of unknown kind #45735

Merged
merged 1 commit into from
Nov 8, 2017

Conversation

tirr-c
Copy link
Contributor

@tirr-c tirr-c commented Nov 3, 2017

Fixes #45730.

Before, it ICE'd when pointer_kind encountered TyInfer.

@rust-highfive
Copy link
Collaborator

r? @pnkfelix

(rust_highfive has picked a reviewer for you, use r? to override)

@@ -105,6 +105,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// We should really try to normalize here.
ty::TyProjection(ref pi) => PointerKind::OfProjection(pi),
ty::TyParam(ref p) => PointerKind::OfParam(p),
// Insufficient type information; this will be caught later, so just report that this
// is a thin pointer.
ty::TyInfer(_) => PointerKind::Thin,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this case occur only by lack of type information? I'm not pretty sure.

@shepmaster shepmaster added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 3, 2017
@arielb1
Copy link
Contributor

arielb1 commented Nov 5, 2017

@tirr-c

It's not a good idea to assume things when encountering a TyInfer - trait bounds etc. can make the TyInfer equal to every type, so this could accidentally allow thin-to-fat pointer casts if the _ is later made to be equal fmt::Debug, as in:

use std::fmt;
fn main() {
  let x: *const _ = 0 as *const _; // we don't want to allow this if...
  let y: Option<*const fmt::Debug> = Some(x) as _;
  // ^ later, we have x = *const fmt::Debug
}

fn not_ok() {
  let x = 0 as *const i32 as *const _ as *mut _; //~ ERROR ?
}

Instead, you should make fn pointer_kind return an Option, and emit an error of we are casting to an unknown pointer kind..

@tirr-c tirr-c changed the title Assume thin pointer when pointer type cannot be inferred Forbid casting to/from a pointer of unknown kind Nov 6, 2017
@tirr-c
Copy link
Contributor Author

tirr-c commented Nov 6, 2017

Added a new error code for casting with unknown pointer kind.

r? @arielb1

@rust-highfive rust-highfive assigned arielb1 and unassigned pnkfelix Nov 6, 2017
@@ -457,14 +478,20 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
debug!("check_ptr_ptr_cast m_expr={:?} m_cast={:?}", m_expr, m_cast);
// ptr-ptr cast. vtables must match.

let (expr_kind, cast_kind) =
Copy link
Contributor

@arielb1 arielb1 Nov 6, 2017

Choose a reason for hiding this comment

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

This forbids casting an *const _ to an *const u32, please move the check after the following if to avoid breaking user code.

@tirr-c
Copy link
Contributor Author

tirr-c commented Nov 6, 2017

Updated, but I couldn't trigger the *const _ as *const u32 case. What would the code look like?

@arielb1
Copy link
Contributor

arielb1 commented Nov 6, 2017

@tirr-c

Can't you have 0 as *const u32 as *const _ as *mut u32? Or does that go through a separate code-path?

@arielb1
Copy link
Contributor

arielb1 commented Nov 6, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Nov 6, 2017

📌 Commit 99ada04 has been approved by arielb1

@tirr-c
Copy link
Contributor Author

tirr-c commented Nov 6, 2017

It just compiles. I'm not sure when inference occurs, but maybe *const _ there is inferred as *const u32?

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 7, 2017
@bors
Copy link
Contributor

bors commented Nov 8, 2017

⌛ Testing commit 99ada04 with merge 49bee9d...

bors added a commit that referenced this pull request Nov 8, 2017
Forbid casting to/from a pointer of unknown kind

Fixes #45730.

Before, it ICE'd when `pointer_kind` encountered `TyInfer`.
@bors
Copy link
Contributor

bors commented Nov 8, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: arielb1
Pushing 49bee9d to master...

@bors bors merged commit 99ada04 into rust-lang:master Nov 8, 2017
@tirr-c tirr-c deleted the issue-45730 branch November 19, 2017 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants