Skip to content

Commit 596a2d5

Browse files
committed
added protection against empty get_found_bp result.
1 parent bbc232c commit 596a2d5

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

include/crow/routing.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <algorithm>
1010
#include <type_traits>
1111
#include <optional>
12+
#include <limits>
1213

1314
#include "crow/common.h"
1415
#include "crow/http_response.h"
@@ -23,7 +24,7 @@
2324
namespace crow // NOTE: Already documented in "crow/app.h"
2425
{
2526

26-
constexpr const size_t INVALID_BP_ID{SIZE_T_MAX};
27+
constexpr size_t INVALID_BP_ID{SIZE_MAX};
2728

2829
namespace detail
2930
{
@@ -718,7 +719,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
718719
std::function<void(crow::request&, crow::response&, Args...)> handler_;
719720
};
720721

721-
const int RULE_SPECIAL_REDIRECT_SLASH = 1;
722+
constexpr int RULE_SPECIAL_REDIRECT_SLASH = 1;
722723

723724

724725
/// A search tree.
@@ -1529,11 +1530,13 @@ namespace crow // NOTE: Already documented in "crow/app.h"
15291530
CatchallRule& get_catch_all(const routing_handle_result& found) {
15301531
std::vector<Blueprint*> bps_found;
15311532
get_found_bp(found.blueprint_indices, blueprints_, bps_found);
1532-
for (int i = bps_found.size() - 1; i > 0; i--)
1533-
{
1534-
std::vector<uint16_t> bpi = found.blueprint_indices;
1535-
if (bps_found[i]->catchall_rule().has_handler()) {
1536-
return bps_found[i]->catchall_rule();
1533+
if (!bps_found.empty()) {
1534+
for (size_t i = bps_found.size() - 1; i > 0; i--)
1535+
{
1536+
std::vector<uint16_t> bpi = found.blueprint_indices;
1537+
if (bps_found[i]->catchall_rule().has_handler()) {
1538+
return bps_found[i]->catchall_rule();
1539+
}
15371540
}
15381541
}
15391542
return catchall_rule_;
@@ -1545,25 +1548,25 @@ namespace crow // NOTE: Already documented in "crow/app.h"
15451548

15461549
std::vector<Blueprint*> bps_found;
15471550
get_found_bp(found.blueprint_indices, blueprints_, bps_found);
1548-
for (int i = bps_found.size() - 1; i > 0; i--)
1549-
{
1550-
std::vector<uint16_t> bpi = found.blueprint_indices;
1551-
if (bps_found[i]->catchall_rule().has_handler())
1552-
{
1551+
if (!bps_found.empty()) {
1552+
for (size_t i = bps_found.size() - 1; i > 0; i--) {
1553+
std::vector<uint16_t> bpi = found.blueprint_indices;
1554+
if (bps_found[i]->catchall_rule().has_handler()) {
15531555
#ifdef CROW_ENABLE_DEBUG
1554-
return std::string("Redirected to Blueprint \"" + bps_found[i]->prefix() + "\" Catchall rule");
1556+
return std::string("Redirected to Blueprint \"" + bps_found[i]->prefix() + "\" Catchall rule");
15551557
#else
1556-
return EMPTY;
1558+
return EMPTY;
15571559
#endif
1560+
}
15581561
}
1559-
}
1560-
if (catchall_rule_.has_handler())
1561-
{
1562+
if (catchall_rule_.has_handler()) {
15621563
#ifdef CROW_ENABLE_DEBUG
1563-
return std::string("Redirected to global Catchall rule");
1564+
return std::string("Redirected to global Catchall rule");
15641565
#else
1565-
return EMPTY;
1566+
return EMPTY;
15661567
#endif
1568+
}
1569+
15671570
}
15681571
return EMPTY;
15691572
}

0 commit comments

Comments
 (0)