Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes from_bytes throwing split_to out of bounds error #486

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pumpkin-nbt/src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ impl<'de, T: Buf> de::Deserializer<'de> for &mut Deserializer<'de, T> {

if self.is_named {
// Consume struct name
NbtTag::deserialize(self.input)?;
let len = self.input.get_u16() as usize;
self.input.advance(len);
}
}

Expand Down
33 changes: 33 additions & 0 deletions pumpkin-nbt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ impl_array!(BytesArray, "byte");
mod test {
use serde::{Deserialize, Serialize};

use crate::deserializer::from_bytes;
use crate::serializer::to_bytes;
use crate::BytesArray;
use crate::IntArray;
use crate::LongArray;
Expand Down Expand Up @@ -236,6 +238,23 @@ mod test {
assert_eq!(test, recreated_struct);
}

#[test]
fn test_simple_ser_de_named() {
let name = String::from("Test");
let test = Test {
byte: 123,
short: 1342,
int: 4313,
long: 34,
float: 1.00,
string: "Hello test".to_string(),
};
let mut bytes = to_bytes(&test, name).unwrap();
let recreated_struct: Test = from_bytes(&mut bytes).unwrap();

assert_eq!(test, recreated_struct);
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct TestArray {
#[serde(with = "BytesArray")]
Expand All @@ -258,4 +277,18 @@ mod test {

assert_eq!(test, recreated_struct);
}

#[test]
fn test_simple_ser_de_array_named() {
let name = String::from("Test");
let test = TestArray {
byte_array: vec![0, 3, 2],
int_array: vec![13, 1321, 2],
long_array: vec![1, 0, 200301, 1],
};
let mut bytes = to_bytes(&test, name).unwrap();
let recreated_struct: TestArray = from_bytes(&mut bytes).unwrap();

assert_eq!(test, recreated_struct);
}
}
Loading