Fix clang-tidy bugprone-suspicious-stringview-data-usage warnings

This commit is contained in:
Tim Wojtulewicz 2025-04-17 13:22:03 -07:00
parent 02589c349a
commit 975f24bde6
13 changed files with 28 additions and 19 deletions

View file

@ -10,4 +10,5 @@ Checks: [-*,
bugprone-misplaced-widening-cast, bugprone-misplaced-widening-cast,
bugprone-parent-virtual-call, bugprone-parent-virtual-call,
bugprone-string-literal-with-embedded-nul, bugprone-string-literal-with-embedded-nul,
bugprone-suspicious-stringview-data-usage,
] ]

View file

@ -915,7 +915,7 @@ zeek::RecordValPtr make_backtrace_element(std::string_view name, const VectorVal
static auto line_location_idx = elem_type->FieldOffset("line_location"); static auto line_location_idx = elem_type->FieldOffset("line_location");
auto elem = make_intrusive<RecordVal>(elem_type); auto elem = make_intrusive<RecordVal>(elem_type);
elem->Assign(function_name_idx, name.data()); elem->Assign(function_name_idx, name);
elem->Assign(function_args_idx, args); elem->Assign(function_args_idx, args);
if ( loc ) { if ( loc ) {

View file

@ -118,7 +118,8 @@ bool MMDB::EnsureLoaded() {
if ( ! res && ! reported_error ) { if ( ! res && ! reported_error ) {
reported_error = true; reported_error = true;
zeek::emit_builtin_error(zeek::util::fmt("Failed to open %s", Description().data())); zeek::emit_builtin_error(
zeek::util::fmt("Failed to open %.*s", static_cast<int>(Description().size()), Description().data()));
} }
return res; return res;

View file

@ -467,7 +467,7 @@ void Reporter::Deprecation(std::string_view msg, const detail::Location* loc1, c
if ( loc1 || loc2 ) if ( loc1 || loc2 )
PushLocation(loc1, loc2); PushLocation(loc1, loc2);
Warning("%s", msg.data()); Warning("%.*s", static_cast<int>(msg.size()), msg.data());
if ( loc1 || loc2 ) if ( loc1 || loc2 )
PopLocation(); PopLocation();

View file

@ -981,8 +981,8 @@ static zeek::expected<ValPtr, std::string> BuildVal(const rapidjson::Value& j, c
else if ( unit == "usec" || unit == "usecs" ) else if ( unit == "usec" || unit == "usecs" )
interval_secs += (value * Microseconds); interval_secs += (value * Microseconds);
else else
return zeek::unexpected<std::string>( return zeek::unexpected<std::string>(util::fmt("wrong interval format, invalid unit type %.*s",
util::fmt("wrong interval format, invalid unit type %s", unit.data())); static_cast<int>(unit.size()), unit.data()));
} }
return make_intrusive<IntervalVal>(interval_secs, Seconds); return make_intrusive<IntervalVal>(interval_secs, Seconds);

View file

@ -1186,6 +1186,7 @@ public:
} }
void Assign(int field, const char* new_val) { Assign(field, new StringVal(new_val)); } void Assign(int field, const char* new_val) { Assign(field, new StringVal(new_val)); }
void Assign(int field, const std::string& new_val) { Assign(field, new StringVal(new_val)); } void Assign(int field, const std::string& new_val) { Assign(field, new StringVal(new_val)); }
void Assign(int field, std::string_view new_val) { Assign(field, new StringVal(new_val)); }
void Assign(int field, String* new_val) { Assign(field, new StringVal(new_val)); } void Assign(int field, String* new_val) { Assign(field, new StringVal(new_val)); }
/** /**

View file

@ -4,7 +4,7 @@ type ftp_port: record;
%%{ %%{
#include "zeek/Reporter.h" #include "zeek/Reporter.h"
static zeek::RecordValPtr parse_port(std::string_view line) static zeek::RecordValPtr parse_port(const std::string& line)
{ {
auto r = zeek::make_intrusive<zeek::RecordVal>(zeek::BifType::Record::ftp_port); auto r = zeek::make_intrusive<zeek::RecordVal>(zeek::BifType::Record::ftp_port);
@ -13,7 +13,7 @@ static zeek::RecordValPtr parse_port(std::string_view line)
uint32_t addr = 0; uint32_t addr = 0;
int32_t bytes[6]; int32_t bytes[6];
if ( line.size() >= 11 && sscanf(line.data(), if ( line.size() >= 11 && sscanf(line.c_str(),
"%" SCNd32 ",%" SCNd32 ",%" SCNd32 ",%" SCNd32 ",%" SCNd32 ",%" SCNd32, "%" SCNd32 ",%" SCNd32 ",%" SCNd32 ",%" SCNd32 ",%" SCNd32 ",%" SCNd32,
&bytes[0], &bytes[1], &bytes[2], &bytes[0], &bytes[1], &bytes[2],
&bytes[3], &bytes[4], &bytes[5]) == 6 ) &bytes[3], &bytes[4], &bytes[5]) == 6 )
@ -125,7 +125,7 @@ static zeek::RecordValPtr parse_eftp(const char* line)
## .. zeek:see:: parse_eftp_port parse_ftp_pasv parse_ftp_epsv fmt_ftp_port ## .. zeek:see:: parse_eftp_port parse_ftp_pasv parse_ftp_epsv fmt_ftp_port
function parse_ftp_port%(s: string%): ftp_port function parse_ftp_port%(s: string%): ftp_port
%{ %{
return parse_port(s->ToStdStringView()); return parse_port(s->ToStdString());
%} %}
## Converts a string representation of the FTP EPRT command (see :rfc:`2428`) ## Converts a string representation of the FTP EPRT command (see :rfc:`2428`)
@ -181,7 +181,7 @@ function parse_ftp_pasv%(str: string%): ftp_port
if ( ! line || ( line - s ) > str->Len() ) if ( ! line || ( line - s ) > str->Len() )
return parse_port(""); return parse_port("");
else else
return parse_port(std::string_view{line}); return parse_port(std::string{line});
%} %}
## Converts the result of the FTP EPSV command (see :rfc:`2428`) to an ## Converts the result of the FTP EPSV command (see :rfc:`2428`) to an

View file

@ -1590,7 +1590,7 @@ void Manager::ProcessMessage(std::string_view topic, broker::zeek::Event& ev) {
if ( p.size() > topic.size() ) if ( p.size() > topic.size() )
continue; continue;
if ( strncmp(p.data(), topic.data(), p.size()) != 0 ) if ( strncmp(p.data(), topic.data(), p.size()) != 0 ) // NOLINT(bugprone-suspicious-stringview-data-usage)
continue; continue;
DBG_LOG(DBG_BROKER, "Skip processing of forwarded event: %s %s", std::string{name}.c_str(), DBG_LOG(DBG_BROKER, "Skip processing of forwarded event: %s %s", std::string{name}.c_str(),

View file

@ -38,7 +38,7 @@ void Manager::WakeupHandler::Ping(std::string_view where) {
// Calling DBG_LOG calls fprintf, which isn't safe to call in a signal // Calling DBG_LOG calls fprintf, which isn't safe to call in a signal
// handler. // handler.
if ( signal_val != 0 ) if ( signal_val != 0 )
DBG_LOG(DBG_MAINLOOP, "Pinging WakeupHandler from %s", where.data()); DBG_LOG(DBG_MAINLOOP, "Pinging WakeupHandler from %.*s", static_cast<int>(where.size()), where.data());
flare.Fire(true); flare.Fire(true);
} }

View file

@ -246,7 +246,7 @@ OperationResult Redis::DoOpen(OpenResultCallback* cb, RecordValPtr options) {
StringValPtr host = backend_options->GetField<StringVal>("server_host"); StringValPtr host = backend_options->GetField<StringVal>("server_host");
if ( host ) { if ( host ) {
PortValPtr port = backend_options->GetField<PortVal>("server_port"); PortValPtr port = backend_options->GetField<PortVal>("server_port");
server_addr = util::fmt("%s:%d", host->ToStdStringView().data(), port->Port()); server_addr = util::fmt("%s:%d", host->ToStdString().c_str(), port->Port());
REDIS_OPTIONS_SET_TCP(&opt, host->ToStdStringView().data(), port->Port()); REDIS_OPTIONS_SET_TCP(&opt, host->ToStdStringView().data(), port->Port());
} }
else { else {
@ -695,10 +695,12 @@ OperationResult Redis::ParseReplyError(std::string_view op_str, std::string_view
if ( async_ctx->err == REDIS_ERR_TIMEOUT ) if ( async_ctx->err == REDIS_ERR_TIMEOUT )
return {ReturnCode::TIMEOUT}; return {ReturnCode::TIMEOUT};
else if ( async_ctx->err == REDIS_ERR_IO ) else if ( async_ctx->err == REDIS_ERR_IO )
return {ReturnCode::OPERATION_FAILED, util::fmt("%s operation IO error: %s", op_str.data(), strerror(errno))}; return {ReturnCode::OPERATION_FAILED, util::fmt("%.*s operation IO error: %s", static_cast<int>(op_str.size()),
op_str.data(), strerror(errno))};
else else
return {ReturnCode::OPERATION_FAILED, return {ReturnCode::OPERATION_FAILED,
util::fmt("%s operation failed: %s", op_str.data(), reply_err_str.data())}; util::fmt("%.*s operation failed: %.*s", static_cast<int>(op_str.size()), op_str.data(),
static_cast<int>(reply_err_str.size()), reply_err_str.data())};
} }
void Redis::DoPoll() { void Redis::DoPoll() {

View file

@ -35,8 +35,9 @@ zeek::expected<ValPtr, std::string> JSON::Unserialize(byte_buffer_span buf, Type
std::string_view version = std::string_view(text).substr(0, semicolon); std::string_view version = std::string_view(text).substr(0, semicolon);
if ( version != versioned_name ) if ( version != versioned_name )
return zeek::unexpected<std::string>( return zeek::unexpected<std::string>(util::fmt("Version doesn't match: %.*s vs %s",
util::fmt("Version doesn't match: %s vs %s", version.data(), versioned_name.c_str())); static_cast<int>(version.size()), version.data(),
versioned_name.c_str()));
return zeek::detail::ValFromJSON(text.substr(semicolon + 1), type, Func::nil); return zeek::detail::ValFromJSON(text.substr(semicolon + 1), type, Func::nil);
} }

View file

@ -301,7 +301,8 @@ ValPtr Manager::CollectMetrics(std::string_view prefix_pattern, std::string_view
// Due to the name containing the full information about a metric including a potential unit add an // Due to the name containing the full information about a metric including a potential unit add an
// asterisk to the end of the full pattern so matches work correctly. // asterisk to the end of the full pattern so matches work correctly.
std::string full_pattern = util::fmt("%s_%s", prefix_pattern.data(), name_pattern.data()); std::string full_pattern = util::fmt("%.*s_%.*s", static_cast<int>(prefix_pattern.size()), prefix_pattern.data(),
static_cast<int>(name_pattern.size()), name_pattern.data());
if ( full_pattern[full_pattern.size() - 1] != '*' ) if ( full_pattern[full_pattern.size() - 1] != '*' )
full_pattern.append("*"); full_pattern.append("*");
@ -380,7 +381,8 @@ ValPtr Manager::CollectHistogramMetrics(std::string_view prefix_pattern, std::st
// Due to the name containing the full information about a metric including a potential unit add an // Due to the name containing the full information about a metric including a potential unit add an
// asterisk to the end of the full pattern so matches work correctly. // asterisk to the end of the full pattern so matches work correctly.
std::string full_pattern = util::fmt("%s_%s", prefix_pattern.data(), name_pattern.data()); std::string full_pattern = util::fmt("%.*s_%.*s", static_cast<int>(prefix_pattern.size()), prefix_pattern.data(),
static_cast<int>(name_pattern.size()), name_pattern.data());
if ( full_pattern[full_pattern.size() - 1] != '*' ) if ( full_pattern[full_pattern.size() - 1] != '*' )
full_pattern.append("*"); full_pattern.append("*");

View file

@ -16,7 +16,8 @@ std::string BuildFullPrometheusName(std::string_view prefix, std::string_view na
if ( prefix.empty() || name.empty() ) if ( prefix.empty() || name.empty() )
reporter->FatalError("Telemetry metric families must have a non-zero-length prefix and name"); reporter->FatalError("Telemetry metric families must have a non-zero-length prefix and name");
std::string fn = util::fmt("%s_%s", prefix.data(), name.data()); std::string fn = util::fmt("%.*s_%.*s", static_cast<int>(prefix.size()), prefix.data(),
static_cast<int>(name.size()), name.data());
std::for_each(fn.begin(), fn.end(), [](char& c) { std::for_each(fn.begin(), fn.end(), [](char& c) {
if ( ! std::isalnum(c) ) if ( ! std::isalnum(c) )
c = '_'; c = '_';