<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60541>60541</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Assertion LocInfo.second > SM.getExpansionColumnNumber(Loc) && "position in file is before column number?"Failed for Clang Static Analyzer in Sarif Mode
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Scarlet-Climax
</td>
</tr>
</table>
<pre>
### Description
For clang version below 15.0.7, when clang enables SARIF output mode and the warning appears at the first line, the assertion at line 161 of SarifDiagnostics.cpp would fail:
https://github.com/llvm/llvm-project/blob/8dfdcc7b7bf66834a761bd8de445840ef68e4d1a/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp#L156-L162
### HOW TO REPRODUCE
Code where the bug locates at the first line, will trigger this assertion. The following log shows the a mini code example and crash report.
```zsh
> cat a.c
int foo() { return 0 / 0; }%
> ~/llvm-project/build/bin/clang --version
clang version 15.0.7 (https://github.com/llvm/llvm-project.git 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/xumuchen/llvm-project/build/bin
> ~/llvm-project/build/bin/clang --analyze --analyzer-output sarif a.c
clang: /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp:161: unsigned int adjustColumnPos(const clang::SourceManager&, clang::SourceLocation, unsigned int): Assertion `LocInfo.second > SM.getExpansionColumnNumber(Loc) && "position in file is before column number?"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /home/xumuchen/llvm-project/build/bin/clang --analyze --analyzer-output sarif a.c
1. <eof> parser at end of file
#0 0x00000000040b3021 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/xumuchen/llvm-project/llvm/lib/Support/Unix/Signals.inc:565:22
#1 0x00000000040b30e4 PrintStackTraceSignalHandler(void*) /home/xumuchen/llvm-project/llvm/lib/Support/Unix/Signals.inc:632:1
#2 0x00000000040b0f64 llvm::sys::RunSignalHandlers() /home/xumuchen/llvm-project/llvm/lib/Support/Signals.cpp:103:20
#3 0x00000000040b2967 llvm::sys::CleanupOnSignal(unsigned long) /home/xumuchen/llvm-project/llvm/lib/Support/Unix/Signals.inc:361:31
#4 0x0000000003fd7ffb (anonymous namespace)::CrashRecoveryContextImpl::HandleCrash(int, unsigned long) /home/xumuchen/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:76:5
#5 0x0000000003fd84b1 CrashRecoverySignalHandler(int) /home/xumuchen/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:390:1
#6 0x00007ff7f043ea30 __restore_rt (/lib64/libc.so.6+0x3ea30)
#7 0x00007ff7f048ec0c __pthread_kill_implementation (/lib64/libc.so.6+0x8ec0c)
#8 0x00007ff7f043e986 gsignal (/lib64/libc.so.6+0x3e986)
#9 0x00007ff7f04287f4 abort (/lib64/libc.so.6+0x287f4)
#10 0x00007ff7f042871b _nl_load_domain.cold (/lib64/libc.so.6+0x2871b)
#11 0x00007ff7f0437536 (/lib64/libc.so.6+0x37536)
#12 0x00000000075614ab adjustColumnPos(clang::SourceManager const&, clang::SourceLocation, unsigned int) /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp:164:53
#13 0x000000000756169f createTextRegion(clang::LangOptions const&, clang::SourceRange, clang::SourceManager const&) /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp:186:3
#14 0x0000000007561a2a createPhysicalLocation(clang::LangOptions const&, clang::SourceRange, clang::FileEntry const&, clang::SourceManager const&, llvm::json::Array&) /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp:204:50
#15 0x0000000007562136 createThreadFlow(clang::LangOptions const&, clang::ento::PathPieces const&, llvm::json::Array&) /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp:264:23
#16 0x0000000007562332 createCodeFlow(clang::LangOptions const&, clang::ento::PathPieces const&, llvm::json::Array&) /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp:278:29
#17 0x0000000007562602 createResult(clang::LangOptions const&, clang::ento::PathDiagnostic const&, llvm::json::Array&, llvm::StringMap<unsigned int, llvm::MallocAllocator> const&) /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp:293:27
#18 0x000000000756c4ea createRun(clang::LangOptions const&, std::vector<clang::ento::PathDiagnostic const*, std::allocator<clang::ento::PathDiagnostic const*>>&) /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp:373:22
#19 0x000000000756c8cf (anonymous namespace)::SarifDiagnostics::FlushDiagnosticsImpl(std::vector<clang::ento::PathDiagnostic const*, std::allocator<clang::ento::PathDiagnostic const*>>&, clang::ento::PathDiagnosticConsumer::FilesMade*) /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp:398:22
#20 0x00000000088b34ab clang::ento::PathDiagnosticConsumer::FlushDiagnostics(clang::ento::PathDiagnosticConsumer::FilesMade*) /home/xumuchen/llvm-project/clang/lib/Analysis/PathDiagnostic.cpp:429:23
#21 0x00000000073bc300 clang::ento::AnalysisManager::FlushDiagnostics() /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp:63:18
#22 0x00000000073bc1be clang::ento::AnalysisManager::~AnalysisManager() /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp:53:65
#23 0x00000000073bc27c clang::ento::AnalysisManager::~AnalysisManager() /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp:57:1
#24 0x0000000006b078ea std::default_delete<clang::ento::AnalysisManager>::operator()(clang::ento::AnalysisManager*) const /usr/include/c++/12/bits/unique_ptr.h:96:7
#25 0x0000000006b07c3b std::__uniq_ptr_impl<clang::ento::AnalysisManager, std::default_delete<clang::ento::AnalysisManager>>::reset(clang::ento::AnalysisManager*) /usr/include/c++/12/bits/unique_ptr.h:204:7
#26 0x0000000006afc2b1 std::unique_ptr<clang::ento::AnalysisManager, std::default_delete<clang::ento::AnalysisManager>>::reset(clang::ento::AnalysisManager*) /usr/include/c++/12/bits/unique_ptr.h:502:7
#27 0x0000000006a8a510 (anonymous namespace)::AnalysisConsumer::HandleTranslationUnit(clang::ASTContext&)::'lambda'()::operator()() const /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:591:51
#28 0x0000000006a8c5da llvm::detail::scope_exit<(anonymous namespace)::AnalysisConsumer::HandleTranslationUnit(clang::ASTContext&)::'lambda'()>::~scope_exit() /home/xumuchen/llvm-project/llvm/include/llvm/ADT/ScopeExit.h:47:3
#29 0x0000000006a8a813 (anonymous namespace)::AnalysisConsumer::HandleTranslationUnit(clang::ASTContext&) /home/xumuchen/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:623:1
#30 0x000000000762fe60 clang::ParseAST(clang::Sema&, bool, bool) /home/xumuchen/llvm-project/clang/lib/Parse/ParseAST.cpp:205:14
#31 0x0000000005009729 clang::ASTFrontendAction::ExecuteAction() /home/xumuchen/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1144:11
#32 0x000000000500907f clang::FrontendAction::Execute() /home/xumuchen/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1041:38
#33 0x0000000004f47b46 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /home/xumuchen/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1035:42
#34 0x000000000519add8 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) /home/xumuchen/llvm-project/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:266:38
#35 0x0000000000c82b23 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) /home/xumuchen/llvm-project/clang/tools/driver/cc1_main.cpp:250:40
#36 0x0000000000c7618a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) /home/xumuchen/llvm-project/clang/tools/driver/driver.cpp:317:20
#37 0x0000000004e12c35 clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()::operator()() const /home/xumuchen/llvm-project/clang/lib/Driver/Job.cpp:407:32
#38 0x0000000004e1319a void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) /home/xumuchen/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:46:40
#39 0x0000000002d75e12 llvm::function_ref<void ()>::operator()() const /home/xumuchen/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:68:62
#40 0x0000000003fd8679 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) /home/xumuchen/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:426:10
#41 0x0000000004e12e20 clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const /home/xumuchen/llvm-project/clang/lib/Driver/Job.cpp:407:7
#42 0x0000000004dba19b clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const /home/xumuchen/llvm-project/clang/lib/Driver/Compilation.cpp:200:22
#43 0x0000000004dba4c7 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const /home/xumuchen/llvm-project/clang/lib/Driver/Compilation.cpp:254:62
#44 0x0000000004dcae92 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) /home/xumuchen/llvm-project/clang/lib/Driver/Driver.cpp:1738:28
#45 0x0000000000c7761c clang_main(int, char**) /home/xumuchen/llvm-project/clang/tools/driver/driver.cpp:513:39
#46 0x0000000000c9f971 main /home/xumuchen/llvm-project/build/tools/clang/tools/driver/clang-driver.cpp:11:63
#47 0x00007ff7f0429510 __libc_start_call_main (/lib64/libc.so.6+0x29510)
#48 0x00007ff7f04295c9 __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x295c9)
#49 0x0000000000c74a95 _start (/home/xumuchen/llvm-project/build/bin/clang+0xc74a95)
clang-15: error: clang frontend command failed with exit code 134 (use -v to see invocation)
clang version 15.0.7 (https://github.com/llvm/llvm-project.git 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/xumuchen/llvm-project/build/bin
clang-15: note: diagnostic msg:
********************
PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang-15: note: diagnostic msg: /tmp/a-0ac271.c
clang-15: note: diagnostic msg: /tmp/a-0ac271.sh
clang-15: note: diagnostic msg:
********************
```
### PERSONAL DISCUSSION
I guess `LocInfo` is 0 based, but column number is 1 based. Comparing them requires some offset handling.
So should it be something like:
```cpp
assert(LocInfo.second + 1 >= SM.getExpansionColumnNumber(Loc) &&
"position in file is before column number?");
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsW1tz2ziy_jX0C8oqEuD1wQ-yLM14yhm7LOXsowoEQQkTCNACYCzvw_z2UwBJ82LZiZSMz57dSaksBQQb_X3daDRuWGu2EZReedG1F91c4MpspbpaEqw4NZczznb4cJHL4vnKg6j-gBuqiWJ7w6Tw_BvPn9Z_F1IBwrHYgK9UaSYFyCmXTyCIJv4k8eAMPG2paKpQgXNONVhOH28XQFZmXxm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