Skip to content

Commit d18612c

Browse files
committed
HttpLineReader: prevent memory overflow
1 parent 9348d7a commit d18612c

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/AudioConfig.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,9 @@
114114
#define PWM_FREQUENCY 60000
115115
#endif
116116

117-
/**
118-
* -------------------------------------------------------------------------
119-
* @brief Activate decoders - only after installing them !
120-
*/
121-
122-
//#define USE_HELIX
123-
//#define USE_FDK
124-
//#define USE_LAME
125-
//#define USE_MAD
126-
117+
#ifndef MAX_HTTP_HEADER_LINE_LENGTH
118+
#define MAX_HTTP_HEADER_LINE_LENGTH 240
119+
#endif
127120

128121
/**
129122
* -------------------------------------------------------------------------

src/AudioHttp/HttpHeader.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "AudioBasic/Collections.h"
44
#include "AudioBasic/StrExt.h"
5+
#include "AudioConfig.h"
56
#include "AudioHttp/HttpLineReader.h"
67
#include "AudioHttp/Url.h"
78
#include "AudioHttp/HttpTypes.h"
@@ -10,7 +11,6 @@
1011
namespace audio_tools {
1112

1213
// Class Configuration
13-
const int MaxHeaderLineLength = 240;
1414

1515
// Define relevant header content
1616
const char* CONTENT_TYPE = "Content-Type";
@@ -225,18 +225,18 @@ class HttpHeader {
225225
// remove all existing value
226226
clear();
227227

228-
char line[MaxHeaderLineLength];
228+
char line[MAX_HTTP_HEADER_LINE_LENGTH];
229229
if (in.connected()){
230230
if (in.available()==0) {
231231
LOGW("Waiting for data...");
232232
while(in.available()==0){
233233
delay(500);
234234
}
235235
}
236-
readLine(in, line, MaxHeaderLineLength);
236+
readLine(in, line, MAX_HTTP_HEADER_LINE_LENGTH);
237237
parse1stLine(line);
238238
while (in.available()){
239-
readLine(in, line, MaxHeaderLineLength);
239+
readLine(in, line, MAX_HTTP_HEADER_LINE_LENGTH);
240240
if (isValidStatus() || isRedirectStatus()){
241241
Str lineStr(line);
242242
lineStr.ltrim();
@@ -421,11 +421,11 @@ class HttpReplyHeader : public HttpHeader {
421421
// reads the final chunked reply headers
422422
void readExt(Client &in) {
423423
LOGI("HttpReplyHeader::readExt");
424-
char line[MaxHeaderLineLength];
425-
readLine(in, line, MaxHeaderLineLength);
424+
char line[MAX_HTTP_HEADER_LINE_LENGTH];
425+
readLine(in, line, MAX_HTTP_HEADER_LINE_LENGTH);
426426
while(strlen(line)!=0){
427427
put(line);
428-
readLine(in, line, MaxHeaderLineLength);
428+
readLine(in, line, MAX_HTTP_HEADER_LINE_LENGTH);
429429
}
430430
}
431431

src/AudioHttp/HttpLineReader.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@ class HttpLineReader {
3030
}
3131

3232
// process characters
33+
bool is_buffer_owerflow = false;
3334
for (int j=0;j<len;j++){
3435
int c = client.read();
3536
if (c==-1){
3637
break;
3738
}
38-
result++;
39+
40+
if (j<len){
41+
result++;
42+
} else {
43+
is_buffer_owerflow = true;
44+
}
45+
3946
if (c=='\n'){
4047
if (incl_nl){
4148
str[j]=c;
@@ -55,9 +62,15 @@ class HttpLineReader {
5562
}
5663
}
5764
}
58-
str[j] = c;
65+
if (!is_buffer_owerflow){
66+
str[j] = c;
67+
}
5968
}
6069
str[result]=0;
70+
if (is_buffer_owerflow){
71+
LOGE("Line cut off");
72+
}
73+
6174
return result;
6275
}
6376
};

0 commit comments

Comments
 (0)