server/worker_task: improve newline handling in upid_read_status
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>
This commit is contained in:
parent
340c0bf9e3
commit
6864fd0149
|
@ -190,20 +190,15 @@ pub fn upid_read_status(upid: &UPID) -> Result<TaskState, Error> {
|
||||||
let mut data = Vec::with_capacity(8192);
|
let mut data = Vec::with_capacity(8192);
|
||||||
file.read_to_end(&mut data)?;
|
file.read_to_end(&mut data)?;
|
||||||
|
|
||||||
// task logs should end with newline, we do not want it here
|
// strip newlines at the end of the task logs
|
||||||
if !data.is_empty() && data[data.len()-1] == b'\n' {
|
while data.last() == Some(&b'\n') {
|
||||||
data.pop();
|
data.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
let last_line = {
|
let last_line = match data.iter().rposition(|c| *c == b'\n') {
|
||||||
let mut start = 0;
|
Some(start) if data.len() > (start+1) => &data[start+1..],
|
||||||
for pos in (0..data.len()).rev() {
|
Some(_) => &data, // should not happen, since we removed all trailing newlines
|
||||||
if data[pos] == b'\n' {
|
None => &data,
|
||||||
start = data.len().min(pos + 1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
&data[start..]
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let last_line = std::str::from_utf8(last_line)
|
let last_line = std::str::from_utf8(last_line)
|
||||||
|
|
Loading…
Reference in New Issue