Skip to content

Commit 06bfc08

Browse files
earlephilhowerbryceschober
authored andcommitted
Check Print::write(byte) return and stop on fail (esp8266#4527)
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. (cherry picked from commit 06352ab)
1 parent 6bdd08e commit 06bfc08

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

cores/esp8266/Print.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@
3434
/* default implementation: may be overridden */
3535
size_t Print::write(const uint8_t *buffer, size_t size) {
3636
size_t n = 0;
37-
while(size--) {
38-
n += write(*buffer++);
37+
while (size--) {
38+
size_t ret = write(*buffer++);
39+
if (ret == 0) {
40+
// Write of last byte didn't complete, abort additional processing
41+
break;
42+
}
43+
n += ret;
3944
}
4045
return n;
4146
}

0 commit comments

Comments
 (0)