aos: Fix undefined behaviour in shm_event_loop_test.cc
With `--test_env=UBSAN_OPTIONS=halt_on_error=1`, the test was crashing
with this error:
external/aos/aos/events/shm_event_loop_test.cc:398: Failure
Death test: { ++static_cast<char *>(sender->data())[-1 - i]; sender->CheckOk(sender->Send(0)); }
Result: died but not with expected error.
Expected: contains regular expression "Somebody wrote outside the buffer of their message"
Actual msg:
[ DEATH ] external/aos/aos/events/shm_event_loop_test.cc:393:5: runtime error: addition of unsigned offset to 0x7fddcf81b118 overflowed to 0x7fddcf81b0f8
We fix the issue by performing pointer arithmetic manually rather than
accessing the memory via an out-of-bounds element access.
I'm not entirely sure why the sanitizer is complaining here. I'm
guessing that it's tracking the location and size of the array that
`data()` is pointing to.
Change-Id: I61d76db243423b4c41f17bbea1ba50b804a93b43
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/events/shm_event_loop_test.cc b/aos/events/shm_event_loop_test.cc
index 2382f07..2134687 100644
--- a/aos/events/shm_event_loop_test.cc
+++ b/aos/events/shm_event_loop_test.cc
@@ -389,7 +389,9 @@
SCOPED_TRACE(std::to_string(i));
EXPECT_DEATH(
{
- ++static_cast<char *>(sender->data())[-1 - i];
+ // Can't use `data()[-1 -i]` here because that's undefined behaviour.
+ // We do manual pointer arithmetic to work around that.
+ ++(*(static_cast<char *>(sender->data()) - 1 - i));
sender->CheckOk(sender->Send(0));
},
"Somebody wrote outside the buffer of their message");