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 0E1C56B388 for ; Tue, 26 Jan 2021 10:53:22 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id F342A14AA6 for ; Tue, 26 Jan 2021 10:52:51 +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 8329814A9C for ; Tue, 26 Jan 2021 10:52:51 +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 472C141A13 for ; Tue, 26 Jan 2021 10:52:51 +0100 (CET) Date: Tue, 26 Jan 2021 10:52:50 +0100 From: Wolfgang Bumiller To: Dominik Csapak Cc: pbs-devel@lists.proxmox.com Message-ID: <20210126095250.2wk5gnnelfhnlnp2@wobu-vie.proxmox.com> References: <20210125113229.27666-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210125113229.27666-1-d.csapak@proxmox.com> User-Agent: NeoMutt/20180716 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.047 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] applied: [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: Tue, 26 Jan 2021 09:53:22 -0000 applied On Mon, Jan 25, 2021 at 12:32:29PM +0100, Dominik Csapak wrote: > 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