From 16e71eac43856e23d6cabc44fd2e7f2039a86a15 Mon Sep 17 00:00:00 2001 From: Thad House Date: Wed, 10 May 2017 20:14:14 -0700 Subject: [PATCH] Fixes assertions to not crash StringRef (#534) We can't read [0] on an empty StringRef or the StringRef itself asserts (this may crash in non-debug mode as an empty StringRef is not guaranteed to have a valid pointer). Instead, we can just check if the StringRef is not empty. Closes #502 --- wpilibc/athena/src/Utility.cpp | 4 ++-- wpilibc/sim/src/Utility.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/wpilibc/athena/src/Utility.cpp b/wpilibc/athena/src/Utility.cpp index 741691a63b..1a5325167d 100644 --- a/wpilibc/athena/src/Utility.cpp +++ b/wpilibc/athena/src/Utility.cpp @@ -41,7 +41,7 @@ bool wpi_assert_impl(bool conditionValue, llvm::StringRef conditionText, errorStream << "Assertion \"" << conditionText << "\" "; - if (message[0] != '\0') { + if (!message.empty()) { errorStream << "failed: " << message << std::endl; } else { errorStream << "failed." << std::endl; @@ -78,7 +78,7 @@ void wpi_assertEqual_common_impl(llvm::StringRef valueA, llvm::StringRef valueB, errorStream << "Assertion \"" << valueA << " " << equalityType << " " << valueB << "\" "; - if (message[0] != '\0') { + if (!message.empty()) { errorStream << "failed: " << message << std::endl; } else { errorStream << "failed." << std::endl; diff --git a/wpilibc/sim/src/Utility.cpp b/wpilibc/sim/src/Utility.cpp index 97c02def49..1f386f9818 100644 --- a/wpilibc/sim/src/Utility.cpp +++ b/wpilibc/sim/src/Utility.cpp @@ -69,7 +69,7 @@ bool wpi_assert_impl(bool conditionValue, llvm::StringRef conditionText, llvm::SmallString<128> fileTemp; errorStream << "of " << basename(fileName.c_str(fileTemp)) << " "; - if (message[0] != '\0') { + if (!message.empty()) { errorStream << "failed: " << message << std::endl; } else { errorStream << "failed." << std::endl; @@ -98,7 +98,7 @@ void wpi_assertEqual_common_impl(int valueA, int valueB, // If an error message was specified, include it // Build error string - if (message.size() > 0) { + if (!message.empty()) { error << "Assertion failed: \"" << message << "\", \"" << valueA << "\" " << equalityType << " \"" << valueB << "\" in " << funcName << "() in " << fileName << " at line " << lineNumber << "\n";