hediet/slideo

attempt to subtract with overflow in db.rs

Closed this issue · 1 comments

When trying to build and run on macos/linux I get the following error:
thread 'main' panicked at 'attempt to subtract with overflow', crates/app/src/db.rs:180:31
I seemed to have already fixed the issue by modifying the db.rs file (after some googling I found out that this issue seems to accur, when you try to subtract 1 from 0)

diff --git a/crates/app/src/db.rs b/crates/app/src/db.rs
index 2478358..da7e2b1 100644
--- a/crates/app/src/db.rs
+++ b/crates/app/src/db.rs
@@ -177,7 +177,12 @@ impl<'a> Db<'a, TransactionMarker> {
             let pdf_hash = matching.image.map(|p| p.pdf_hash);
             let video_ms = matching.video_time.as_millis() as u32;
             let page_nr = matching.image.map(|p| p.page_nr as u32).unwrap_or(0);
-            let page_offset = page_nr - 1;
+            let page_offset;
+            if page_nr > 0 {
+                page_offset = page_nr - 1;
+            } else {
+                page_offset = 0;
+            }
             sqlx::query!(
                 "INSERT INTO videos_mapping(video_id, video_ms, pdf_hash, page) VALUES (?, ?, ?, ?)",
                 video_id,

I don't understand how I missed that bug. Thanks for spotting it! I'll fix it soon.

Interestingly the overflow was just ignored on my machine:
image