Skip to content

Commit f0f2beb

Browse files
author
QuickSander
committed
HttpResource::operator[] robust against out of range index
1 parent 3647099 commit f0f2beb

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.gcc-flags.json
55
.piolibdeps
66
.pio
7+
.vscode

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"name": "Sander van Woensel",
88
"url": "http://sander.technology"
99
},
10-
"version": "0.9.5",
10+
"version": "0.9.6",
1111
"repository":
1212
{
1313
"type": "git",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoHttpServer
2-
version=0.9.5
2+
version=0.9.6
33
author=Sander van Woensel <[email protected]>
44
maintainer=Sander van Woensel <[email protected]>
55
sentence=Server side minimalistic HTTP protocol implementation.

src/internals/HttpResource.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ bool ArduinoHttpServer::HttpResource::isValid()
3737

3838
//! Retrieve resource part at the specified index.
3939
//! \details E.g. HttpResource("/api/sensors/1/state")[1]
40-
//! returns "sensors"
40+
//! returns "sensors".
41+
//! \returns Empty string when index specified is out of range.
4142
String ArduinoHttpServer::HttpResource::operator[](const unsigned int index) const
4243
{
4344
int fromOffset(0);
@@ -46,12 +47,20 @@ String ArduinoHttpServer::HttpResource::operator[](const unsigned int index) con
4647
for (unsigned int currentIndex=0; currentIndex <= index; ++currentIndex)
4748
{
4849
fromOffset = m_resource.indexOf(RESOURCE_SEPERATOR, fromOffset);
50+
if(fromOffset == -1)
51+
{
52+
return String("");
53+
}
54+
4955
++fromOffset; // Seek past '/'.
5056
}
5157

5258
// Find next possible '/' or end.
5359
int toOffset( m_resource.indexOf(RESOURCE_SEPERATOR, fromOffset) );
54-
toOffset == -1 ? m_resource.length() - 1 : toOffset;
60+
if(toOffset == -1)
61+
{
62+
toOffset = m_resource.length();
63+
}
5564

5665
return m_resource.substring(fromOffset, toOffset);
5766

0 commit comments

Comments
 (0)