Skip to content

Commit

Permalink
Don't crash on incomplete POPM tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Marekkon5 committed May 13, 2021
1 parent 1c2f23e commit ac0676a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ For more info check out our [website](https://onetagger.github.io/).

You can download latest binaries from [releases](https://github.com/Marekkon5/onetagger/releases)

### Linux

You might need to install additional dependencies to make One Tagger work:
```
sudo apt install libsndfile1-dev
```

## Compilling

### Linux & Mac
Expand Down
17 changes: 11 additions & 6 deletions src/tag/id3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl ID3Tag {
pub fn get_popularimeter(&self) -> Option<ID3Popularimeter> {
let tag = self.tag.get("POPM")?;
let data = tag.content().unknown()?;
let popm = ID3Popularimeter::from_bytes(data).ok()?;
let popm = ID3Popularimeter::from_bytes(data)?;
Some(popm)
}

Expand Down Expand Up @@ -232,7 +232,7 @@ impl TagImpl for ID3Tag {
fn get_rating(&self) -> Option<u8> {
let tag = self.tag.get("POPM")?;
let value = tag.content().unknown()?;
let popm = ID3Popularimeter::from_bytes(value).ok()?;
let popm = ID3Popularimeter::from_bytes(value)?;
//Byte to 1 - 5
let rating = (popm.rating as f32 / 51.0).ceil() as u8;
if rating == 0 {
Expand Down Expand Up @@ -425,10 +425,15 @@ impl ID3Popularimeter {
}

// EMAIL \0 RATING (u8) COUNTER (u32)
pub fn from_bytes(data: &[u8]) -> Result<ID3Popularimeter, Box<dyn Error>> {
let pos = data.iter().position(|b| b == &0u8).ok_or("Can't find null byte!")?;
Ok(ID3Popularimeter {
email: String::from_utf8(data[0..pos].to_vec())?,
pub fn from_bytes(data: &[u8]) -> Option<ID3Popularimeter> {
let pos = data.iter().position(|b| b == &0u8)?;
if pos + 6 > data.len() {
warn!("POMP Tag has invalid length! Len: {}, null: {}", data.len(), pos);
return None;
}

Some(ID3Popularimeter {
email: String::from_utf8(data[0..pos].to_vec()).ok()?,
rating: data[pos+1],
counter: u32::from_be_bytes(data[pos+2..pos+6].try_into().unwrap_or([0,0,0,0]))
})
Expand Down

0 comments on commit ac0676a

Please sign in to comment.