Skip to content

Commit be20b75

Browse files
committed
parser-gcc: fix incomplete file paths in UBSAN key events
UBSAN may only use the base name for the file path in its key event. However, the top of the backtrace, if present, always contains the whole path to this file.
1 parent 0e4db39 commit be20b75

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ include_directories(lib)
5757

5858
# link cslib.a and boost libraries
5959
link_libraries(cs
60+
${Boost_FILESYSTEM_LIBRARY}
6061
${Boost_PROGRAM_OPTIONS_LIBRARY}
6162
${Boost_REGEX_LIBRARY})
6263

@@ -67,13 +68,11 @@ add_executable(cshtml cshtml.cc)
6768
add_executable(cslinker cslinker.cc)
6869
add_executable(cssort cssort.cc)
6970
target_link_libraries(cshtml
70-
${Boost_FILESYSTEM_LIBRARY}
7171
${Boost_SYSTEM_LIBRARY})
7272

7373
# experimental
7474
add_executable(cstrans-df-run cstrans-df-run.cc)
7575
target_link_libraries(cstrans-df-run
76-
${Boost_FILESYSTEM_LIBRARY}
7776
${Boost_SYSTEM_LIBRARY})
7877

7978
# declare what 'make install' should install

src/lib/parser-gcc.cc

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "regex.hh"
2424

2525
#include <algorithm>
26+
#include <boost/filesystem.hpp>
2627

2728
namespace GccParserImpl {
2829

@@ -639,8 +640,32 @@ void GccPostProcessor::Private::transUbsan(Defect *pDef) const
639640

640641
// UBSAN always uses 'runtime error' for its key event
641642
DefEvent &keyEvt = pDef->events[pDef->keyEventIdx];
642-
if (keyEvt.event == "runtime error")
643-
pDef->checker = "UBSAN_WARNING";
643+
if (keyEvt.event != "runtime error")
644+
return;
645+
646+
pDef->checker = "UBSAN_WARNING";
647+
648+
// UBSAN may only use the base name for the file path in its key event.
649+
// However, the top of the backtrace, if present, always contains the
650+
// whole path to this file.
651+
if (!keyEvt.fileName.empty() && keyEvt.fileName.front() == '/')
652+
return;
653+
654+
using path = boost::filesystem::path;
655+
const auto &keyFileName = path(keyEvt.fileName).filename();
656+
657+
for (const auto &evt : pDef->events) {
658+
const auto &evtFileName = path(evt.fileName).filename();
659+
660+
// check that the basenames and line numbers match
661+
if (&keyEvt != &evt
662+
&& keyFileName == evtFileName
663+
&& keyEvt.line == evt.line)
664+
{
665+
keyEvt.fileName = evt.fileName;
666+
break;
667+
}
668+
}
644669
}
645670

646671
void GccPostProcessor::Private::polishGccAnal(Defect *pDef) const

tests/csgrep/0112-gcc-parser-ubsan-bt-stdout.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Error: UBSAN_WARNING:
2-
byteorder.h:83:9: runtime error: load of misaligned address 0x556e3e877805 for type 'const uint32_t', which requires 4 byte alignment
2+
/builddir/build/BUILD/rsync-3.2.3/byteorder.h:83:9: runtime error: load of misaligned address 0x556e3e877805 for type 'const uint32_t', which requires 4 byte alignment
33
0x556e3e877805: note: pointer points here
44
# b5 21 00 00 6c 00 00 07 ff 65 a0 b8 03 05 2f 74 65 78 74 0e 70 d6 f0 d2 4d 97 21 a4 81 00 00 a0
55
# ^
@@ -24,7 +24,7 @@ byteorder.h:83:9: runtime error: load of misaligned address 0x556e3e877805 for t
2424
/builddir/build/BUILD/rsync-3.2.3/rsync: note: _start() at 0x556e3dc0f324
2525

2626
Error: UBSAN_WARNING:
27-
test.c:2:23: runtime error: load of null pointer of type 'char'
27+
/home/lukas/csdiff/tests/csgrep/test.c:2:23: runtime error: load of null pointer of type 'char'
2828
/home/lukas/csdiff/tests/csgrep/test.c:2: note: main() at 0x401147
2929
/lib64/libc.so.6: note: __libc_start_call_main() at 0x7f7851249b49
3030
/lib64/libc.so.6: note: __libc_start_main_alias_2() at 0x7f7851249c0a

0 commit comments

Comments
 (0)