Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77ab33f

Browse files
committed
Fix buffer overflow and formatting
1 parent b39146d commit 77ab33f

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

cores/esp8266/MD5Builder.cpp

+32-33
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,42 @@ void MD5Builder::addHexString(const char * data){
2424
}
2525

2626
bool MD5Builder::addStream(Stream & stream, const size_t total_len) {
27-
const int buf_size = 512;
28-
int bytesleft = total_len;
29-
uint8_t * buf = (uint8_t*) malloc(buf_size);
30-
if(buf) {
31-
while((stream.available() > -1) && (bytesleft > 0)) {
27+
const int buf_size = 512;
28+
int bytesleft = total_len;
29+
uint8_t * buf = (uint8_t*) malloc(buf_size);
30+
if(buf) {
31+
while((stream.available() > -1) && (bytesleft > 0)) {
32+
// get available data size
33+
int sizeAvailable = stream.available();
34+
if(sizeAvailable) {
35+
int readBytes = sizeAvailable;
3236

33-
// get available data size
34-
int sizeAvailable = stream.available();
35-
if(sizeAvailable) {
36-
int readBytes = sizeAvailable;
37-
38-
// read only the asked bytes
39-
if(readBytes > bytesleft) {
40-
readBytes = bytesleft ;
41-
}
37+
// read only the asked bytes
38+
if(readBytes > bytesleft) {
39+
readBytes = bytesleft ;
40+
}
4241

43-
// not read more the buffer can handle
44-
if(readBytes > buf_size) {
45-
readBytes = buf_size;
46-
}
42+
// not read more the buffer can handle
43+
if(readBytes > buf_size) {
44+
readBytes = buf_size;
45+
}
4746

48-
// read data
49-
int bytesread = stream.readBytes(buf, readBytes);
50-
bytesleft -= bytesread;
51-
if(bytesread > 0) {
52-
MD5Update(&_ctx, buf, bytesread);
53-
}
54-
}
55-
// time for network streams
56-
delay(0);
47+
// read data
48+
int bytesread = stream.readBytes(buf, readBytes);
49+
bytesleft -= bytesread;
50+
if(bytesread > 0) {
51+
MD5Update(&_ctx, buf, bytesread);
5752
}
58-
// not free null ptr
59-
free(buf);
60-
return (bytesleft == 0);
61-
} else {
62-
return false;
53+
}
54+
// time for network streams
55+
delay(0);
6356
}
57+
// guaranteed not null
58+
free(buf);
59+
return (bytesleft == 0);
60+
} else {
61+
return false;
62+
}
6463
}
6564

6665
void MD5Builder::calculate(void){
@@ -77,7 +76,7 @@ void MD5Builder::getChars(char * output){
7776
}
7877

7978
String MD5Builder::toString(void){
80-
char out[32];
79+
char out[33];
8180
getChars(out);
8281
return String(out);
8382
}

0 commit comments

Comments
 (0)