From 10d9ea2184a7a45acefc069ec959f46a6b4c5f8f Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 16 Mar 2018 11:06:48 -0700 Subject: [PATCH] Check Print::write(byte) return and stop on fail The default Print::write(byte, count) method was continuing to send bytes one-by-one even when a prior write returned 0. Because the buffer pointer was incremented no matter success or fail, this leads to data corruption as you'll not send some bytes in the middle and will then send extra bytes past the end of the passed in buffer. Because there's no concept of timeout, just stop on the first time write(byte) fails and return the total bytes successfully written and let the user worry about retrying or handling an error. Found by @d-a-v and discussed on gitter. --- cores/esp8266/Print.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/Print.cpp b/cores/esp8266/Print.cpp index 0c3d362ab7..786c594065 100644 --- a/cores/esp8266/Print.cpp +++ b/cores/esp8266/Print.cpp @@ -34,8 +34,13 @@ /* default implementation: may be overridden */ size_t Print::write(const uint8_t *buffer, size_t size) { size_t n = 0; - while(size--) { - n += write(*buffer++); + while (size--) { + size_t ret = write(*buffer++); + if (ret == 0) { + // Write of last byte didn't complete, abort additional processing + break; + } + n += ret; } return n; }