diff options
author | Damien George <damien.p.george@gmail.com> | 2014-07-22 11:01:24 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-07-22 11:01:24 +0100 |
commit | 3e0bce3587acd99a91b1d12fffba556f4e43a0d9 (patch) | |
tree | 6db71e64e858aa9547e80d4c8829626832b80e3e /py/stream.c | |
parent | 512465bc66082a37cb696ef7c78e3ee878ffd92f (diff) | |
parent | 1d8816c36bbb638cd9e5319f820d2b2cf22a9822 (diff) | |
download | micropython-3e0bce3587acd99a91b1d12fffba556f4e43a0d9.tar.gz micropython-3e0bce3587acd99a91b1d12fffba556f4e43a0d9.zip |
Merge pull request #767 from dhylands/fix-short-read
Deal with reading a buffer less than what was allocated.
Diffstat (limited to 'py/stream.c')
-rw-r--r-- | py/stream.c | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/py/stream.c b/py/stream.c index 4c8b8a570a..5b5fa90df4 100644 --- a/py/stream.c +++ b/py/stream.c @@ -97,7 +97,7 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) { } int error; mp_int_t out_sz = o->type->stream_p->read(o, p, more_bytes, &error); - if (out_sz == -1) { + if (out_sz < 0) { vstr_cut_tail_bytes(&vstr, more_bytes); if (is_nonblocking_error(error)) { // 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) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error)); } - if (out_sz == 0) { + if (out_sz < more_bytes) { // Finish reading. // TODO what if we have read only half a non-ASCII char? - vstr_cut_tail_bytes(&vstr, more_bytes); - break; + vstr_cut_tail_bytes(&vstr, more_bytes - out_sz); + if (out_sz == 0) { + break; + } } // count chars from bytes just read |