From ae2dbec59a5d161af102be4a88c77bd21f076a26 Mon Sep 17 00:00:00 2001 From: Kaile Date: Mon, 14 Nov 2022 21:00:49 +0800 Subject: [PATCH] Fix infinite `while` loop when buf size is less than `3` --- src/matchers/text.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/matchers/text.rs b/src/matchers/text.rs index 8f95fdd..60297f7 100644 --- a/src/matchers/text.rs +++ b/src/matchers/text.rs @@ -64,14 +64,12 @@ fn trim_start_whitespaces(mut buf: &[u8]) -> &[u8] { /// Strip BOM at the beginning of the buffer. fn trim_start_byte_order_marks(mut buf: &[u8]) -> &[u8] { - while !buf.is_empty() { - if buf.len() >= 3 { - match (buf[0], buf[1], buf[2]) { - (0xEF, 0xBB, 0xBF) => buf = &buf[3..], // UTF-8 - (0xFE, 0xFF, _) => buf = &buf[2..], // UTF-16 BE - (0xFF, 0xFE, _) => buf = &buf[2..], // UTF-16 BE - _ => break, - } + while buf.len() >= 3 { + match (buf[0], buf[1], buf[2]) { + (0xEF, 0xBB, 0xBF) => buf = &buf[3..], // UTF-8 + (0xFE, 0xFF, _) => buf = &buf[2..], // UTF-16 BE + (0xFF, 0xFE, _) => buf = &buf[2..], // UTF-16 BE + _ => break, } } buf