Fix unsafe and inefficient uses of copy_string

Add a new overload to `copy_string` that takes the input characters plus
size. The new overload avoids inefficient scanning of the input for the
null terminator in cases where we know the size beforehand. Furthermore,
this overload *must* be used when dealing with input character sequences
that may have no null terminator, e.g., when the input is from a
`std::string_view` object.
This commit is contained in:
Dominik Charousset 2023-11-02 17:37:11 +01:00
parent 4eb1b71d1b
commit cebb85b1e8
25 changed files with 85 additions and 56 deletions

View file

@ -157,7 +157,8 @@ int TraceState::LogTrace(const char* fmt, ...) {
}
if ( ! loc.filename ) {
loc.filename = util::copy_string("<no filename>");
static constexpr const char str[] = "<no filename>";
loc.filename = util::copy_string(str, std::size(str) - 1);
loc.last_line = 0;
}
@ -675,7 +676,8 @@ string get_context_description(const Stmt* stmt, const Frame* frame) {
if ( stmt )
loc = *stmt->GetLocationInfo();
else {
loc.filename = util::copy_string("<no filename>");
static constexpr const char str[] = "<no filename>";
loc.filename = util::copy_string(str, std::size(str) - 1);
loc.last_line = 0;
}