Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/quick-start/src/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ auto sum(spider::TaskContext& /*context*/, int x, int y) -> int {
}

// Register the task with Spider
// NOLINTNEXTLINE(cert-err58-cpp)
SPIDER_REGISTER_TASK(sum);
11 changes: 7 additions & 4 deletions src/spider/worker/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstddef>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -101,14 +102,16 @@ auto response_get_result_buffers(msgpack::sbuffer const& buffer)
// NOLINTEND(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic)
}

auto FunctionManager::get_instance() -> FunctionManager& {
auto FunctionManager::get_instance() noexcept -> FunctionManager& {
static FunctionManager instance;
return instance;
}

auto FunctionManager::get_function(std::string const& name) const -> Function const* {
if (auto const func_iter = m_function_map.find(name); func_iter != m_function_map.end()) {
return &(func_iter->second);
auto FunctionManager::get_function(std::string_view name) const -> Function const* {
for (auto const& pair : m_function_map) {
if (pair.first == name) {
return &pair.second;
}
Comment on lines +110 to +114

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Potential dangling std::string_view in stored map – risk of UB

m_function_map stores std::string_view values supplied by caller code.
When register_function_invoker() is called with a temporary or a std::string that goes out of scope, the stored view will dangle.
Subsequent get_function() look-ups will then dereference invalid memory (undefined behaviour).

Suggested fix: own the key.

-using FunctionMap = std::vector<std::pair<std::string_view, Function>>;
+using FunctionMap = std::vector<std::pair<std::string, Function>>;

and in the registrar:

-m_function_map.emplace_back(name, f);
+m_function_map.emplace_back(std::string{name}, f);

If zero-copy is required, document that the caller must pass a string literal or other static storage and enforce it with static_assert(!std::is_same_v<std::decay_t<decltype(name)>, std::string>).

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
auto FunctionManager::get_function(std::string_view name) const -> Function const* {
for (auto const& pair : m_function_map) {
if (pair.first == name) {
return &pair.second;
}
// Change the map alias to own its keys
- using FunctionMap = std::vector<std::pair<std::string_view, Function>>;
+ using FunctionMap = std::vector<std::pair<std::string, Function>>;
// In the registrar where functions are added
- m_function_map.emplace_back(name, f);
+ m_function_map.emplace_back(std::string{name}, f);

}
return nullptr;
}
Expand Down
46 changes: 28 additions & 18 deletions src/spider/worker/FunctionManager.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#ifndef SPIDER_WORKER_FUNCTIONMANAGER_HPP
#define SPIDER_WORKER_FUNCTIONMANAGER_HPP

#include <algorithm>
#include <cstdint>
#include <exception>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>

#include <absl/container/flat_hash_map.h>
#include <boost/uuid/uuid.hpp>
#include <fmt/format.h>

Expand Down Expand Up @@ -46,7 +47,7 @@ using ResultBuffer = msgpack::sbuffer;

using Function = std::function<ResultBuffer(TaskContext& context, ArgsBuffer const&)>;

using FunctionMap = absl::flat_hash_map<std::string, Function>;
using FunctionMap = std::vector<std::pair<std::string_view, Function>>;

template <class T>
struct TemplateParameter;
Expand Down Expand Up @@ -358,35 +359,44 @@ class FunctionManager {

auto operator=(FunctionManager&&) -> FunctionManager& = delete;

static auto get_instance() -> FunctionManager&;
static auto get_instance() noexcept -> FunctionManager&;

template <class F>
auto register_function(std::string const& name, F f) -> bool {
if (m_function_map.contains(name)) {
auto register_function(std::string_view name, F f) noexcept -> bool {
if (contains(name)) {
return false;
}
return m_function_map
.emplace(
name,
std::bind(
&FunctionInvoker<F>::apply,
std::move(f),
std::placeholders::_1,
std::placeholders::_2
)
m_function_map.emplace_back(
name,
std::bind(
&FunctionInvoker<F>::apply,
std::move(f),
std::placeholders::_1,
std::placeholders::_2
)
.second;
);
return true;
}
Comment on lines 364 to 379

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

noexcept + std::string_view here is unsafe and may terminate the process

  1. register_function is declared noexcept, yet:

    • std::ranges::any_of, emplace_back, std::bind, and the allocator inside std::vector can all throw (bad_alloc, etc.).
    • Any exception will invoke std::terminate, hiding the root cause.
  2. std::string_view is stored in the vector.
    If the caller passes anything with a shorter lifetime than static storage (e.g., std::string name returned from a factory), the stored view will dangle → UB.

Proposed fix:

-auto register_function(std::string_view name, F f) noexcept -> bool {
+template <class F>
+auto register_function(std::string_view name, F f) -> bool {
@@
-    m_function_map.emplace_back(
-            name,
+    // Own the key to avoid dangling views
+    m_function_map.emplace_back(
+            std::string{name},
             std::bind(
                     &FunctionInvoker<F>::apply,
                     std::move(f),
                     std::placeholders::_1,
                     std::placeholders::_2
             )
     );
     return true;
 }

(This removes noexcept and takes ownership of the key.)

Alternatively, keep the string_view but constrain the API so only string literals can be passed (harder to police).

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
template <class F>
auto register_function(std::string const& name, F f) -> bool {
if (m_function_map.contains(name)) {
auto register_function(std::string_view name, F f) noexcept -> bool {
if (contains(name)) {
return false;
}
return m_function_map
.emplace(
name,
std::bind(
&FunctionInvoker<F>::apply,
std::move(f),
std::placeholders::_1,
std::placeholders::_2
)
m_function_map.emplace_back(
name,
std::bind(
&FunctionInvoker<F>::apply,
std::move(f),
std::placeholders::_1,
std::placeholders::_2
)
.second;
);
return true;
}
template <class F>
auto register_function(std::string_view name, F f) -> bool {
if (contains(name)) {
return false;
}
m_function_map.emplace_back(
std::string{name},
std::bind(
&FunctionInvoker<F>::apply,
std::move(f),
std::placeholders::_1,
std::placeholders::_2
)
);
return true;
}


auto register_function_invoker(std::string const& name, Function f) -> bool {
return m_function_map.emplace(name, f).second;
auto register_function_invoker(std::string_view name, Function const& f) -> bool {
if (contains(name)) {
return false;
}
m_function_map.emplace_back(name, f);
return true;
}
Comment on lines +381 to 387

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Same lifetime problem applies to register_function_invoker

register_function_invoker stores std::string_view without owning its data. Please apply the same remediation (store std::string or clearly document and enforce that callers pass only string literals).

-m_function_map.emplace_back(name, f);
+m_function_map.emplace_back(std::string{name}, f);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
auto register_function_invoker(std::string_view name, Function const& f) -> bool {
if (contains(name)) {
return false;
}
m_function_map.emplace_back(name, f);
return true;
}
auto register_function_invoker(std::string_view name, Function const& f) -> bool {
if (contains(name)) {
return false;
}
m_function_map.emplace_back(std::string{name}, f);
return true;
}


[[nodiscard]] auto get_function(std::string const& name) const -> Function const*;
[[nodiscard]] auto get_function(std::string_view name) const -> Function const*;

[[nodiscard]] auto get_function_map() const -> FunctionMap const& { return m_function_map; }

private:
[[nodiscard]] auto contains(std::string_view name) const -> bool {
return std::ranges::any_of(m_function_map, [&name](auto const& pair) {
return pair.first == name;
});
}

FunctionManager() = default;

~FunctionManager() = default;
Expand Down
8 changes: 5 additions & 3 deletions src/spider/worker/FunctionNameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
#include <boost/dll/alias.hpp>

namespace spider::core {
auto FunctionNameManager::get_instance() -> FunctionNameManager& {
auto FunctionNameManager::get_instance() noexcept -> FunctionNameManager& {
static FunctionNameManager instance;
return instance;
}

auto FunctionNameManager::get_function_name(void const* ptr) const -> std::optional<std::string> {
if (auto const& it = m_name_map.find(ptr); it != m_name_map.end()) {
return it->second;
for (auto const& it : m_name_map) {
if (it.first == ptr) {
return std::string{it.second};
}
}
return std::nullopt;
}
Expand Down
26 changes: 20 additions & 6 deletions src/spider/worker/FunctionNameManager.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef SPIDER_CORE_FUNCTIONNAMEMANAGER_HPP
#define SPIDER_CORE_FUNCTIONNAMEMANAGER_HPP

#include <algorithm>
#include <optional>
#include <string>

#include <absl/container/flat_hash_map.h>
#include <string_view>
#include <utility>
#include <vector>

// NOLINTBEGIN(cppcoreguidelines-macro-usage)
#define NAME_CONCAT_DIRECT(s1, s2) s1##s2
Expand All @@ -17,7 +19,7 @@
= spider::core::FunctionNameManager::get_instance().register_function(#func, func);

namespace spider::core {
using FunctionNameMap = absl::flat_hash_map<void*, std::string>;
using FunctionNameMap = std::vector<std::pair<void*, std::string_view>>;

class FunctionNameManager {
public:
Expand All @@ -29,12 +31,24 @@ class FunctionNameManager {

auto operator=(FunctionNameManager&&) -> FunctionNameManager& = delete;

static auto get_instance() -> FunctionNameManager&;
static auto get_instance() noexcept -> FunctionNameManager&;

template <typename F>
auto register_function(std::string const& name, F function_pointer) -> bool {
auto register_function(std::string_view name, F function_pointer) noexcept -> bool {
if (std::ranges::find_if(
m_name_map,
[function_pointer](auto const& pair) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return pair.first == reinterpret_cast<void*>(function_pointer);
}
)
!= m_name_map.end())
{
return false;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return m_name_map.emplace(reinterpret_cast<void*>(function_pointer), name).second;
m_name_map.emplace_back(reinterpret_cast<void*>(function_pointer), name);
return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Same dangling string_view concern as in FunctionManager

name is forwarded unowned into the vector; if the argument is not a string literal the view may dangle after the call. Either:

  1. Change the container to hold std::string, or
  2. Constrain the API so only compile-time string literals are accepted (e.g., via a user-defined literal or const char(&)[N]).

}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

[[nodiscard]] auto get_function_name(void const* ptr) const -> std::optional<std::string>;
Expand Down
2 changes: 0 additions & 2 deletions tests/worker/signal-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,5 @@ auto sleep_test(spider::TaskContext&, int const seconds) -> int {
return 0;
}

// NOLINTNEXTLINE(cert-err58-cpp)
SPIDER_REGISTER_TASK(signal_handler_test);
// NOLINTNEXTLINE(cert-err58-cpp)
SPIDER_REGISTER_TASK(sleep_test);
2 changes: 0 additions & 2 deletions tests/worker/worker-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ auto join_string_test(
return input_1 + input_2;
}

// NOLINTBEGIN(cert-err58-cpp)
SPIDER_REGISTER_TASK(sum_test);
SPIDER_REGISTER_TASK(swap_test);
SPIDER_REGISTER_TASK(error_test);
Expand All @@ -79,4 +78,3 @@ SPIDER_REGISTER_TASK(random_fail_test);
SPIDER_REGISTER_TASK(create_data_test);
SPIDER_REGISTER_TASK(create_task_test);
SPIDER_REGISTER_TASK(join_string_test);
// NOLINTEND(cert-err58-cpp)