Skip to content

Commit 783d4f0

Browse files
committed
HttpLineReader: prevent memory overflow
1 parent d18612c commit 783d4f0

File tree

2 files changed

+67
-65
lines changed

2 files changed

+67
-65
lines changed

src/AudioConfig.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@
9797
#define COPY_RETRY_LIMIT 20
9898
#endif
9999

100+
#ifndef MAX_HTTP_HEADER_LINE_LENGTH
101+
#define MAX_HTTP_HEADER_LINE_LENGTH 240
102+
#endif
100103

101104
/**
102105
* -------------------------------------------------------------------------
@@ -114,9 +117,6 @@
114117
#define PWM_FREQUENCY 60000
115118
#endif
116119

117-
#ifndef MAX_HTTP_HEADER_LINE_LENGTH
118-
#define MAX_HTTP_HEADER_LINE_LENGTH 240
119-
#endif
120120

121121
/**
122122
* -------------------------------------------------------------------------

src/AudioHttp/HttpLineReader.h

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,76 @@
55
namespace audio_tools {
66

77
/**
8-
* @brief We read a single line. A terminating 0 is added to the string to make it
9-
* compliant for c string functions.
10-
* @author Phil Schatzmann
11-
* @copyright GPLv3
12-
*/
8+
* @brief We read a single line. A terminating 0 is added to the string to make it
9+
* compliant for c string functions.
10+
* @author Phil Schatzmann
11+
* @copyright GPLv3
12+
*/
1313

1414
class HttpLineReader {
15-
public:
16-
HttpLineReader(){}
17-
// reads up the the next CR LF - but never more then the indicated len. returns the number of characters read including crlf
18-
virtual int readlnInternal(Stream &client, uint8_t* str, int len, bool incl_nl=true){
19-
int result = 0;
20-
LOGD( "HttpLineReader %s","readlnInternal");
21-
// wait for first character
22-
for (int w=0;w<20 && client.available()==0; w++){
23-
delay(100);
24-
}
25-
// if we do not have any data we stop
26-
if (client.available()==0) {
27-
LOGW( "HttpLineReader %s","readlnInternal->no Data");
28-
str[0]=0;
29-
return 0;
30-
}
15+
public:
16+
HttpLineReader(){}
17+
// reads up the the next CR LF - but never more then the indicated len. returns the number of characters read including crlf
18+
virtual int readlnInternal(Stream &client, uint8_t* str, int len, bool incl_nl=true){
19+
int result = 0;
20+
LOGD( "HttpLineReader %s","readlnInternal");
21+
// wait for first character
22+
for (int w=0;w<20 && client.available()==0; w++){
23+
delay(100);
24+
}
25+
// if we do not have any data we stop
26+
if (client.available()==0) {
27+
LOGW( "HttpLineReader %s","readlnInternal->no Data");
28+
str[0]=0;
29+
return 0;
30+
}
3131

32-
// process characters
33-
bool is_buffer_owerflow = false;
34-
for (int j=0;j<len;j++){
35-
int c = client.read();
36-
if (c==-1){
37-
break;
38-
}
32+
// process characters until we find a new line
33+
bool is_buffer_owerflow = false;
34+
int j=0;
35+
while (true){
36+
int c = client.read();
37+
if (c==-1){
38+
break;
39+
}
3940

40-
if (j<len){
41-
result++;
42-
} else {
43-
is_buffer_owerflow = true;
44-
}
41+
if (j<len){
42+
result++;
43+
} else {
44+
is_buffer_owerflow = true;
45+
}
4546

46-
if (c=='\n'){
47-
if (incl_nl){
48-
str[j]=c;
49-
break;
50-
} else {
51-
// remove cl lf
52-
if (j>=1){
53-
if (str[j-1]=='\r'){
54-
// remove cr
55-
str[j-1]=0;
56-
break;;
57-
} else {
58-
// remove nl
59-
str[j]=0;
60-
break;
61-
}
62-
}
63-
}
64-
}
65-
if (!is_buffer_owerflow){
66-
str[j] = c;
67-
}
68-
}
69-
str[result]=0;
70-
if (is_buffer_owerflow){
71-
LOGE("Line cut off");
72-
}
47+
if (c=='\n'){
48+
if (incl_nl){
49+
str[j]=c;
50+
break;
51+
} else {
52+
// remove cr lf
53+
if (j>=1){
54+
if (str[j-1]=='\r'){
55+
// remove cr
56+
str[j-1]=0;
57+
break;;
58+
} else {
59+
// remove nl
60+
str[j]=0;
61+
break;
62+
}
63+
}
64+
}
65+
}
66+
if (!is_buffer_owerflow){
67+
str[j] = c;
68+
}
69+
j++;
70+
}
71+
str[result-1]=0;
72+
if (is_buffer_owerflow){
73+
LOGE("Line cut off: %s", str);
74+
}
7375

74-
return result;
75-
}
76+
return result;
77+
}
7678
};
7779

7880
}

0 commit comments

Comments
 (0)