From f98bb2ddcbfed7033ae93ae8fe98b0540a9fb42f Mon Sep 17 00:00:00 2001 From: Delio Brignoli Date: Sat, 20 Aug 2016 10:51:28 +0200 Subject: py/mpprint: Fail an assertion with unsupported format specifiers. Arguments of an unknown type cannot be skipped and continuing to parse a format string after encountering an unknown format specifier leads to undefined behaviour. This patch helps to find use of unsupported formats. --- unix/coverage.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'unix/coverage.c') diff --git a/unix/coverage.c b/unix/coverage.c index 9d5372554..ce1da50e2 100644 --- a/unix/coverage.c +++ b/unix/coverage.c @@ -12,7 +12,6 @@ STATIC mp_obj_t extra_coverage(void) { // mp_printf (used by ports that don't have a native printf) { mp_printf(&mp_plat_print, "# mp_printf\n"); - mp_printf(&mp_plat_print, "%"); // nothing after percent mp_printf(&mp_plat_print, "%d %+d % d\n", -123, 123, 123); // sign mp_printf(&mp_plat_print, "%05d\n", -123); // negative number with zero padding mp_printf(&mp_plat_print, "%ld\n", 123); // long @@ -21,7 +20,6 @@ STATIC mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "%.*s\n", -1, "abc"); // negative string precision mp_printf(&mp_plat_print, "%b %b\n", 0, 1); // bools mp_printf(&mp_plat_print, "%s\n", NULL); // null string - mp_printf(&mp_plat_print, "%t\n"); // non-format char mp_printf(&mp_plat_print, "%d\n", 0x80000000); // should print signed mp_printf(&mp_plat_print, "%u\n", 0x80000000); // should print unsigned mp_printf(&mp_plat_print, "%x\n", 0x80000000); // should print unsigned -- cgit v1.2.3 From 58f386135813ab6e55641bec6eae32cc0fd35115 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 2 Sep 2016 15:07:42 +1000 Subject: tests/unix/extra_coverage: Add test for str/bytes with invalid hash. --- tests/unix/extra_coverage.py | 9 ++++++++- tests/unix/extra_coverage.py.exp | 5 +++++ unix/coverage.c | 9 ++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) (limited to 'unix/coverage.c') diff --git a/tests/unix/extra_coverage.py b/tests/unix/extra_coverage.py index 8f330f1da..72bcc9994 100644 --- a/tests/unix/extra_coverage.py +++ b/tests/unix/extra_coverage.py @@ -5,4 +5,11 @@ except NameError: import sys sys.exit() -extra_coverage() +data = extra_coverage() + +# test hashing of str/bytes that have an invalid hash +print(data) +print(hash(data[0])) +print(hash(data[1])) +print(hash(bytes(data[0], 'utf8'))) +print(hash(str(data[1], 'utf8'))) diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 44749c45e..ea73a54e4 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -35,3 +35,8 @@ ementation 12345678 0 0 +('0123456789', b'0123456789') +7300 +7300 +7300 +7300 diff --git a/unix/coverage.c b/unix/coverage.c index ce1da50e2..c84a653f7 100644 --- a/unix/coverage.c +++ b/unix/coverage.c @@ -1,12 +1,17 @@ #include #include "py/obj.h" +#include "py/objstr.h" #include "py/runtime.h" #include "py/repl.h" #include "py/mpz.h" #if defined(MICROPY_UNIX_COVERAGE) +// str/bytes objects without a valid hash +STATIC const mp_obj_str_t str_no_hash_obj = {{&mp_type_str}, 0, 10, (const byte*)"0123456789"}; +STATIC const mp_obj_str_t bytes_no_hash_obj = {{&mp_type_bytes}, 0, 10, (const byte*)"0123456789"}; + // function to run extra tests for things that can't be checked by scripts STATIC mp_obj_t extra_coverage(void) { // mp_printf (used by ports that don't have a native printf) @@ -109,7 +114,9 @@ STATIC mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value)); } - return mp_const_none; + // return a tuple of data for testing on the Python side + mp_obj_t items[] = {(mp_obj_t)&str_no_hash_obj, (mp_obj_t)&bytes_no_hash_obj}; + return mp_obj_new_tuple(MP_ARRAY_SIZE(items), items); } MP_DEFINE_CONST_FUN_OBJ_0(extra_coverage_obj, extra_coverage); -- cgit v1.2.3