Skip to content

Commit 1178428

Browse files
author
Ulrich Lukas
committed
Fix me-no-dev#837 invalidated iterator when removing items from list
1 parent f6fff3f commit 1178428

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/WebRequest.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,17 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len){
179179
}
180180

181181
void AsyncWebServerRequest::_removeNotInterestingHeaders(){
182-
if (_interestingHeaders.containsIgnoreCase("ANY")) return; // nothing to do
183-
for(const auto& header: _headers){
182+
if (_interestingHeaders.containsIgnoreCase("ANY")) {
183+
return; // nothing to do
184+
}
185+
// When removing items from the list, we must increase the iterator first
186+
// before removing the current item, otherwise the iterator is invalidated
187+
// So, no for(;;) loop can be used, see: https://stackoverflow.com/q/596162
188+
auto i_header = _headers.begin();
189+
const auto i_end = _headers.end();
190+
while (i_header != i_end){
191+
const auto header = *i_header;
192+
++i_header;
184193
if(!_interestingHeaders.containsIgnoreCase(header->name().c_str())){
185194
_headers.remove(header);
186195
}

0 commit comments

Comments
 (0)