Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

"attempt to divide by zero" in handwriting/models.rs #360

Closed
agileadam opened this issue Nov 12, 2024 · 1 comment · Fixed by #364
Closed

"attempt to divide by zero" in handwriting/models.rs #360

agileadam opened this issue Nov 12, 2024 · 1 comment · Fixed by #364
Assignees
Labels
bug Something isn't working crate: database Related to the database crate needs info More information required to diagnose

Comments

@agileadam
Copy link

I was getting this error on the latest release while trying imessage-exporter --format=html --copy-method=efficient:

thread 'main' panicked at imessage-database/src/message_types/handwriting/models.rs:215:5:
attempt to divide by zero
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I was able to get past the error by making this change and recompiling.
DISCLAIMER: I have never programmed in rust. This change is straight from ChatGPT.
I assume the developer(s) will address the bug accordingly.

diff --git a/imessage-database/src/message_types/handwriting/models.rs b/imessage-database/src/message_types/handwriting/models.rs
index 67e4a00..321d4fd 100644
--- a/imessage-database/src/message_types/handwriting/models.rs
+++ b/imessage-database/src/message_types/handwriting/models.rs
@@ -212,7 +212,12 @@ fn fit_strokes(

 /// Resize converts `v` from a coordinate where `max_v` is the current height/width and `box_size` is the wanted height/width.
 fn resize(v: u16, box_size: u16, max_v: u16) -> u16 {
-    (v as i64 * box_size as i64 / max_v as i64) as u16
+    if max_v == 0 {
+        // Return 0, or handle this case differently if another behavior is desired
+        0
+    } else {
+        (v as i64 * box_size as i64 / max_v as i64) as u16
+    }
 }
@ReagentX
Copy link
Owner

ReagentX commented Nov 12, 2024

If possible, can you send the handwritten message blob? Would be nice to have a unit test for this. max_x and max_y (which are passed as max_v in this function) are the width and height of the SVG:

Point {
x: resize(point.x, width, max_x),
y: resize(point.y, height, max_y),
width: resize(point.width, 9, max_width)+1,
}

This error indicates that the handwritten message had 0 for height, width, or both; or possibly that there were simply zero points in the handwritten message altogether:

/// Iterates through each point in each stroke and extracts the maximum `x`, `y`, and `width` values.
fn get_max_dimension(strokes: &[Vec<Point>]) -> (u16, u16, u16) {
strokes
.iter()
.flat_map(|stroke| stroke.iter())
.fold((0, 0, 0), |(max_x, max_y, max_width), point| {
(max_x.max(point.x), max_y.max(point.y), max_width.max(point.width-1))
})
}

@ReagentX ReagentX self-assigned this Nov 12, 2024
@ReagentX ReagentX added crate: cli Related to the CLI crate exporter Related to exporter processes labels Nov 12, 2024
@ReagentX ReagentX moved this to Todo in 2.2: Live Oak Nov 12, 2024
@ReagentX ReagentX added crate: database Related to the database crate needs info More information required to diagnose and removed crate: cli Related to the CLI crate labels Nov 12, 2024
ReagentX added a commit that referenced this issue Nov 15, 2024
@ReagentX ReagentX added bug Something isn't working and removed exporter Related to exporter processes labels Nov 15, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in 2.2: Live Oak Nov 15, 2024
@ReagentX ReagentX mentioned this issue Nov 23, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working crate: database Related to the database crate needs info More information required to diagnose
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants