Skip to content

Commit b361871

Browse files
authored
Merge pull request #5234 from dhalbert/validate-WaveFile-buffer-arg
restrict WaveFile buffer size to 8-1024
2 parents 5425454 + a608934 commit b361871

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

locale/circuitpython.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ msgid "%q indices must be integers, not %s"
103103
msgstr ""
104104

105105
#: py/argcheck.c
106-
msgid "%q length must be %q"
106+
msgid "%q length must be %d-%d"
107107
msgstr ""
108108

109109
#: shared-bindings/usb_hid/Device.c

py/argcheck.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ mp_float_t mp_arg_validate_obj_float_non_negative(mp_obj_t float_in, mp_float_t
195195
return f;
196196
}
197197

198-
size_t mp_arg_validate_length_with_name(mp_int_t i, size_t length, qstr arg_name, qstr length_name) {
199-
if (i != (mp_int_t)length) {
200-
mp_raise_ValueError_varg(translate("%q length must be %q"), arg_name, length_name);
198+
mp_uint_t mp_arg_validate_length_range(mp_uint_t length, mp_uint_t min, mp_uint_t max, qstr arg_name) {
199+
if (length < min || length > max) {
200+
mp_raise_ValueError_varg(translate("%q length must be %d-%d"), arg_name, min, max);
201201
}
202-
return (size_t)i;
202+
return length;
203203
}
204204

205205
mp_obj_t mp_arg_validate_type(mp_obj_t obj, const mp_obj_type_t *type, qstr arg_name) {

py/runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ mp_int_t mp_arg_validate_int_min(mp_int_t i, mp_int_t min, qstr arg_name);
9595
mp_int_t mp_arg_validate_int_max(mp_int_t i, mp_int_t j, qstr arg_name);
9696
mp_int_t mp_arg_validate_int_range(mp_int_t i, mp_int_t min, mp_int_t max, qstr arg_name);
9797
mp_float_t mp_arg_validate_obj_float_non_negative(mp_obj_t float_in, mp_float_t default_for_null, qstr arg_name);
98-
size_t mp_arg_validate_length_with_name(mp_int_t i, size_t length, qstr arg_name, qstr length_name);
98+
mp_uint_t mp_arg_validate_length_range(mp_uint_t length, mp_uint_t min, mp_uint_t max, qstr arg_name);
9999
mp_obj_t mp_arg_validate_type(mp_obj_t obj, const mp_obj_type_t *type, qstr arg_name);
100100
mp_obj_t mp_arg_validate_string(mp_obj_t obj, qstr arg_name);
101101

shared-bindings/audiocore/WaveFile.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@
3838
//|
3939
//| A .wav file prepped for audio playback. Only mono and stereo files are supported. Samples must
4040
//| be 8 bit unsigned or 16 bit signed. If a buffer is provided, it will be used instead of allocating
41-
//| an internal buffer."""
41+
//| an internal buffer, which can prevent memory fragmentation."""
4242
//|
4343
//| def __init__(self, file: typing.BinaryIO, buffer: WriteableBuffer) -> None:
4444
//| """Load a .wav file for playback with `audioio.AudioOut` or `audiobusio.I2SOut`.
4545
//|
4646
//| :param typing.BinaryIO file: Already opened wave file
47-
//| :param ~_typing.WriteableBuffer buffer: Optional pre-allocated buffer, that will be split in half and used for double-buffering of the data. If not provided, two 256 byte buffers are allocated internally.
47+
//| :param ~_typing.WriteableBuffer buffer: Optional pre-allocated buffer,
48+
//| that will be split in half and used for double-buffering of the data.
49+
//| The buffer must be 8 to 1024 bytes long.
50+
//| If not provided, two 256 byte buffers are initially allocated internally.
4851
//|
4952
//|
5053
//| Playing a wave file from flash::
@@ -83,7 +86,7 @@ STATIC mp_obj_t audioio_wavefile_make_new(const mp_obj_type_t *type, size_t n_ar
8386
mp_buffer_info_t bufinfo;
8487
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
8588
buffer = bufinfo.buf;
86-
buffer_size = bufinfo.len;
89+
buffer_size = mp_arg_validate_length_range(bufinfo.len, 8, 1024, MP_QSTR_buffer);
8790
}
8891
common_hal_audioio_wavefile_construct(self, MP_OBJ_TO_PTR(args[0]),
8992
buffer, buffer_size);

0 commit comments

Comments
 (0)