public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup v2 10/11] tools/systemd/time: fix signed conversion
Date: Fri,  4 Sep 2020 14:33:33 +0200	[thread overview]
Message-ID: <20200904123334.3731-11-d.csapak@proxmox.com> (raw)
In-Reply-To: <20200904123334.3731-1-d.csapak@proxmox.com>

instead of using 'as' and silently converting wrong,
use the TryInto trait and raise an error if we cannot convert

this should only happen if we have a negative year,
but this is expected (we do not want schedules from before the year 0)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/tools/systemd/time.rs | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/tools/systemd/time.rs b/src/tools/systemd/time.rs
index 69f5f5fb..7717ecee 100644
--- a/src/tools/systemd/time.rs
+++ b/src/tools/systemd/time.rs
@@ -1,3 +1,5 @@
+use std::convert::TryInto;
+
 use anyhow::Error;
 use bitflags::bitflags;
 
@@ -174,17 +176,17 @@ pub fn compute_next_event(
         }
 
         if !all_days { // match day first
-            let day_num = t.day_num();
+            let day_num: u32 = t.day_num().try_into()?;
             let day = WeekDays::from_bits(1<<day_num).unwrap();
             if !event.days.contains(day) {
                 if let Some(n) = ((day_num+1)..7)
                     .find(|d| event.days.contains(WeekDays::from_bits(1<<d).unwrap()))
                 {
                     // try next day
-                    t.add_days(n - day_num,)?;
+                    t.add_days((n - day_num).try_into()?)?;
                 } else {
                     // try next week
-                    t.add_days(7 - day_num)?;
+                    t.add_days((7 - day_num).try_into()?)?;
                 }
                 continue;
             }
@@ -192,11 +194,11 @@ pub fn compute_next_event(
 
         // this day
         if !event.hour.is_empty() {
-            let hour = t.hour() as u32;
+            let hour = t.hour().try_into()?;
             if !DateTimeValue::list_contains(&event.hour, hour) {
                 if let Some(n) = DateTimeValue::find_next(&event.hour, hour) {
                     // test next hour
-                    t.set_time(n as libc::c_int, 0, 0)?;
+                    t.set_time(n.try_into()?, 0, 0)?;
                 } else {
                     // test next day
                     t.add_days(1)?;
@@ -207,11 +209,11 @@ pub fn compute_next_event(
 
         // this hour
         if !event.minute.is_empty() {
-            let minute = t.min() as u32;
+            let minute = t.min().try_into()?;
             if !DateTimeValue::list_contains(&event.minute, minute) {
                 if let Some(n) = DateTimeValue::find_next(&event.minute, minute) {
                     // test next minute
-                    t.set_min_sec(n as libc::c_int, 0)?;
+                    t.set_min_sec(n.try_into()?, 0)?;
                 } else {
                     // test next hour
                     t.set_time(t.hour() + 1, 0, 0)?;
@@ -222,11 +224,11 @@ pub fn compute_next_event(
 
         // this minute
         if !event.second.is_empty() {
-            let second = t.sec() as u32;
+            let second = t.sec().try_into()?;
             if !DateTimeValue::list_contains(&event.second, second) {
                 if let Some(n) = DateTimeValue::find_next(&event.second, second) {
                     // test next second
-                    t.set_sec(n as libc::c_int)?;
+                    t.set_sec(n.try_into()?)?;
                 } else {
                     // test next min
                     t.set_min_sec(t.min() + 1, 0)?;
-- 
2.20.1





  parent reply	other threads:[~2020-09-04 12:34 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-04 12:33 [pbs-devel] [PATCH proxmox-backup v2 00/11] implement dates for calendarevents Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 01/11] tools/systemd/tm_editor: remove TMChanges optimization Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 02/11] tools/systemd/time: let libc normalize time for us Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 03/11] tools/systemd/time: move continue out of the if/else Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 04/11] tools/systemd/time: convert the resulting timestamp into an option Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 05/11] tools/systemd/tm_editor: remove reset_time from add_days and document it Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 06/11] tools/systemd/parse_time: error out on invalid ranges Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 07/11] tools/systemd/time: fix selection for multiple options Dominik Csapak
2020-09-04 13:38   ` Dietmar Maurer
2020-09-07  6:27     ` Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 08/11] tools/systemd/tm_editor: move conversion of the year into getter and setter Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 09/11] tools/systemd/tm_editor: add setter/getter for months/years/days Dominik Csapak
2020-09-04 12:33 ` Dominik Csapak [this message]
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 11/11] tools/systemd/time: enable dates for calendarevents Dominik Csapak
2020-09-04 13:36 ` [pbs-devel] applied: [PATCH proxmox-backup v2 00/11] implement " Dietmar Maurer

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=20200904123334.3731-11-d.csapak@proxmox.com \
    --to=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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal