Skip to content

Commit

Permalink
Merge pull request #1209 from tyler92/fix-remove-route-crash
Browse files Browse the repository at this point in the history
fix(server): crash if remove not existing route
  • Loading branch information
Tachi107 authored Apr 28, 2024
2 parents fd4a75f + 8ae6b61 commit 9331499
Show file tree
Hide file tree
Showing 24 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .reuse/dep5
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/

Files: tests/fuzzers/corpus/http-*.txt
Files: tests/fuzzers/corpus/*
Copyright: 2024 Mikhail Khachayants
License: Apache-2.0

7 changes: 6 additions & 1 deletion src/server/router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ namespace Pistache::Rest
SegmentTreeNode::SegmentType
SegmentTreeNode::getSegmentType(const std::string_view& fragment)
{
if (fragment.empty())
return SegmentType::Fixed;

auto optpos = fragment.find('?');
if (fragment[0] == ':')
{
Expand Down Expand Up @@ -217,6 +220,8 @@ namespace Pistache::Rest
collection = &optional_;
break;
case SegmentType::Splat:
if (!splat_)
throw std::runtime_error("Requested route does not exist.");
return splat_->removeRoute(lower_path);
}

Expand All @@ -230,7 +235,7 @@ namespace Pistache::Rest
}
catch (const std::out_of_range&)
{
throw std::runtime_error("Requested does not exist.");
throw std::runtime_error("Requested route does not exist.");
}
}
else
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
S*/apiR
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/fuzzers/fuzz_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void fuzz_router(const std::string& input)
});
break;
case 'R':
// ignoreExceptions([&] { tree.removeRoute(path); });
ignoreExceptions([&] { tree.removeRoute(path); });
break;
case 'F':
ignoreExceptions([&] { tree.findRoute(path); });
Expand Down
24 changes: 24 additions & 0 deletions tests/router_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <algorithm>
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>

#include <pistache/common.h>
#include <pistache/endpoint.h>
Expand Down Expand Up @@ -248,6 +249,29 @@ TEST(router_test, test_route_head_request)
endpoint->shutdown();
}

TEST(router_test, test_remove_not_existing)
{
SegmentTreeNode routes;

using testing::ThrowsMessage;

ASSERT_THAT(
[&] { routes.removeRoute("/v1/hello"); },
ThrowsMessage<std::runtime_error>("Requested route does not exist."));
ASSERT_THAT(
[&] { routes.removeRoute("/v1/hello/:name/"); },
ThrowsMessage<std::runtime_error>("Requested route does not exist."));
ASSERT_THAT(
[&] { routes.removeRoute("/get/:key?/bar"); },
ThrowsMessage<std::runtime_error>("Requested route does not exist."));
ASSERT_THAT(
[&] { routes.removeRoute("/say/*/to/*"); },
ThrowsMessage<std::runtime_error>("Requested route does not exist."));
ASSERT_THAT(
[&] { routes.removeRoute("*/api"); },
ThrowsMessage<std::runtime_error>("Requested route does not exist."));
}

class MyHandler
{
public:
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.9.20240427
0.2.9.20240428

0 comments on commit 9331499

Please sign in to comment.