Skip to content

Commit 6562076

Browse files
Krzysztof Blazewiczpfalcon
authored andcommitted
py/stream.c: use mp_obj_get_type in mp_get_stream_raise
In current state `mp_get_stream_raise` assumes that `self_in` is an object and always performs a pointer derefence which may cause a segfault. This function shall throw an exception whenever `self_in` does not implement a stream protocol, that includes qstr's and numbers. fixes #2331
1 parent 5a5449d commit 6562076

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

py/stream.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf_, mp_uint_t size, int *errcode
9494
}
9595

9696
const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
97-
mp_obj_base_t *o = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
98-
const mp_stream_p_t *stream_p = o->type->protocol;
97+
mp_obj_type_t *type = mp_obj_get_type(self_in);
98+
const mp_stream_p_t *stream_p = type->protocol;
9999
if (stream_p == NULL
100100
|| ((flags & MP_STREAM_OP_READ) && stream_p->read == NULL)
101101
|| ((flags & MP_STREAM_OP_WRITE) && stream_p->write == NULL)
@@ -167,7 +167,7 @@ STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte fl
167167
// TODO what if we have read only half a non-ASCII char?
168168
vstr_cut_tail_bytes(&vstr, more_bytes - out_sz);
169169
if (out_sz == 0) {
170-
break;
170+
break;
171171
}
172172
}
173173

0 commit comments

Comments
 (0)