1
0
Fork 0
mirror of https://github.com/azalea-rs/simdnbt.git synced 2025-08-02 07:26:04 +00:00

fix ub when writing

This commit is contained in:
mat 2025-08-01 15:46:01 -09:00
parent 2fcede1cfe
commit 1505d233fd
9 changed files with 44 additions and 10 deletions

View file

@ -12,8 +12,15 @@ libfuzzer-sys = "0.4"
simdnbt = { path = "../simdnbt" }
[[bin]]
name = "fuzz_target_1"
path = "fuzz_targets/fuzz_target_1.rs"
name = "borrow_read"
path = "fuzz_targets/borrow_read.rs"
test = false
doc = false
bench = false
[[bin]]
name = "owned_write"
path = "fuzz_targets/owned_write.rs"
test = false
doc = false
bench = false

View file

@ -0,0 +1,11 @@
#![no_main]
use std::io::Cursor;
use libfuzzer_sys::fuzz_target;
fuzz_target!(|data: &[u8]| {
if let Ok(simdnbt::borrow::Nbt::Some(r)) = simdnbt::borrow::read(&mut Cursor::new(data)) {
r.as_compound().to_owned();
}
});

View file

@ -6,6 +6,6 @@ fuzz_target!(|data: &[u8]| {
if let Ok(simdnbt::borrow::Nbt::Some(r)) =
simdnbt::borrow::read(&mut std::io::Cursor::new(data))
{
r.as_compound().to_owned();
simdnbt::owned::BaseNbt::write_unnamed(&r.as_compound().to_owned().into(), &mut Vec::new());
}
});

View file

@ -1,9 +1,10 @@
use std::{
fs::File,
hint::black_box,
io::{Cursor, Read},
};
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use flate2::read::GzDecoder;
fn bench_read_file(filename: &str, c: &mut Criterion) {

View file

@ -1,10 +1,11 @@
use std::{
collections::HashMap,
fs::File,
hint::black_box,
io::{Cursor, Read},
};
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use flate2::read::GzDecoder;
pub fn bench_read_file(filename: &str, c: &mut Criterion) {

View file

@ -1,9 +1,10 @@
use std::{
fs::File,
hint::black_box,
io::{Cursor, Read},
};
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use flate2::read::GzDecoder;
fn bench_file(filename: &str, c: &mut Criterion) {

View file

@ -1,9 +1,10 @@
use std::{
fs::File,
hint::black_box,
io::{Cursor, Read},
};
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use flate2::read::GzDecoder;
fn bench_file(filename: &str, c: &mut Criterion) {

View file

@ -271,10 +271,13 @@ impl<'orig, T> FastVecFromVec<'orig, T> {
}
}
impl<T> Drop for FastVecFromVec<'_, T> {
/// Move the FastVec contents back into the original Vec.
fn drop(&mut self) {
// we intentionally don't drop the fastvec since the allocation is moved into
// the vec
*self.original = unsafe { ManuallyDrop::take(&mut self.fastvec).into_vec() };
// we intentionally don't drop anything here
unsafe {
let new_vec = ManuallyDrop::take(&mut self.fastvec).into_vec();
(self.original as *mut Vec<T>).write(new_vec);
}
}
}

View file

@ -810,4 +810,13 @@ mod tests {
assert_ne!(nbt, modified_nbt);
}
#[test]
fn write_compound() {
let nbt = BaseNbt {
name: "".into(),
tag: NbtCompound { values: vec![] },
};
BaseNbt::write_unnamed(&nbt, &mut Vec::new());
}
}