Skip to content

Commit

Permalink
Merge pull request #41 from stumpapp/al/fix-xml-panic
Browse files Browse the repository at this point in the history
fix: panic for empty or otherwise too small inner file
  • Loading branch information
danigm authored Feb 5, 2024
2 parents 4797111 + ed90b0d commit 6c9aac8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
Cargo.lock
*.swp
.DS_Store
7 changes: 7 additions & 0 deletions src/xmlutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum XMLError {
InvalidState,
#[error("No XML Elements Found")]
NoElements,
#[error("XML content is empty")]
NoContent,
}

pub struct XMLReader<'a> {
Expand All @@ -40,6 +42,11 @@ pub struct XMLReader<'a> {

impl<'a> XMLReader<'a> {
pub fn parse(content: &[u8]) -> Result<RefCell<XMLNode>, XMLError> {
// The operations below require at least 4 bytes to not panic
if content.is_empty() || content.len() < 4 {
return Err(XMLError::NoContent);
}

let content_str;
//If there is a UTF-8 BOM marker, ignore it
let content_slice = if content[0..3] == [0xefu8, 0xbbu8, 0xbfu8] {
Expand Down

0 comments on commit 6c9aac8

Please sign in to comment.