From 43881f933d0049d08aee6c51f72f407e3a7b5007 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 9 Jun 2017 11:20:53 -0700 Subject: 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 --- py/argcheck.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'py') 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 } } } -- cgit v1.2.3