Re-enabled address sanitizer.
continuous-integration/drone/push Build is passing Details

Found out what was wrong: I fed boost::regex_search() the pointer to a substring
that was created in-place. match[2] was a pointer to a substring inside that.

The problem was, that match was declared outside of the if-block. So after the
if-block match[2] would point to a now freed memory address. It didn't have any
effects because I didn't use match afterwards.

I rewrote the whole thing with iterators. Slightly less readable, slightly
better performance (probably).
This commit is contained in:
tastytea 2021-06-05 17:45:07 +02:00
parent bdf9a86651
commit 99e1cd8e98
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 13 additions and 10 deletions

View File

@ -25,7 +25,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"
"-Wformat=2"
"-ftrapv"
"-fsanitize=undefined"
# "-fsanitize=address"
"-fsanitize=address"
"-Og"
"-fno-omit-frame-pointer")
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
@ -47,8 +47,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"
list(APPEND tmp_LDFLAGS
"-fsanitize=undefined"
# "-fsanitize=address"
)
"-fsanitize=address")
# add_link_options was introduced in version 3.13.
if(${CMAKE_VERSION} VERSION_LESS 3.13)
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${tmp_LDFLAGS}")

View File

@ -149,12 +149,16 @@ void cleanup_text(string &text)
else if (text.substr(pos, 6) == "<span ")
{
auto endpos{text.find('>')};
boost::match_results<const char *> match;
if (boost::regex_search(text.substr(pos, endpos - pos).data(),
match, re_pagebreak))
boost::match_results<string::const_iterator> match;
using it_size_t = string::const_iterator::difference_type;
string::const_iterator begin{text.begin()
+ static_cast<it_size_t>(pos)};
string::const_iterator end{text.end()
+ static_cast<it_size_t>(endpos)};
if (boost::regex_search(begin, end, match, re_pagebreak))
{
// FIXME: -fsanitize=address is complaining about this. ↓ 🤷
// Could not reproduce it.
replacement = format("<PAGE {0:s}>", match[2].str());
}
}
@ -240,9 +244,9 @@ match_context context(const boost::match_results<string::const_iterator> &match,
words -= 1;
}
const std::string before_reversed(rbegin_before, pos_before);
const string before_reversed(rbegin_before, pos_before);
string before(before_reversed.rbegin(), before_reversed.rend());
std::string after(begin_after, pos_after);
string after(begin_after, pos_after);
while (helpers::is_whitespace(*before.begin()))
{
before.erase(0, 1);