Skip to content

Commit 244dc80

Browse files
authored
Merge pull request adafruit#10872 from dhalbert/allocate-after-validations
in `*_make_new()`, allocate obj only after all validations
2 parents 7ecbb1b + 687b4b3 commit 244dc80

File tree

77 files changed

+223
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+223
-167
lines changed

shared-bindings/_bleio/Address.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ static mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args,
3737
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
3838
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
3939

40-
bleio_address_obj_t *self = mp_obj_malloc(bleio_address_obj_t, &bleio_address_type);
41-
4240
const mp_obj_t address = args[ARG_address].u_obj;
4341
mp_buffer_info_t buf_info;
4442
mp_get_buffer_raise(address, &buf_info, MP_BUFFER_READ);
@@ -51,6 +49,7 @@ static mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args,
5149
mp_arg_error_invalid(MP_QSTR_address_type);
5250
}
5351

52+
bleio_address_obj_t *self = mp_obj_malloc(bleio_address_obj_t, &bleio_address_type);
5453
common_hal_bleio_address_construct(self, buf_info.buf, address_type);
5554

5655
return MP_OBJ_FROM_PTR(self);

shared-bindings/_bleio/UUID.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
static mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
3535
mp_arg_check_num(n_args, n_kw, 1, 1, false);
3636

37-
bleio_uuid_obj_t *self = mp_obj_malloc(bleio_uuid_obj_t, &bleio_uuid_type);
38-
3937
const mp_obj_t value = all_args[0];
4038
uint8_t uuid128[16];
4139

@@ -46,8 +44,10 @@ static mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, si
4644
}
4745

4846
// NULL means no 128-bit value.
47+
bleio_uuid_obj_t *self = mp_obj_malloc(bleio_uuid_obj_t, &bleio_uuid_type);
4948
common_hal_bleio_uuid_construct(self, uuid16, NULL);
5049

50+
return MP_OBJ_FROM_PTR(self);
5151
} else {
5252
if (mp_obj_is_str(value)) {
5353
// 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
@@ -93,10 +93,12 @@ static mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, si
9393
uint32_t uuid16 = (uuid128[13] << 8) | uuid128[12];
9494
uuid128[12] = 0;
9595
uuid128[13] = 0;
96+
97+
bleio_uuid_obj_t *self = mp_obj_malloc(bleio_uuid_obj_t, &bleio_uuid_type);
9698
common_hal_bleio_uuid_construct(self, uuid16, uuid128);
97-
}
9899

99-
return MP_OBJ_FROM_PTR(self);
100+
return MP_OBJ_FROM_PTR(self);
101+
}
100102
}
101103

102104
//| uuid16: int

