1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-22 21:23:07 +03:00

Check Print::write(byte) return and stop on fail (#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.
This commit is contained in:
Earle F. Philhower, III 2018-03-16 21:38:56 -07:00 committed by Develo
parent 732e22ec37
commit 06352ab0e1

View File

@ -35,7 +35,12 @@
size_t Print::write(const uint8_t *buffer, size_t size) { size_t Print::write(const uint8_t *buffer, size_t size) {
size_t n = 0; size_t n = 0;
while (size--) { while (size--) {
n += write(*buffer++); size_t ret = write(*buffer++);
if (ret == 0) {
// Write of last byte didn't complete, abort additional processing
break;
}
n += ret;
} }
return n; return n;
} }