From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 461846AE59 for ; Mon, 25 Jan 2021 12:33:01 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 35D729B11 for ; Mon, 25 Jan 2021 12:32:31 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id BA8669B08 for ; Mon, 25 Jan 2021 12:32:30 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 8129345782 for ; Mon, 25 Jan 2021 12:32:30 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Mon, 25 Jan 2021 12:32:29 +0100 Message-Id: <20210125113229.27666-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.264 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup v2] server/worker_task: improve newline handling in upid_read_status X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jan 2021 11:33:01 -0000 improves upid_read_status with: * ignore multiple newlines at the end * remove all code that could panic (array index access) the one place where we access with '[pos+1..]' is ok since we explicitely test the len of the vector, this is done to let rust optimize away the range checks, so it cannot panic Signed-off-by: Dominik Csapak --- changes from v1: * Some(start) to Some(_) to avoid unused variable warning src/server/worker_task.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/server/worker_task.rs b/src/server/worker_task.rs index 967814c9..4a73ff0b 100644 --- a/src/server/worker_task.rs +++ b/src/server/worker_task.rs @@ -190,20 +190,15 @@ pub fn upid_read_status(upid: &UPID) -> Result { let mut data = Vec::with_capacity(8192); file.read_to_end(&mut data)?; - // task logs should end with newline, we do not want it here - if !data.is_empty() && data[data.len()-1] == b'\n' { + // strip newlines at the end of the task logs + while data.last() == Some(&b'\n') { data.pop(); } - let last_line = { - let mut start = 0; - for pos in (0..data.len()).rev() { - if data[pos] == b'\n' { - start = data.len().min(pos + 1); - break; - } - } - &data[start..] + let last_line = match data.iter().rposition(|c| *c == b'\n') { + Some(start) if data.len() > (start+1) => &data[start+1..], + Some(_) => &data, // should not happen, since we removed all trailing newlines + None => &data, }; let last_line = std::str::from_utf8(last_line) -- 2.20.1