From ad8b1b7b2405b39d0f86b7adb00df7662dcd1a19 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 9 Nov 2022 18:59:03 +0000 Subject: [PATCH] ignore bad utf8 --- Cargo.lock | 1 + azalea-client/src/client.rs | 3 +-- azalea-nbt/Cargo.toml | 1 + azalea-nbt/src/decode.rs | 10 +++++++++- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e7687cb..73103862 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,6 +246,7 @@ dependencies = [ "byteorder", "criterion", "flate2", + "log", "num-derive", "num-traits", ] diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index ac340ede..645497f6 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -583,8 +583,7 @@ impl Client { client .dimension .lock() - .replace_with_packet_data(&pos, &mut Cursor::new(&p.chunk_data.data)) - .unwrap(); + .replace_with_packet_data(&pos, &mut Cursor::new(&p.chunk_data.data)); } ClientboundGamePacket::LightUpdate(_p) => { // debug!("Got light update packet {:?}", p); diff --git a/azalea-nbt/Cargo.toml b/azalea-nbt/Cargo.toml index 5e776dbe..f104e7bc 100644 --- a/azalea-nbt/Cargo.toml +++ b/azalea-nbt/Cargo.toml @@ -13,6 +13,7 @@ ahash = "^0.8.0" azalea-buf = {path = "../azalea-buf", version = "^0.3.0" } byteorder = "^1.4.3" flate2 = "^1.0.23" +log = "0.4.17" num-derive = "^0.3.3" num-traits = "^0.2.14" diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs index 6937dc05..ae7cfde6 100755 --- a/azalea-nbt/src/decode.rs +++ b/azalea-nbt/src/decode.rs @@ -4,6 +4,7 @@ use ahash::AHashMap; use azalea_buf::{BufReadError, McBufReadable}; use byteorder::{ReadBytesExt, BE}; use flate2::read::{GzDecoder, ZlibDecoder}; +use log::warn; use std::io::Cursor; use std::io::{BufRead, Read}; @@ -23,7 +24,14 @@ fn read_string(stream: &mut Cursor<&[u8]>) -> Result { let length = stream.read_u16::()? as usize; let buf = read_bytes(stream, length)?; - Ok(std::str::from_utf8(buf)?.to_string()) + + Ok(if let Ok(string) = std::str::from_utf8(buf) { + string.to_string() + } else { + let lossy_string = String::from_utf8_lossy(buf).into_owned(); + warn!("Error decoding utf8 (bytes: {buf:?}, lossy: \"{lossy_string})\""); + lossy_string + }) } impl Tag {