ReagentX / imessage-exporter

Export iMessage data + run iMessage Diagnostics
GNU General Public License v3.0
3.08k stars 135 forks source link

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

Closed agileadam closed 1 week ago

agileadam commented 1 week ago

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 commented 1 week ago

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:

https://github.com/ReagentX/imessage-exporter/blob/fa67b1d798291f1dc04cabf5d68d384ba66ee8e1/imessage-database/src/message_types/handwriting/models.rs#L202-L206

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:

https://github.com/ReagentX/imessage-exporter/blob/fa67b1d798291f1dc04cabf5d68d384ba66ee8e1/imessage-database/src/message_types/handwriting/models.rs#L218-L226