From 254579df2c893075cb61c3a0efb91bbe1601a6ba Mon Sep 17 00:00:00 2001 From: Kai Nacke Date: Tue, 5 Nov 2013 11:44:21 +0100 Subject: [PATCH] Correct issues found by AddressSanitizer. --- dmd2/identifier.c | 17 +++++++++++++++++ dmd2/mangle.c | 4 ++++ dmd2/root/rmem.c | 11 +++++++++++ dmd2/scope.c | 4 ++++ 4 files changed, 36 insertions(+) diff --git a/dmd2/identifier.c b/dmd2/identifier.c index 9146fc3a..2ef37b60 100644 --- a/dmd2/identifier.c +++ b/dmd2/identifier.c @@ -32,12 +32,20 @@ hash_t Identifier::hashCode() bool Identifier::equals(RootObject *o) { +#if IN_LLVM // ASan + return this == o || strncmp(string,o->toChars(),len+1) == 0; +#else return this == o || memcmp(string,o->toChars(),len+1) == 0; +#endif } int Identifier::compare(RootObject *o) { +#if IN_LLVM // ASan + return strncmp(string, o->toChars(), len + 1); +#else return memcmp(string, o->toChars(), len + 1); +#endif } char *Identifier::toChars() @@ -60,12 +68,21 @@ const char *Identifier::toHChars2() { p = toChars(); if (*p == '_') { +#if IN_LLVM // ASan + if (strncmp(p, "_staticCtor", 11) == 0) + p = "static this"; + else if (strncmp(p, "_staticDtor", 11) == 0) + p = "static ~this"; + else if (strncmp(p, "__invariant", 11) == 0) + p = "invariant"; +#else if (memcmp(p, "_staticCtor", 11) == 0) p = "static this"; else if (memcmp(p, "_staticDtor", 11) == 0) p = "static ~this"; else if (memcmp(p, "__invariant", 11) == 0) p = "invariant"; +#endif } } diff --git a/dmd2/mangle.c b/dmd2/mangle.c index 45881436..253a24ef 100644 --- a/dmd2/mangle.c +++ b/dmd2/mangle.c @@ -311,7 +311,11 @@ const char *ClassDeclaration::mangle(bool isv) this == object || this == Type::typeinfoclass || this == Module::moduleinfo || +#if IN_LLVM // ASan + strncmp(ident->toChars(), "TypeInfo_", 9) == 0 +#else memcmp(ident->toChars(), "TypeInfo_", 9) == 0 +#endif ) parent = NULL; diff --git a/dmd2/root/rmem.c b/dmd2/root/rmem.c index 1ba38ff2..6fb249f3 100644 --- a/dmd2/root/rmem.c +++ b/dmd2/root/rmem.c @@ -138,6 +138,14 @@ void Mem::addroots(char* pStart, char* pEnd) /* =================================================== */ +#if defined(__has_feature) +#if !__has_feature(address_sanitizer) +#define DEFINE_NEW_DELETE +#endif +#endif + +#if defined(DEFINE_NEW_DELETE) + #if IN_DMD /* Allocate, but never release @@ -209,3 +217,6 @@ void operator delete(void *p) } #endif + +#endif + diff --git a/dmd2/scope.c b/dmd2/scope.c index be12f2cb..265ae966 100644 --- a/dmd2/scope.c +++ b/dmd2/scope.c @@ -278,9 +278,13 @@ unsigned *Scope::saveFieldInit() { size_t dim = fieldinit_dim; fi = new unsigned[dim]; +#if IN_LLVM // ASan + memcpy(fi, fieldinit, sizeof(*fi) * dim); +#else fi[0] = dim; for (size_t i = 0; i < dim; i++) fi[i] = fieldinit[i]; +#endif } return fi; }