From feeb06f7cfeeeccebad1c51a2d9b28c21d038861 Mon Sep 17 00:00:00 2001 From: Dominik Charousset Date: Tue, 3 Dec 2024 17:26:23 +0100 Subject: [PATCH] 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). --- src/broker/Manager.cc | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/broker/Manager.cc b/src/broker/Manager.cc index 26c19d04d7..6e5dd26cd7 100644 --- a/src/broker/Manager.cc +++ b/src/broker/Manager.cc @@ -42,18 +42,6 @@ broker::vector broker_vector_from(const broker::variant& arg) { return std::move(broker::get(tmp)); } -// Converts a string_view into a string to make sure that we can safely call `.c_str()` on the result. -template -std::enable_if_t, 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 -std::enable_if_t, std::string>, const std::string&> c_str_safe(String&& arg) { - return arg; -} - void print_escaped(std::string& buf, std::string_view str) { buf.push_back('"'); for ( auto c : str ) { @@ -712,7 +700,7 @@ bool Manager::PublishLogWrite(EnumVal* stream, EnumVal* writer, const string& pa reporter->Error( "Failed to remotely log: log_topic func did not return" " 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; } @@ -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. 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(); 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 ) 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()); return; } @@ -1205,7 +1193,7 @@ void Manager::ProcessMessage(std::string_view topic, broker::zeek::Event& ev) { reporter->Warning( "got event message '%s' with invalid # of args," " 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; } @@ -1240,7 +1228,7 @@ void Manager::ProcessMessage(std::string_view topic, broker::zeek::Event& ev) { 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()); // 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); 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; } @@ -1342,7 +1330,7 @@ bool Manager::ProcessMessage(std::string_view, broker::zeek::LogWrite& lw) { auto wrapped_writer_id = broker::data{lw.writer_id()}; auto writer_id = detail::data_to_val(wrapped_writer_id, writer_id_type); 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; } @@ -1358,7 +1346,7 @@ bool Manager::ProcessMessage(std::string_view, broker::zeek::LogWrite& lw) { if ( ! success ) { 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; } @@ -1367,7 +1355,7 @@ bool Manager::ProcessMessage(std::string_view, broker::zeek::LogWrite& lw) { for ( int i = 0; i < num_fields; ++i ) { if ( ! rec[i].Read(&fmt) ) { 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; } @@ -1387,7 +1375,7 @@ bool Manager::ProcessMessage(std::string_view, broker::zeek::IdentifierUpdate& i } 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(); const auto& id = zeek::detail::global_scope()->Find(id_name);