diff --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp --- a/llvm/lib/ProfileData/SampleProfReader.cpp +++ b/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. // 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 --- /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 --- /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 +