Skip to content

When reading data from a file into a str, check if it's utf-8 #6754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions py/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "py/objstr.h"
#include "py/stream.h"
#include "py/runtime.h"
#include "py/unicode.h"
#include "supervisor/shared/translate/translate.h"

// This file defines generic Python stream read/write methods which
Expand All @@ -43,6 +44,13 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in);

#define STREAM_CONTENT_TYPE(stream) (((stream)->is_text) ? &mp_type_str : &mp_type_bytes)

static mp_obj_t mp_obj_new_str_from_vstr_check(const mp_obj_type_t *type, vstr_t *vstr) {
if (type == &mp_type_str && !utf8_check((void *)vstr->buf, vstr->len)) {
mp_raise_msg(&mp_type_UnicodeError, NULL);
}
return mp_obj_new_str_from_vstr(type, vstr);
}

// Returns error condition in *errcode, if non-zero, return value is number of bytes written
// before error condition occurred. If *errcode == 0, returns total bytes written (which will
// be equal to input size).
Expand Down Expand Up @@ -201,8 +209,7 @@ STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte fl
}
}
}

return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
return mp_obj_new_str_from_vstr_check(&mp_type_str, &vstr);
}
#endif

Expand All @@ -223,7 +230,7 @@ STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte fl
mp_raise_OSError(error);
} else {
vstr.len = out_sz;
return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
return mp_obj_new_str_from_vstr_check(STREAM_CONTENT_TYPE(stream_p), &vstr);
}
}

Expand Down Expand Up @@ -364,7 +371,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
}

vstr.len = total_size;
return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
return mp_obj_new_str_from_vstr_check(STREAM_CONTENT_TYPE(stream_p), &vstr);
}

// Unbuffered, inefficient implementation of readline() for raw I/O files.
Expand Down Expand Up @@ -417,7 +424,7 @@ STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args)
}
}

return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
return mp_obj_new_str_from_vstr_check(STREAM_CONTENT_TYPE(stream_p), &vstr);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);

Expand Down
2 changes: 1 addition & 1 deletion tests/extmod/qrio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
raise SystemExit

loc = __file__.rsplit("/", 1)[0]
with open(f"{loc}/data/qr.pgm") as f:
with open(f"{loc}/data/qr.pgm", "rb") as f:
content = f.read()[-320 * 240 :]

decoder = qrio.QRDecoder(320, 240)
Expand Down