Fix nfcsnoop_cb on overflow sanitized builds.
On builds with integer overflow sanitizer enabled, a runtime error
occurs due to an overflow when calculating a time delta:
runtime error: unsigned integer overflow: 1513015414325284 -
1513015414449720 cannot be represented in type 'unsigned long'
This overflow looks benign, so this refactors the code to use the
built-in overflow function so the runtime error no longer occurs.
Bug: 30969751
Test: Compiles, device boots.
Change-Id: I84e2f033c22f94ec3675336a53e50f7f794cb7a6
diff --git a/src/adaptation/debug_nfcsnoop.cc b/src/adaptation/debug_nfcsnoop.cc
index b58554e..9466b9f 100644
--- a/src/adaptation/debug_nfcsnoop.cc
+++ b/src/adaptation/debug_nfcsnoop.cc
@@ -60,8 +60,13 @@
// Insert data
header.length = length;
header.is_received = is_received ? 1 : 0;
- header.delta_time_ms =
- last_timestamp_ms ? timestamp_us - last_timestamp_ms : 0;
+
+ uint64_t delta_time_ms = 0;
+ if (last_timestamp_ms) {
+ __builtin_sub_overflow(timestamp_us, last_timestamp_ms, &delta_time_ms);
+ }
+ header.delta_time_ms = delta_time_ms;
+
last_timestamp_ms = timestamp_us;
ringbuffer_insert(buffer, (uint8_t*)&header, sizeof(nfcsnooz_header_t));