Skip to content

Commit

Permalink
"Fix" UB [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Apr 12, 2024
1 parent d63c825 commit 61c3150
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/core/replication_fns/command_fns.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::io::Cursor;
use std::{io::Cursor, mem};

use bevy::{
ecs::{component::ComponentId, system::EntityCommands},
prelude::*,
ptr::Ptr,
};

use super::serde_fns::SerdeFns;
use super::serde_fns::{DeserializeInPlaceFn, SerdeFns};
use crate::{
client::client_mapper::{ClientMapper, ServerEntityMap},
core::replicon_tick::RepliconTick,
Expand Down Expand Up @@ -110,7 +110,8 @@ unsafe fn write<C: Component>(
};

if let Some(mut component) = entity.get_mut::<C>() {
rule_fns.deserialize_in_place(&mut component, cursor, &mut mapper)?;
let deserialize: DeserializeInPlaceFn<C> = mem::transmute(rule_fns.deserialize_in_place);
(deserialize)(&mut component, cursor, &mut mapper)?;
} else {
let component: C = rule_fns.deserialize(cursor, &mut mapper)?;
commands.entity(entity.id()).insert(component);
Expand Down
12 changes: 1 addition & 11 deletions src/core/replication_fns/serde_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct SerdeFns {
commands_id: CommandFnsId,
serialize: unsafe fn(),
deserialize: unsafe fn(),
deserialize_in_place: unsafe fn(),
pub deserialize_in_place: unsafe fn(),
}

impl SerdeFns {
Expand Down Expand Up @@ -47,16 +47,6 @@ impl SerdeFns {
(deserialize)(cursor, mapper)
}

pub unsafe fn deserialize_in_place<C>(
&self,
component: &mut C,
cursor: &mut Cursor<&[u8]>,
mapper: &mut ClientMapper,
) -> bincode::Result<()> {
let deserialize: DeserializeInPlaceFn<C> = mem::transmute(self.deserialize_in_place);
(deserialize)(component, cursor, mapper)
}

pub(crate) fn commands_id(&self) -> CommandFnsId {
self.commands_id
}
Expand Down

0 comments on commit 61c3150

Please sign in to comment.