[llvm] r345137 - Fix llvm-strings crash for negative char values

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 24 06:16:16 PDT 2018


Author: jhenderson
Date: Wed Oct 24 06:16:16 2018
New Revision: 345137

URL: http://llvm.org/viewvc/llvm-project?rev=345137&view=rev
Log:
Fix llvm-strings crash for negative char values

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

Added:
    llvm/trunk/test/tools/llvm-strings/negative-char.test
Modified:
    llvm/trunk/tools/llvm-strings/llvm-strings.cpp

Added: llvm/trunk/test/tools/llvm-strings/negative-char.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-strings/negative-char.test?rev=345137&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-strings/negative-char.test (added)
+++ llvm/trunk/test/tools/llvm-strings/negative-char.test Wed Oct 24 06:16:16 2018
@@ -0,0 +1,3 @@
+# RUN: echo -e "z\0\x80\0a\0" | llvm-strings --bytes 1 - | FileCheck %s
+# CHECK: z{{$}}
+# CHECK-NEXT: {{^}} a

Modified: llvm/trunk/tools/llvm-strings/llvm-strings.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-strings/llvm-strings.cpp?rev=345137&r1=345136&r2=345137&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-strings/llvm-strings.cpp (original)
+++ llvm/trunk/tools/llvm-strings/llvm-strings.cpp Wed Oct 24 06:16:16 2018
@@ -80,7 +80,7 @@ static void strings(raw_ostream &OS, Str
   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) {




More information about the llvm-commits mailing list