Ensure Query does not use the wrong World (#7150)
				
					
				
			`Query` relies on the `World` it stores being the same as the world used for creating the `QueryState` it stores. If they are not the same then everything is very unsound. This was not actually being checked anywhere, `Query::new` did not have a safety invariant or even an assertion that the `WorldId`'s are the same. This shouldn't have any user facing impact unless we have really messed up in bevy and have unsoundness elsewhere (in which case we would now get a panic instead of being unsound).
This commit is contained in:
		
							parent
							
								
									aaaf357dbb
								
							
						
					
					
						commit
						d03c1a0687
					
				| @ -1202,4 +1202,15 @@ mod tests { | |||||||
|             ); |             ); | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     #[test] | ||||||
|  |     #[should_panic = "Attempted to use bevy_ecs::query::state::QueryState<()> with a mismatched World."] | ||||||
|  |     fn query_validates_world_id() { | ||||||
|  |         let mut world1 = World::new(); | ||||||
|  |         let world2 = World::new(); | ||||||
|  |         let qstate = world1.query::<()>(); | ||||||
|  |         // SAFETY: doesnt access anything
 | ||||||
|  |         let query = unsafe { Query::new(&world2, &qstate, 0, 0) }; | ||||||
|  |         query.iter(); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -295,6 +295,10 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> std::fmt::Debug for Query<'w, | |||||||
| impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { | impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { | ||||||
|     /// Creates a new query.
 |     /// Creates a new query.
 | ||||||
|     ///
 |     ///
 | ||||||
|  |     /// # Panics
 | ||||||
|  |     ///
 | ||||||
|  |     /// This will panic if the world used to create `state` is not `world`.
 | ||||||
|  |     ///
 | ||||||
|     /// # Safety
 |     /// # Safety
 | ||||||
|     ///
 |     ///
 | ||||||
|     /// This will create a query that could violate memory safety rules. Make sure that this is only
 |     /// This will create a query that could violate memory safety rules. Make sure that this is only
 | ||||||
| @ -306,6 +310,8 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { | |||||||
|         last_change_tick: u32, |         last_change_tick: u32, | ||||||
|         change_tick: u32, |         change_tick: u32, | ||||||
|     ) -> Self { |     ) -> Self { | ||||||
|  |         state.validate_world(world); | ||||||
|  | 
 | ||||||
|         Self { |         Self { | ||||||
|             force_read_only_component_access: false, |             force_read_only_component_access: false, | ||||||
|             world, |             world, | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Boxy
						Boxy