shared-bindings/_stage/Layer.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,38 @@ static mp_obj_t layer_make_new(const mp_obj_type_t *type, size_t n_args,
3838
size_t n_kw, const mp_obj_t *args) {
3939
mp_arg_check_num(n_args, n_kw, 4, 5, false);
4040

41-
layer_obj_t *self = mp_obj_malloc(layer_obj_t, type);
42-
43-
self->width = mp_obj_get_int(args[0]);
44-
self->height = mp_obj_get_int(args[1]);
45-
self->x = 0;
46-
self->y = 0;
47-
self->frame = 0;
48-
self->rotation = false;
41+
mp_uint_t width = mp_arg_validate_int_min(mp_obj_get_int(args[0]), 0, MP_QSTR_width);
42+
mp_uint_t height = mp_arg_validate_int_min(mp_obj_get_int(args[1]), 0, MP_QSTR_height);
4943

50-
mp_buffer_info_t bufinfo;
51-
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
52-
self->graphic = bufinfo.buf;
53-
if (bufinfo.len != 2048) {
54-
mp_raise_ValueError(MP_ERROR_TEXT("graphic must be 2048 bytes long"));
55-
}
44+
mp_buffer_info_t graphic_bufinfo;
45+
mp_get_buffer_raise(args[2], &graphic_bufinfo, MP_BUFFER_READ);
46+
mp_arg_validate_length(graphic_bufinfo.len, 2048, MP_QSTR_graphic);
5647

57-
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
58-
self->palette = bufinfo.buf;
59-
if (bufinfo.len != 32) {
60-
mp_raise_ValueError(MP_ERROR_TEXT("palette must be 32 bytes long"));
61-
}
48+
mp_buffer_info_t palette_bufinfo;
49+
mp_get_buffer_raise(args[3], &palette_bufinfo, MP_BUFFER_READ);
50+
mp_arg_validate_length(palette_bufinfo.len, 32, MP_QSTR_palette);
6251

52+
mp_buffer_info_t map_bufinfo = { .buf = NULL };
6353
if (n_args > 4) {
64-
mp_get_buffer_raise(args[4], &bufinfo, MP_BUFFER_READ);
65-
self->map = bufinfo.buf;
66-
if (bufinfo.len < (self->width * self->height) / 2) {
54+
mp_get_buffer_raise(args[4], &map_bufinfo, MP_BUFFER_READ);
55+
if (map_bufinfo.len < (width * height) / 2) {
6756
mp_raise_ValueError(MP_ERROR_TEXT("map buffer too small"));
6857
}
69-
} else {
70-
self->map = NULL;
7158
}
7259

60+
// Only allocate after validation is finished.
61+
layer_obj_t *self = mp_obj_malloc(layer_obj_t, type);
62+
63+
self->width = width;
64+
self->height = height;
65+
self->x = 0;
66+
self->y = 0;
67+
self->frame = 0;
68+
self->rotation = false;
69+
self->graphic = graphic_bufinfo.buf;
70+
self->palette = palette_bufinfo.buf;
71+
self->map = map_bufinfo.buf;
72+
7373
return MP_OBJ_FROM_PTR(self);
7474
}
7575

shared-bindings/_stage/Text.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,32 @@ static mp_obj_t text_make_new(const mp_obj_type_t *type, size_t n_args,
3838
size_t n_kw, const mp_obj_t *args) {
3939
mp_arg_check_num(n_args, n_kw, 5, 5, false);
4040

41-
text_obj_t *self = mp_obj_malloc(text_obj_t, type);
41+
mp_uint_t width = mp_arg_validate_int_min(mp_obj_get_int(args[0]), 0, MP_QSTR_width);
42+
mp_uint_t height = mp_arg_validate_int_min(mp_obj_get_int(args[1]), 0, MP_QSTR_height);
4243

43-
self->width = mp_obj_get_int(args[0]);
44-
self->height = mp_obj_get_int(args[1]);
45-
self->x = 0;
46-
self->y = 0;
47-
48-
mp_buffer_info_t bufinfo;
49-
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
50-
self->font = bufinfo.buf;
51-
if (bufinfo.len != 2048) {
52-
mp_raise_ValueError(MP_ERROR_TEXT("font must be 2048 bytes long"));
53-
}
44+
mp_buffer_info_t font_bufinfo;
45+
mp_get_buffer_raise(args[2], &font_bufinfo, MP_BUFFER_READ);
46+
mp_arg_validate_length(font_bufinfo.len, 2048, MP_QSTR_font);
5447

55-
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
56-
self->palette = bufinfo.buf;
57-
if (bufinfo.len != 32) {
58-
mp_raise_ValueError(MP_ERROR_TEXT("palette must be 32 bytes long"));
59-
}
48+
mp_buffer_info_t palette_bufinfo;
49+
mp_get_buffer_raise(args[3], &palette_bufinfo, MP_BUFFER_READ);
50+
mp_arg_validate_length(font_bufinfo.len, 32, MP_QSTR_palette);
6051

61-
mp_get_buffer_raise(args[4], &bufinfo, MP_BUFFER_READ);
62-
self->chars = bufinfo.buf;
63-
if (bufinfo.len < self->width * self->height) {
52+
mp_buffer_info_t chars_bufinfo;
53+
mp_get_buffer_raise(args[4], &chars_bufinfo, MP_BUFFER_READ);
54+
if (chars_bufinfo.len < width * height) {
6455
mp_raise_ValueError(MP_ERROR_TEXT("chars buffer too small"));
6556
}
6657

58+
text_obj_t *self = mp_obj_malloc(text_obj_t, type);
59+
self->width = width;
60+
self->height = height;
61+
self->x = 0;
62+
self->y = 0;
63+
self->font = font_bufinfo.buf;
64+
self->palette = palette_bufinfo.buf;
65+
self->chars = chars_bufinfo.buf;
66+
6767
return MP_OBJ_FROM_PTR(self);
6868
}
6969

shared-bindings/adafruit_bus_device/i2c_device/I2CDevice.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
//| ...
4848
//|
4949
static mp_obj_t adafruit_bus_device_i2cdevice_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
50-
adafruit_bus_device_i2cdevice_obj_t *self =
51-
mp_obj_malloc(adafruit_bus_device_i2cdevice_obj_t, &adafruit_bus_device_i2cdevice_type);
5250
enum { ARG_i2c, ARG_device_address, ARG_probe };
5351
static const mp_arg_t allowed_args[] = {
5452
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -60,12 +58,14 @@ static mp_obj_t adafruit_bus_device_i2cdevice_make_new(const mp_obj_type_t *type
6058

6159
mp_obj_t *i2c = args[ARG_i2c].u_obj;
6260

61+
adafruit_bus_device_i2cdevice_obj_t *self =
62+
mp_obj_malloc(adafruit_bus_device_i2cdevice_obj_t, &adafruit_bus_device_i2cdevice_type);
6363
common_hal_adafruit_bus_device_i2cdevice_construct(MP_OBJ_TO_PTR(self), i2c, args[ARG_device_address].u_int);
6464
if (args[ARG_probe].u_bool == true) {
6565
common_hal_adafruit_bus_device_i2cdevice_probe_for_device(self);
6666
}
6767

68-
return (mp_obj_t)self;
68+
return MP_OBJ_FROM_PTR(self);
6969
}
7070

7171
//| def __enter__(self) -> I2CDevice:

shared-bindings/adafruit_bus_device/spi_device/SPIDevice.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
//| ...
6060
//|
6161
static mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
62-
adafruit_bus_device_spidevice_obj_t *self =
63-
mp_obj_malloc(adafruit_bus_device_spidevice_obj_t, &adafruit_bus_device_spidevice_type);
6462
enum { ARG_spi, ARG_chip_select, ARG_cs_active_value, ARG_baudrate, ARG_polarity, ARG_phase, ARG_extra_clocks };
6563
static const mp_arg_t allowed_args[] = {
6664
{ MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -78,12 +76,11 @@ static mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type
7876

7977
mp_arg_validate_type_or_none(args[ARG_chip_select].u_obj, &digitalio_digitalinout_type, MP_QSTR_chip_select);
8078

81-
common_hal_adafruit_bus_device_spidevice_construct(MP_OBJ_TO_PTR(self), spi, args[ARG_chip_select].u_obj, args[ARG_cs_active_value].u_bool, args[ARG_baudrate].u_int, args[ARG_polarity].u_int,
82-
args[ARG_phase].u_int, args[ARG_extra_clocks].u_int);
83-
8479
if (args[ARG_chip_select].u_obj != mp_const_none) {
85-
digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(MP_OBJ_TO_PTR(args[ARG_chip_select].u_obj),
86-
true, DRIVE_MODE_PUSH_PULL);
80+
digitalinout_result_t result =
81+
common_hal_digitalio_digitalinout_switch_to_output(MP_OBJ_TO_PTR(args[ARG_chip_select].u_obj),
82+
true,
83+
DRIVE_MODE_PUSH_PULL);
8784
#if CIRCUITPY_DIGITALIO_HAVE_INPUT_ONLY
8885
if (result == DIGITALINOUT_INPUT_ONLY) {
8986
mp_raise_NotImplementedError(MP_ERROR_TEXT("Pin is input only"));
@@ -93,7 +90,19 @@ static mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type
9390
#endif
9491
}
9592

96-
return (mp_obj_t)self;
93+
adafruit_bus_device_spidevice_obj_t *self =
94+
mp_obj_malloc(adafruit_bus_device_spidevice_obj_t, &adafruit_bus_device_spidevice_type);
95+
common_hal_adafruit_bus_device_spidevice_construct(MP_OBJ_TO_PTR(self),
96+
spi,
97+
args[ARG_chip_select].u_obj,
98+
args[ARG_cs_active_value].u_bool,
99+
args[ARG_baudrate].u_int,
100+
args[ARG_polarity].u_int,
101+
args[ARG_phase].u_int,
102+
args[ARG_extra_clocks].u_int);
103+
104+
105+
return MP_OBJ_FROM_PTR(self);
97106
}
98107

99108
//| def __enter__(self) -> busio.SPI:

shared-bindings/aesio/aes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@
5555

5656
static mp_obj_t aesio_aes_make_new(const mp_obj_type_t *type, size_t n_args,
5757
size_t n_kw, const mp_obj_t *all_args) {
58-
aesio_aes_obj_t *self = mp_obj_malloc(aesio_aes_obj_t, &aesio_aes_type);
59-
6058
enum { ARG_key, ARG_mode, ARG_IV, ARG_counter, ARG_segment_size };
6159
static const mp_arg_t allowed_args[] = {
6260
{MP_QSTR_key, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
@@ -100,8 +98,10 @@ static mp_obj_t aesio_aes_make_new(const mp_obj_type_t *type, size_t n_args,
10098
iv = bufinfo.buf;
10199
}
102100

101+
aesio_aes_obj_t *self = mp_obj_malloc(aesio_aes_obj_t, &aesio_aes_type);
103102
common_hal_aesio_aes_construct(self, key, key_length, iv, mode,
104103
args[ARG_counter].u_int);
104+
105105
return MP_OBJ_FROM_PTR(self);
106106
}
107107

shared-bindings/alarm/pin/PinAlarm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
//| ...
4343
//|
4444
static mp_obj_t alarm_pin_pinalarm_make_new(const mp_obj_type_t *type, mp_uint_t n_args, size_t n_kw, const mp_obj_t *all_args) {
45-
alarm_pin_pinalarm_obj_t *self = mp_obj_malloc(alarm_pin_pinalarm_obj_t, &alarm_pin_pinalarm_type);
4645
enum { ARG_pin, ARG_value, ARG_edge, ARG_pull };
4746
static const mp_arg_t allowed_args[] = {
4847
{ MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -55,6 +54,7 @@ static mp_obj_t alarm_pin_pinalarm_make_new(const mp_obj_type_t *type, mp_uint_t
5554

5655
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj, MP_QSTR_pin);
5756

57+
alarm_pin_pinalarm_obj_t *self = mp_obj_malloc(alarm_pin_pinalarm_obj_t, &alarm_pin_pinalarm_type);
5858
common_hal_alarm_pin_pinalarm_construct(self,
5959
pin,
6060
args[ARG_value].u_bool,

shared-bindings/alarm/time/TimeAlarm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ mp_obj_t MP_WEAK rtc_get_time_source_time(void) {
4646
//|
4747
static mp_obj_t alarm_time_timealarm_make_new(const mp_obj_type_t *type,
4848
size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
49-
alarm_time_timealarm_obj_t *self = mp_obj_malloc(alarm_time_timealarm_obj_t, &alarm_time_timealarm_type);
50-
5149
enum { ARG_monotonic_time, ARG_epoch_time };
5250
static const mp_arg_t allowed_args[] = {
5351
{ MP_QSTR_monotonic_time, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
@@ -92,6 +90,7 @@ static mp_obj_t alarm_time_timealarm_make_new(const mp_obj_type_t *type,
9290
mp_raise_ValueError(MP_ERROR_TEXT("Time is in the past."));
9391
}
9492

93+
alarm_time_timealarm_obj_t *self = mp_obj_malloc(alarm_time_timealarm_obj_t, &alarm_time_timealarm_type);
9594
common_hal_alarm_time_timealarm_construct(self, monotonic_time);
9695

9796
return MP_OBJ_FROM_PTR(self);

shared-bindings/alarm/touch/TouchAlarm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
//|
2727
static mp_obj_t alarm_touch_touchalarm_make_new(const mp_obj_type_t *type,
2828
size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
29-
alarm_touch_touchalarm_obj_t *self = mp_obj_malloc(alarm_touch_touchalarm_obj_t, &alarm_touch_touchalarm_type);
30-
3129
enum { ARG_pin };
3230
static const mp_arg_t allowed_args[] = {
3331
{ MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -38,6 +36,7 @@ static mp_obj_t alarm_touch_touchalarm_make_new(const mp_obj_type_t *type,
3836

3937
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj, MP_QSTR_pin);
4038

39+
alarm_touch_touchalarm_obj_t *self = mp_obj_malloc(alarm_touch_touchalarm_obj_t, &alarm_touch_touchalarm_type);
4140
common_hal_alarm_touch_touchalarm_construct(self, pin);
4241

4342
return MP_OBJ_FROM_PTR(self);

0 commit comments

Comments
 (0)