Skip to content

Mutating Middleware context in CROW_CATCHALL_ROUTE causes Segmentation fault #859

@lbatuska

Description

@lbatuska

When I try to modify a value contained in the context struct in a CROW_CATCHALL_ROUTE it causes a segfault but works perfectly with a CROW_ROUTE

A very minimal example to reproduce it:

struct TestMW {
  struct context {  // NOLINT(readability-identifier-naming)
    int id{};
    std::string s;
    context() : s("Test") {}
  };

  void before_handle(  // NOLINT(readability-convert-member-functions-to-static)
      crow::request& req,  // NOLINT(misc-unused-parameters)
      crow::response& res,
      context& ctx) {  // NOLINT(misc-unused-parameters)
  }

  void after_handle(  // NOLINT(readability-convert-member-functions-to-static)
      crow::request& req,   // NOLINT(misc-unused-parameters)
      crow::response& res,  // NOLINT(misc-unused-parameters)
      context& ctx          // NOLINT(misc-unused-parameters)
  ) {}
};

int main() {
  crow::App<TestMW> app;
  app.loglevel(crow::LogLevel::Debug);
  CROW_CATCHALL_ROUTE(app)
  ([&](crow::request const& req, crow::response& res) {
    auto& ctx = app.get_context<TestMW>(req);
    ctx.s = "Something else";
    res.write("Test Write");
    res.end();
  });

  app.port(8181).multithreaded().run();
}

When I run the following code and try curl-ing on any url on localhost:8181 I get the following on the server side:

(2024-07-27 19:19:54) [INFO    ] Crow/master server is running at http://0.0.0.0:8181 using 12 threads
(2024-07-27 19:19:54) [INFO    ] Call app.loglevel(crow::LogLevel::Warning) to hide Info level logs.
(2024-07-27 19:19:54) [DEBUG   ] 0xaac179bbc840 {0} queue length: 1
(2024-07-27 19:19:58) [DEBUG   ] 0xaac179bbc450 {1} queue length: 1
(2024-07-27 19:19:58) [DEBUG   ] 0xaac179bc7960 timer cancelled: 0xff930fdfdfa8 0
(2024-07-27 19:19:58) [DEBUG   ] task_timer cancelled: 0xff930fdfdfa8 0
(2024-07-27 19:19:58) [DEBUG   ] task_timer scheduled: 0xff930fdfdfa8 1
(2024-07-27 19:19:58) [DEBUG   ] 0xaac179bc7960 timer added: 0xff930fdfdfa8 1
Segmentation fault (core dumped)

I have tested it on 2 different systems with different architectures ARM64 / aarch64 (Win11 wsl2) and x86_64 (Arch linux) unfortunately with the same result.
I'm using the origin/v1.2.0 tag with it's latest commit.

Thank You in Advance for the help and let me know if I can provide any extra info!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions