diff --git a/source/common/http/header_map_impl.cc b/source/common/http/header_map_impl.cc index e192438992ee..98097cc976ee 100644 --- a/source/common/http/header_map_impl.cc +++ b/source/common/http/header_map_impl.cc @@ -92,9 +92,12 @@ void HeaderString::append(const char* data, uint32_t size) { // allocate and copy. type_ = Type::Dynamic; const uint64_t new_capacity = newCapacity(string_length_, size); - dynamic_capacity_ = std::max(MinDynamicCapacity, new_capacity); - if (dynamic_capacity_ != MinDynamicCapacity) { + if (new_capacity > MinDynamicCapacity) { + // TODO(alyssawilk) unit test. validateCapacity(new_capacity); + dynamic_capacity_ = new_capacity; + } else { + dynamic_capacity_ = MinDynamicCapacity; } char* buf = static_cast(malloc(dynamic_capacity_)); RELEASE_ASSERT(buf != nullptr, ""); @@ -104,7 +107,8 @@ void HeaderString::append(const char* data, uint32_t size) { } case Type::Inline: { - if (size + 1 + string_length_ <= sizeof(inline_buffer_)) { + const uint64_t new_capacity = static_cast(size) + 1 + string_length_; + if (new_capacity <= sizeof(inline_buffer_)) { // Already inline and the new value fits in inline storage. break; } diff --git a/test/common/http/header_map_impl_test.cc b/test/common/http/header_map_impl_test.cc index dc55e0d2e7ae..70c16ae507e8 100644 --- a/test/common/http/header_map_impl_test.cc +++ b/test/common/http/header_map_impl_test.cc @@ -704,6 +704,13 @@ TEST(HeaderMapImplTest, TestAppendHeader) { } } +TEST(HeaderMapImplTest, TestHeaderLengthChecks) { + HeaderString value; + value.setCopy("some;", 5); + EXPECT_DEATH(value.append(nullptr, std::numeric_limits::max()), + "Trying to allocate overly large headers."); +} + TEST(HeaderMapImplTest, PseudoHeaderOrder) { typedef testing::MockFunction MockCb; MockCb cb;