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

William Junda Huang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 5 23:13:18 PST 2023


huangjd created this revision.
huangjd added reviewers: davidxl, xur, ellis, snehasish, gulfem.
Herald added subscribers: wenlei, hiraditya.
Herald added a project: All.
huangjd requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Text editors can introduce spaces aligning the previous line's indentation. This crashes llvm-profdata. Added check to handle this case.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143369

Files:
  llvm/lib/ProfileData/SampleProfReader.cpp
  llvm/test/tools/llvm-profdata/Inputs/sample-empty-lines.proftext
  llvm/test/tools/llvm-profdata/sample-empty-lines.test


Index: llvm/test/tools/llvm-profdata/sample-empty-lines.test
===================================================================
--- /dev/null
+++ 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
+
Index: llvm/test/tools/llvm-profdata/Inputs/sample-empty-lines.proftext
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-profdata/Inputs/sample-empty-lines.proftext
@@ -0,0 +1,9 @@
+main:10:1
+ 2: 3
+ 
+
+ 3: inline1:5
+  
+  4: 1
+ 
+
Index: llvm/lib/ProfileData/SampleProfReader.cpp
===================================================================
--- llvm/lib/ProfileData/SampleProfReader.cpp
+++ llvm/lib/ProfileData/SampleProfReader.cpp
@@ -328,7 +328,8 @@
   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.
     //


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143369.495005.patch
Type: text/x-patch
Size: 1375 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230206/2dceecd3/attachment.bin>


More information about the llvm-commits mailing list