Skip to content

Commit

Permalink
Merge pull request esp8266#1544 from andig/md5
Browse files Browse the repository at this point in the history
Fix buffer overflow and formatting
  • Loading branch information
igrr committed Jan 31, 2016
2 parents 8899f55 + 77ab33f commit 8a26750
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions cores/esp8266/MD5Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,42 @@ void MD5Builder::addHexString(const char * data){
}

bool MD5Builder::addStream(Stream & stream, const size_t total_len) {
const int buf_size = 512;
int bytesleft = total_len;
uint8_t * buf = (uint8_t*) malloc(buf_size);
if(buf) {
while((stream.available() > -1) && (bytesleft > 0)) {
const int buf_size = 512;
int bytesleft = total_len;
uint8_t * buf = (uint8_t*) malloc(buf_size);
if(buf) {
while((stream.available() > -1) && (bytesleft > 0)) {
// get available data size
int sizeAvailable = stream.available();
if(sizeAvailable) {
int readBytes = sizeAvailable;

// get available data size
int sizeAvailable = stream.available();
if(sizeAvailable) {
int readBytes = sizeAvailable;

// read only the asked bytes
if(readBytes > bytesleft) {
readBytes = bytesleft ;
}
// read only the asked bytes
if(readBytes > bytesleft) {
readBytes = bytesleft ;
}

// not read more the buffer can handle
if(readBytes > buf_size) {
readBytes = buf_size;
}
// not read more the buffer can handle
if(readBytes > buf_size) {
readBytes = buf_size;
}

// read data
int bytesread = stream.readBytes(buf, readBytes);
bytesleft -= bytesread;
if(bytesread > 0) {
MD5Update(&_ctx, buf, bytesread);
}
}
// time for network streams
delay(0);
// read data
int bytesread = stream.readBytes(buf, readBytes);
bytesleft -= bytesread;
if(bytesread > 0) {
MD5Update(&_ctx, buf, bytesread);
}
// not free null ptr
free(buf);
return (bytesleft == 0);
} else {
return false;
}
// time for network streams
delay(0);
}
// guaranteed not null
free(buf);
return (bytesleft == 0);
} else {
return false;
}
}

void MD5Builder::calculate(void){
Expand All @@ -77,7 +76,7 @@ void MD5Builder::getChars(char * output){
}

String MD5Builder::toString(void){
char out[32];
char out[33];
getChars(out);
return String(out);
}

0 comments on commit 8a26750

Please sign in to comment.