Skip to content

Commit 3e0bce3

Browse files
committed
Merge pull request adafruit#767 from dhylands/fix-short-read
Deal with reading a buffer less than what was allocated.
2 parents 512465b + 1d8816c commit 3e0bce3

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

py/stream.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
9797
}
9898
int error;
9999
mp_int_t out_sz = o->type->stream_p->read(o, p, more_bytes, &error);
100-
if (out_sz == -1) {
100+
if (out_sz < 0) {
101101
vstr_cut_tail_bytes(&vstr, more_bytes);
102102
if (is_nonblocking_error(error)) {
103103
// With non-blocking streams, we read as much as we can.
@@ -113,11 +113,13 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
113113
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
114114
}
115115

116-
if (out_sz == 0) {
116+
if (out_sz < more_bytes) {
117117
// Finish reading.
118118
// TODO what if we have read only half a non-ASCII char?
119-
vstr_cut_tail_bytes(&vstr, more_bytes);
120-
break;
119+
vstr_cut_tail_bytes(&vstr, more_bytes - out_sz);
120+
if (out_sz == 0) {
121+
break;
122+
}
121123
}
122124

123125
// count chars from bytes just read

0 commit comments

Comments
 (0)