From 77ab33f7bf2c7fdba04c0f1dc8a015a4dae2095c Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 31 Jan 2016 18:47:13 +0100 Subject: [PATCH] Fix buffer overflow and formatting --- cores/esp8266/MD5Builder.cpp | 65 ++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/cores/esp8266/MD5Builder.cpp b/cores/esp8266/MD5Builder.cpp index a382093a4a..dd3d9dd333 100644 --- a/cores/esp8266/MD5Builder.cpp +++ b/cores/esp8266/MD5Builder.cpp @@ -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){ @@ -77,7 +76,7 @@ void MD5Builder::getChars(char * output){ } String MD5Builder::toString(void){ - char out[32]; + char out[33]; getChars(out); return String(out); } \ No newline at end of file