[libcxxabi] r231839 - [libcxx] Fix PR21580 - Undefined behavior in readEncodedPointer()
Eric Fiselier
eric at efcs.ca
Tue Mar 10 13:43:34 PDT 2015
Author: ericwf
Date: Tue Mar 10 15:43:34 2015
New Revision: 231839
URL: http://llvm.org/viewvc/llvm-project?rev=231839&view=rev
Log:
[libcxx] Fix PR21580 - Undefined behavior in readEncodedPointer()
Summary: This patch fixes a bug in `readEncodedPointer()` where it would read from memory that was not suitably aligned. This patch fixes it by using memcpy.
Reviewers: danalbert, echristo, compnerd, mclow.lists
Reviewed By: compnerd, mclow.lists
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D8179
Modified:
libcxxabi/trunk/src/cxa_personality.cpp
Modified: libcxxabi/trunk/src/cxa_personality.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/cxa_personality.cpp?rev=231839&r1=231838&r2=231839&view=diff
==============================================================================
--- libcxxabi/trunk/src/cxa_personality.cpp (original)
+++ libcxxabi/trunk/src/cxa_personality.cpp Tue Mar 10 15:43:34 2015
@@ -14,6 +14,7 @@
#include <assert.h>
#include <stdlib.h>
+#include <string.h>
#include <typeinfo>
#include "config.h"
@@ -141,6 +142,19 @@ Notes:
namespace __cxxabiv1
{
+namespace
+{
+
+template <class AsType>
+uintptr_t readPointerHelper(const uint8_t*& p) {
+ AsType value;
+ memcpy(&value, const_cast<uint8_t*>(p), sizeof(AsType));
+ p += sizeof(AsType);
+ return static_cast<uintptr_t>(value);
+}
+
+} // end namespace
+
extern "C"
{
@@ -245,28 +259,22 @@ readEncodedPointer(const uint8_t** data,
result = static_cast<uintptr_t>(readSLEB128(&p));
break;
case DW_EH_PE_udata2:
- result = *((uint16_t*)p);
- p += sizeof(uint16_t);
+ result = readPointerHelper<uint16_t>(p);
break;
case DW_EH_PE_udata4:
- result = *((uint32_t*)p);
- p += sizeof(uint32_t);
+ result = readPointerHelper<uint32_t>(p);
break;
case DW_EH_PE_udata8:
- result = static_cast<uintptr_t>(*((uint64_t*)p));
- p += sizeof(uint64_t);
+ result = readPointerHelper<uint64_t>(p);
break;
case DW_EH_PE_sdata2:
- result = static_cast<uintptr_t>(*((int16_t*)p));
- p += sizeof(int16_t);
+ result = readPointerHelper<int16_t>(p);
break;
case DW_EH_PE_sdata4:
- result = static_cast<uintptr_t>(*((int32_t*)p));
- p += sizeof(int32_t);
+ result = readPointerHelper<int32_t>(p);
break;
case DW_EH_PE_sdata8:
- result = static_cast<uintptr_t>(*((int64_t*)p));
- p += sizeof(int64_t);
+ result = readPointerHelper<int64_t>(p);
break;
default:
// not supported
More information about the cfe-commits
mailing list