OSDN Git Service

Fix llvm-strings crash for negative char values
authorJames Henderson <jh7370@my.bristol.ac.uk>
Wed, 24 Oct 2018 13:16:16 +0000 (13:16 +0000)
committerJames Henderson <jh7370@my.bristol.ac.uk>
Wed, 24 Oct 2018 13:16:16 +0000 (13:16 +0000)
On Windows at least, llvm-strings was crashing if it encountered bytes
that mapped to negative chars, as it was passing these into
std::isgraph and std::isblank functions, resulting in undefined
behaviour. On debug builds using MSVC, these functions verfiy that the
value passed in is representable as an unsigned char. Since the char is
promoted to an int, a value greater than 127 would turn into a negative
integer value, and fail the check. Using the llvm::isPrint function is
sufficient to solve the issue.

Reviewed by: ruiu, mstorsjo

Differential Revision: https://reviews.llvm.org/D53509

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@345137 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-strings/negative-char.test [new file with mode: 0644]
tools/llvm-strings/llvm-strings.cpp

diff --git a/test/tools/llvm-strings/negative-char.test b/test/tools/llvm-strings/negative-char.test
new file mode 100644 (file)
index 0000000..331dde4
--- /dev/null
@@ -0,0 +1,3 @@
+# RUN: echo -e "z\0\x80\0a\0" | llvm-strings --bytes 1 - | FileCheck %s
+# CHECK: z{{$}}
+# CHECK-NEXT: {{^}} a
index 8e2d213..c355caf 100644 (file)
@@ -80,7 +80,7 @@ static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
   const char *B = Contents.begin();
   const char *P = nullptr, *E = nullptr, *S = nullptr;
   for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
-    if (std::isgraph(*P) || std::isblank(*P)) {
+    if (isPrint(*P) || *P == '\t') {
       if (S == nullptr)
         S = P;
     } else if (S) {