Remove obsolete c_str_safe utility

The old `c_str_safe` utility function allowed Zeek to operator on
`broker::data` and `broker::variant`. The former grants access to actual
`std::string` objects while the latter only provides access to fields
via `std::string_view`. Since the Zeek formatting functions need null
terminated strings, we need to copy the characters into a
null-terminated container first.

After removing support for `broker::data` and `broker::variant` from the
same code paths, we can drop `c_str_safe` and always do the copying
(since we are always dealing with `broker::variant` now).
This commit is contained in:
Dominik Charousset 2024-12-03 17:26:23 +01:00
parent 470f545b37
commit feeb06f7cf

View file

@ -42,18 +42,6 @@ broker::vector broker_vector_from(const broker::variant& arg) {
return std::move(broker::get<broker::vector>(tmp)); return std::move(broker::get<broker::vector>(tmp));
} }
// Converts a string_view into a string to make sure that we can safely call `.c_str()` on the result.
template<class View>
std::enable_if_t<std::is_same_v<std::decay_t<View>, std::string_view>, std::string> c_str_safe(View&& arg) {
return std::string{arg};
}
// Passes through a string without copying it (already safe to call `.c_str()` on it).
template<class String>
std::enable_if_t<std::is_same_v<std::decay_t<String>, std::string>, const std::string&> c_str_safe(String&& arg) {
return arg;
}
void print_escaped(std::string& buf, std::string_view str) { void print_escaped(std::string& buf, std::string_view str) {
buf.push_back('"'); buf.push_back('"');
for ( auto c : str ) { for ( auto c : str ) {
@ -712,7 +700,7 @@ bool Manager::PublishLogWrite(EnumVal* stream, EnumVal* writer, const string& pa
reporter->Error( reporter->Error(
"Failed to remotely log: log_topic func did not return" "Failed to remotely log: log_topic func did not return"
" a value for stream %s at path %s", " a value for stream %s at path %s",
stream_id, c_str_safe(path).c_str()); stream_id, std::string{path}.c_str());
return false; return false;
} }
@ -1180,7 +1168,7 @@ void Manager::ProcessMessage(std::string_view topic, broker::zeek::Event& ev) {
// Default to current network time, if the received event did not contain a timestamp. // Default to current network time, if the received event did not contain a timestamp.
ts = run_state::network_time; ts = run_state::network_time;
DBG_LOG(DBG_BROKER, "Process event: %s (%.6f) %s", c_str_safe(name).c_str(), ts, RenderMessage(args).c_str()); DBG_LOG(DBG_BROKER, "Process event: %s (%.6f) %s", std::string{name}.c_str(), ts, RenderMessage(args).c_str());
num_events_incoming_metric->Inc(); num_events_incoming_metric->Inc();
auto handler = event_registry->Lookup(name); auto handler = event_registry->Lookup(name);
@ -1194,7 +1182,7 @@ void Manager::ProcessMessage(std::string_view topic, broker::zeek::Event& ev) {
if ( strncmp(p.data(), topic.data(), p.size()) != 0 ) if ( strncmp(p.data(), topic.data(), p.size()) != 0 )
continue; continue;
DBG_LOG(DBG_BROKER, "Skip processing of forwarded event: %s %s", c_str_safe(name).c_str(), DBG_LOG(DBG_BROKER, "Skip processing of forwarded event: %s %s", std::string{name}.c_str(),
RenderMessage(args).c_str()); RenderMessage(args).c_str());
return; return;
} }
@ -1205,7 +1193,7 @@ void Manager::ProcessMessage(std::string_view topic, broker::zeek::Event& ev) {
reporter->Warning( reporter->Warning(
"got event message '%s' with invalid # of args," "got event message '%s' with invalid # of args,"
" got %zd, expected %zu", " got %zd, expected %zu",
c_str_safe(name).c_str(), args.size(), arg_types.size()); std::string{name}.c_str(), args.size(), arg_types.size());
return; return;
} }
@ -1240,7 +1228,7 @@ void Manager::ProcessMessage(std::string_view topic, broker::zeek::Event& ev) {
expected_type->GetName().c_str()); expected_type->GetName().c_str());
} }
reporter->Warning("failed to convert remote event '%s' arg #%zu, %s", c_str_safe(name).c_str(), i, reporter->Warning("failed to convert remote event '%s' arg #%zu, %s", std::string{name}.c_str(), i,
msg_addl.c_str()); msg_addl.c_str());
// If we got a vector and expected a function this is // If we got a vector and expected a function this is
@ -1334,7 +1322,7 @@ bool Manager::ProcessMessage(std::string_view, broker::zeek::LogWrite& lw) {
auto stream_id = detail::data_to_val(wrapped_stream_id, log_id_type); auto stream_id = detail::data_to_val(wrapped_stream_id, log_id_type);
if ( ! stream_id ) { if ( ! stream_id ) {
reporter->Warning("failed to unpack remote log stream id: %s", c_str_safe(stream_id_name).c_str()); reporter->Warning("failed to unpack remote log stream id: %s", std::string{stream_id_name}.c_str());
return false; return false;
} }
@ -1342,7 +1330,7 @@ bool Manager::ProcessMessage(std::string_view, broker::zeek::LogWrite& lw) {
auto wrapped_writer_id = broker::data{lw.writer_id()}; auto wrapped_writer_id = broker::data{lw.writer_id()};
auto writer_id = detail::data_to_val(wrapped_writer_id, writer_id_type); auto writer_id = detail::data_to_val(wrapped_writer_id, writer_id_type);
if ( ! writer_id ) { if ( ! writer_id ) {
reporter->Warning("failed to unpack remote log writer id for stream: %s", c_str_safe(stream_id_name).c_str()); reporter->Warning("failed to unpack remote log writer id for stream: %s", std::string{stream_id_name}.c_str());
return false; return false;
} }
@ -1358,7 +1346,7 @@ bool Manager::ProcessMessage(std::string_view, broker::zeek::LogWrite& lw) {
if ( ! success ) { if ( ! success ) {
reporter->Warning("failed to unserialize remote log num fields for stream: %s", reporter->Warning("failed to unserialize remote log num fields for stream: %s",
c_str_safe(stream_id_name).c_str()); std::string{stream_id_name}.c_str());
return false; return false;
} }
@ -1367,7 +1355,7 @@ bool Manager::ProcessMessage(std::string_view, broker::zeek::LogWrite& lw) {
for ( int i = 0; i < num_fields; ++i ) { for ( int i = 0; i < num_fields; ++i ) {
if ( ! rec[i].Read(&fmt) ) { if ( ! rec[i].Read(&fmt) ) {
reporter->Warning("failed to unserialize remote log field %d for stream: %s", i, reporter->Warning("failed to unserialize remote log field %d for stream: %s", i,
c_str_safe(stream_id_name).c_str()); std::string{stream_id_name}.c_str());
return false; return false;
} }
@ -1387,7 +1375,7 @@ bool Manager::ProcessMessage(std::string_view, broker::zeek::IdentifierUpdate& i
} }
num_ids_incoming_metric->Inc(); num_ids_incoming_metric->Inc();
auto id_name = c_str_safe(iu.id_name()); auto id_name = std::string{iu.id_name()};
auto id_value = iu.id_value().to_data(); auto id_value = iu.id_value().to_data();
const auto& id = zeek::detail::global_scope()->Find(id_name); const auto& id = zeek::detail::global_scope()->Find(id_name);