diff options
| author | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-06-09 11:20:53 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-06-09 11:20:53 -0700 |
| commit | 43881f933d0049d08aee6c51f72f407e3a7b5007 (patch) | |
| tree | b5b47461596d10f8bf4a06eda572efbb1391c394 /py | |
| parent | a884acc7f655e7383bbde16ccda4d266ccb4eb8b (diff) | |
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 #154
Diffstat (limited to 'py')
| -rw-r--r-- | py/argcheck.c | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/py/argcheck.c b/py/argcheck.c index 6da62151d..d639d08fb 100644 --- a/py/argcheck.c +++ b/py/argcheck.c @@ -31,43 +31,46 @@ #include "py/runtime.h" 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) { + // NOTE(tannewt): This prevents this function from being optimized away. + // Without it, functions can crash when reading invalid args. + asm (""); // TODO maybe take the function name as an argument so we can print nicer error messages if (n_kw && !takes_kw) { - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { + #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); - } else { + #else mp_raise_TypeError("function does not take keyword arguments"); - } + #endif } if (n_args_min == n_args_max) { if (n_args != n_args_min) { - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { + #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); - } else { + #else mp_raise_TypeError_varg( "function takes %d positional arguments but %d were given", n_args_min, n_args); - } + #endif } } else { if (n_args < n_args_min) { - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { + #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); - } else { + #else mp_raise_TypeError_varg( "function missing %d required positional arguments", n_args_min - n_args); - } + #endif } else if (n_args > n_args_max) { - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { + #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); - } else { + #else mp_raise_TypeError_varg( "function expected at most %d arguments, got %d", n_args_max, n_args); - } + #endif } } } |
