[llvm] 84719e2 - [llvm-profdata] Fix bug llvm-profdata crashes when reading a text sample profile with an empty line with spaces.

William Huang via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 6 14:30:45 PST 2023


Author: William Huang
Date: 2023-02-06T22:29:20Z
New Revision: 84719e2f038751b0c76d668413934dd54f720e84

URL: https://github.com/llvm/llvm-project/commit/84719e2f038751b0c76d668413934dd54f720e84
DIFF: https://github.com/llvm/llvm-project/commit/84719e2f038751b0c76d668413934dd54f720e84.diff

LOG: [llvm-profdata] Fix bug llvm-profdata crashes when reading a text sample profile with an empty line with spaces.

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

Added: 
    llvm/test/tools/llvm-profdata/Inputs/sample-empty-lines.proftext
    llvm/test/tools/llvm-profdata/sample-empty-lines.test

Modified: 
    llvm/lib/ProfileData/SampleProfReader.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp
index 7fa3d5c535c25..86aa0f1d438c1 100644
--- a/llvm/lib/ProfileData/SampleProfReader.cpp
+++ b/llvm/lib/ProfileData/SampleProfReader.cpp
@@ -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
index 0000000000000..800876f65e8cd
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/Inputs/sample-empty-lines.proftext
@@ -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
index 0000000000000..1793a4eab6a88
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/sample-empty-lines.test
@@ -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
+


        


More information about the llvm-commits mailing list