[clang-tools-extra] r327902 - [clangd] Fix undefined behavior due to misaligned type cast

Jan Korous via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 19 13:26:18 PDT 2018


Author: jkorous
Date: Mon Mar 19 13:26:18 2018
New Revision: 327902

URL: http://llvm.org/viewvc/llvm-project?rev=327902&view=rev
Log:
[clangd] Fix undefined behavior due to misaligned type cast

The current code was casting pointer to a misaligned type which is undefined behavior.
Found by compiling with Undefined Behavior Sanitizer and running tests (check-clang-tools).

Modified:
    clang-tools-extra/trunk/clangd/index/Index.h

Modified: clang-tools-extra/trunk/clangd/index/Index.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/Index.h?rev=327902&r1=327901&r2=327902&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/Index.h (original)
+++ clang-tools-extra/trunk/clangd/index/Index.h Mon Mar 19 13:26:18 2018
@@ -61,7 +61,9 @@ private:
   friend llvm::hash_code hash_value(const SymbolID &ID) {
     // We already have a good hash, just return the first bytes.
     static_assert(sizeof(size_t) <= HashByteLength, "size_t longer than SHA1!");
-    return *reinterpret_cast<const size_t *>(ID.HashValue.data());
+    size_t Result;
+    memcpy(&Result, ID.HashValue.data(), sizeof(size_t));
+    return llvm::hash_code(Result);
   }
   friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
                                        const SymbolID &ID);




More information about the cfe-commits mailing list