Skip to content

Commit 1eafdb8

Browse files
committed
Fix Double Copy implementation vulnerability
1 parent 360f7d7 commit 1eafdb8

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
267267
kind,
268268
self.is_upvar_field_projection(original_path.as_ref())
269269
);
270+
if self.maybe_copy(original_path.ty(self.body, self.infcx.tcx).ty) {
271+
// If the type may implement Copy, skip the error.
272+
// It's an error with the Copy implementation (e.g. duplicate Copy) rather than borrow check
273+
return;
274+
}
270275
(
271276
match kind {
272277
&IllegalMoveOriginKind::BorrowedContent { target_place } => self
@@ -291,6 +296,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
291296
self.buffer_error(err);
292297
}
293298

299+
fn maybe_copy(&mut self, ty: Ty<'tcx>) -> bool {
300+
use rustc_trait_selection::infer::InferCtxtExt;
301+
let Some(copy_trait_def) = self.infcx.tcx.lang_items().copy_trait() else { return false };
302+
self.infcx.type_implements_trait(copy_trait_def, [ty], self.param_env).may_apply()
303+
}
304+
294305
fn report_cannot_move_from_static(&mut self, place: Place<'tcx>, span: Span) -> Diag<'infcx> {
295306
let description = if place.projection.len() == 1 {
296307
format!("static item {}", self.describe_any_place(place.as_ref()))

0 commit comments

Comments
 (0)