From 64df9f28c8452500506af3361dd079e78f736ad5 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Tue, 30 Nov 2021 19:46:18 +0100 Subject: Fix UBSan error (ptr + offset overflow) As `i + offset` is promoted to a "negative" size_t, UBSan would complain when adding the resulting offset to `dst`: ``` /tmp/RtmptDX1SS/file584e37df4e/snappy_ep-prefix/src/snappy_ep/snappy.cc:343:43: runtime error: addition of unsigned offset to 0x6120003c5ec1 overflowed to 0x6120003c5ec0 #0 0x7f9ebd21769c in snappy::(anonymous namespace)::Copy64BytesWithPatternExtension(char*, unsigned long) /tmp/RtmptDX1SS/file584e37df4e/snappy_ep-prefix/src/snappy_ep/snappy.cc:343:43 #1 0x7f9ebd21769c in std::__1::pair snappy::DecompressBranchless(unsigned char const*, unsigned char const*, long, char*, long) /tmp/RtmptDX1SS/file584e37df4e/snappy_ep-prefix/src/snappy_ep/snappy.cc:1160:15 ``` --- snappy.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snappy.cc b/snappy.cc index ee9a2c4..bb9e0e5 100644 --- a/snappy.cc +++ b/snappy.cc @@ -340,7 +340,7 @@ static inline bool Copy64BytesWithPatternExtension(char* dst, size_t offset) { if (SNAPPY_PREDICT_TRUE(offset < 16)) { if (SNAPPY_PREDICT_FALSE(offset == 0)) return false; // Extend the pattern to the first 16 bytes. - for (int i = 0; i < 16; i++) dst[i] = dst[i - offset]; + for (int i = 0; i < 16; i++) dst[i] = (dst - offset)[i]; // Find a multiple of pattern >= 16. static std::array pattern_sizes = []() { std::array res; -- cgit v1.2.1