Skip to content

Commit

Permalink
Fix undefined behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed May 29, 2024
1 parent 254304f commit 423260d
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions azul-core/src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,20 +1139,23 @@ pub fn normalize_casing(input: &str) -> String {
/// Given a root node, traverses along the hierarchy, and returns a
/// mutable reference to the last child node of the root node
pub fn get_item<'a>(hierarchy: &[usize], root_node: &'a mut XmlNode) -> Option<&'a mut XmlNode> {
let mut current_node = &*root_node;
let mut iter = hierarchy.iter();
let mut hierarchy = hierarchy.to_vec();
hierarchy.reverse();
let item = hierarchy.pop()?;
let node = root_node.children.as_mut_slice_extended().get_mut(item)?;
get_item_internal(&mut hierarchy, node)
}

while let Some(item) = iter.next() {
current_node = current_node.children.get(*item).as_ref()?;
fn get_item_internal<'a>(hierarchy: &mut Vec<usize>, root_node: &'a mut XmlNode) -> Option<&'a mut XmlNode> {
if hierarchy.is_empty() {
return Some(root_node);
}

// Safe because we only ever have one mutable reference, but
// the borrow checker doesn't allow recursive mutable borrowing
let node_ptr = current_node as *const XmlNode;
let mut_node_ptr = node_ptr as *mut XmlNode;
Some(unsafe { &mut *mut_node_ptr }) // safe because we hold a &'a mut XmlNode
let cur_item = hierarchy.pop()?;
let node = root_node.children.as_mut_slice_extended().get_mut(cur_item)?;
get_item_internal(hierarchy, node)
}


/// Parses an XML string and returns a `StyledDom` with the components instantiated in the `<app></app>`
pub fn str_to_dom<'a>(
root_nodes: &'a [XmlNode],
Expand Down

0 comments on commit 423260d

Please sign in to comment.