summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVitaly Buka <vitalybuka@google.com>2016-11-15 00:01:40 +0000
committerVitaly Buka <vitalybuka@google.com>2016-11-15 00:01:40 +0000
commit3ee54a693394c3e00cd0357650665fed4cf94695 (patch)
tree7fe1f84fc87321608b57735411add2c334831f99
parent18270a843a959d5a2cc02daf15caa00dbc350f73 (diff)
downloadbcm5719-llvm-3ee54a693394c3e00cd0357650665fed4cf94695.tar.gz
bcm5719-llvm-3ee54a693394c3e00cd0357650665fed4cf94695.zip
Avoid calling std::memcmp with nullptr
Summary: UBSAN complains that this is undefined behavior. We can assume that empty substring (N==1) always satisfy conditions. So std::memcmp will be called only only for N > 1 and Str.size() > 0. Reviewers: ruiu, zturner Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D26646 llvm-svn: 286910
-rw-r--r--llvm/include/llvm/ADT/StringSwitch.h9
1 files changed, 6 insertions, 3 deletions
diff --git a/llvm/include/llvm/ADT/StringSwitch.h b/llvm/include/llvm/ADT/StringSwitch.h
index d2a0521f73b..75577b7738b 100644
--- a/llvm/include/llvm/ADT/StringSwitch.h
+++ b/llvm/include/llvm/ADT/StringSwitch.h
@@ -72,8 +72,9 @@ public:
template<unsigned N>
LLVM_ATTRIBUTE_ALWAYS_INLINE
StringSwitch& Case(const char (&S)[N], const T& Value) {
+ assert(N);
if (!Result && N-1 == Str.size() &&
- (std::memcmp(S, Str.data(), N-1) == 0)) {
+ (N == 1 || std::memcmp(S, Str.data(), N-1) == 0)) {
Result = &Value;
}
return *this;
@@ -82,8 +83,9 @@ public:
template<unsigned N>
LLVM_ATTRIBUTE_ALWAYS_INLINE
StringSwitch& EndsWith(const char (&S)[N], const T &Value) {
+ assert(N);
if (!Result && Str.size() >= N-1 &&
- std::memcmp(S, Str.data() + Str.size() + 1 - N, N-1) == 0) {
+ (N == 1 || std::memcmp(S, Str.data() + Str.size() + 1 - N, N-1) == 0)) {
Result = &Value;
}
return *this;
@@ -92,8 +94,9 @@ public:
template<unsigned N>
LLVM_ATTRIBUTE_ALWAYS_INLINE
StringSwitch& StartsWith(const char (&S)[N], const T &Value) {
+ assert(N);
if (!Result && Str.size() >= N-1 &&
- std::memcmp(S, Str.data(), N-1) == 0) {
+ (N == 1 || std::memcmp(S, Str.data(), N-1) == 0)) {
Result = &Value;
}
return *this;
OpenPOWER on IntegriCloud