From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Dominik Csapak <d.csapak@proxmox.com>
Cc: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] applied: [PATCH proxmox-backup v2] server/worker_task: improve newline handling in upid_read_status
Date: Tue, 26 Jan 2021 10:52:50 +0100 [thread overview]
Message-ID: <20210126095250.2wk5gnnelfhnlnp2@wobu-vie.proxmox.com> (raw)
In-Reply-To: <20210125113229.27666-1-d.csapak@proxmox.com>
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 <d.csapak@proxmox.com>
> ---
> 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<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(_) => &data, // should not happen, since we removed all trailing newlines
> + None => &data,
> };
>
> let last_line = std::str::from_utf8(last_line)
> --
> 2.20.1
prev parent reply other threads:[~2021-01-26 9:53 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-25 11:32 [pbs-devel] " Dominik Csapak
2021-01-26 9:52 ` Wolfgang Bumiller [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210126095250.2wk5gnnelfhnlnp2@wobu-vie.proxmox.com \
--to=w.bumiller@proxmox.com \
--cc=d.csapak@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox