Skip to content

Commit 43881f9

Browse files
committed
py: Prevent mp_arg_check_num from being optimized away by the compiler.
Also, change the MICROPY_ERROR_REPORTING checks to macros to make it clear the compiler can handle it immediately. Fixes adafruit#154
1 parent a884acc commit 43881f9

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

py/argcheck.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,46 @@
3131
#include "py/runtime.h"
3232

3333
void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw) {
34+
// NOTE(tannewt): This prevents this function from being optimized away.
35+
// Without it, functions can crash when reading invalid args.
36+
asm ("");
3437
// TODO maybe take the function name as an argument so we can print nicer error messages
3538

3639
if (n_kw && !takes_kw) {
37-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
40+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
3841
mp_arg_error_terse_mismatch();
39-
} else {
42+
#else
4043
mp_raise_TypeError("function does not take keyword arguments");
41-
}
44+
#endif
4245
}
4346

4447
if (n_args_min == n_args_max) {
4548
if (n_args != n_args_min) {
46-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
49+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
4750
mp_arg_error_terse_mismatch();
48-
} else {
51+
#else
4952
mp_raise_TypeError_varg(
5053
"function takes %d positional arguments but %d were given",
5154
n_args_min, n_args);
52-
}
55+
#endif
5356
}
5457
} else {
5558
if (n_args < n_args_min) {
56-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
59+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
5760
mp_arg_error_terse_mismatch();
58-
} else {
61+
#else
5962
mp_raise_TypeError_varg(
6063
"function missing %d required positional arguments",
6164
n_args_min - n_args);
62-
}
65+
#endif
6366
} else if (n_args > n_args_max) {
64-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
67+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
6568
mp_arg_error_terse_mismatch();
66-
} else {
69+
#else
6770
mp_raise_TypeError_varg(
6871
"function expected at most %d arguments, got %d",
6972
n_args_max, n_args);
70-
}
73+
#endif
7174
}
7275
}
7376
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy