Skip to content

Commit

Permalink
asset: make HandleUntyped::id private (bevyengine#7076)
Browse files Browse the repository at this point in the history
# Objective

It is currently possible to break reference counting for assets by creating a strong `HandleUntyped` and then modifying the `id` field before dropping the handle. This should not be allowed.

## Solution

Change the `id` field visibility to private and add a getter instead. The same change was previously done for `Handle<T>` in bevyengine#6176, but `HandleUntyped` was forgotten.

---

## Migration Guide

- Instead of directly accessing the ID of a `HandleUntyped` as `handle.id`, use the new getter `handle.id()`.
  • Loading branch information
neocturne authored and ItsDoot committed Feb 1, 2023
1 parent cf04434 commit 380d815
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ impl<T: Asset> Clone for Handle<T> {
/// To convert back to a typed handle, use the [typed](HandleUntyped::typed) method.
#[derive(Debug)]
pub struct HandleUntyped {
/// An unique identifier to an Asset.
pub id: HandleId,
id: HandleId,
handle_type: HandleType,
}

Expand Down Expand Up @@ -352,6 +351,12 @@ impl HandleUntyped {
}
}

/// The ID of the asset.
#[inline]
pub fn id(&self) -> HandleId {
self.id
}

/// Creates a weak copy of this handle.
#[must_use]
pub fn clone_weak(&self) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ fn check_textures(
rpg_sprite_handles: ResMut<RpgSpriteHandles>,
asset_server: Res<AssetServer>,
) {
if let LoadState::Loaded =
asset_server.get_group_load_state(rpg_sprite_handles.handles.iter().map(|handle| handle.id))
if let LoadState::Loaded = asset_server
.get_group_load_state(rpg_sprite_handles.handles.iter().map(|handle| handle.id()))
{
state.set(AppState::Finished).unwrap();
}
Expand Down

0 comments on commit 380d815

Please sign in to comment.