public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox-backup] server/worker_task: improve newline handling in upid_read_status
@ 2021-01-25 11:28 Dominik Csapak
  0 siblings, 0 replies; only message in thread
From: Dominik Csapak @ 2021-01-25 11:28 UTC (permalink / raw)
  To: pbs-devel

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 <d.csapak@proxmox.com>
---
 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..8b0d88fd 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<TaskState, Error> {
     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(start) => &data, // should not happen, since we removed all trailing newlines
+        None => &data,
     };
 
     let last_line = std::str::from_utf8(last_line)
-- 
2.20.1





^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-01-25 11:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-25 11:28 [pbs-devel] [PATCH proxmox-backup] server/worker_task: improve newline handling in upid_read_status Dominik Csapak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal