[llvm-profdata] Fix bug llvm-profdata crashes when reading a text sample profile...
authorWilliam Huang <williamjhuang@google.com>
Mon, 6 Feb 2023 22:26:10 +0000 (22:26 +0000)
committerWilliam Huang <williamjhuang@google.com>
Mon, 6 Feb 2023 22:29:20 +0000 (22:29 +0000)
Text editors can introduce spaces aligning the previous line's indentation. This crashes llvm-profdata. Added check to handle this case.

Reviewed By: snehasish

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

llvm/lib/ProfileData/SampleProfReader.cpp
llvm/test/tools/llvm-profdata/Inputs/sample-empty-lines.proftext [new file with mode: 0644]
llvm/test/tools/llvm-profdata/sample-empty-lines.test [new file with mode: 0644]

index 7fa3d5c..86aa0f1 100644 (file)
@@ -328,7 +328,8 @@ std::error_code SampleProfileReaderText::readImpl() {
   ProfileIsFS = ProfileIsFSDisciminator;
   FunctionSamples::ProfileIsFS = ProfileIsFS;
   for (; !LineIt.is_at_eof(); ++LineIt) {
-    if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
+    size_t pos = LineIt->find_first_not_of(' ');
+    if (pos == LineIt->npos || (*LineIt)[pos] == '#')
       continue;
     // Read the header of each function.
     //
diff --git a/llvm/test/tools/llvm-profdata/Inputs/sample-empty-lines.proftext b/llvm/test/tools/llvm-profdata/Inputs/sample-empty-lines.proftext
new file mode 100644 (file)
index 0000000..800876f
--- /dev/null
@@ -0,0 +1,9 @@
+main:10:1
+ 2: 3
+
+ 3: inline1:5
+  
+  4: 1
+
diff --git a/llvm/test/tools/llvm-profdata/sample-empty-lines.test b/llvm/test/tools/llvm-profdata/sample-empty-lines.test
new file mode 100644 (file)
index 0000000..1793a4e
--- /dev/null
@@ -0,0 +1,8 @@
+Test llvm-profdata merge can handle empty line with spaces in text format sample profile.
+
+RUN: llvm-profdata merge --sample --text %p/Inputs/sample-empty-lines.proftext | FileCheck %s
+CHECK: main:10:1
+CHECK-NEXT: 2: 3
+CHECK-NEXT: 3: inline1:5
+CHECK-NEXT: 4: 1
+