From 6ee1e383d6b95d0bb5f2902ec91b8d831e4b5803 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 4 Jan 2014 03:47:34 +0200 Subject: [PATCH 1/3] str slice: Trim slice indexes to be in range. --- py/objstr.c | 11 +++++++++++ tests/basics/tests/slice-bstr1.py | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/py/objstr.c b/py/objstr.c index 59547e3cd6..54e6f37705 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -41,9 +41,20 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { int len = strlen(lhs_str); if (start < 0) { start = len + start; + if (start < 0) { + start = 0; + } + } else if (start > len) { + start = len; } if (stop <= 0) { stop = len + stop; + // CPython returns empty string in such case + if (stop < 0) { + stop = start; + } + } else if (stop > len) { + stop = len; } return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start)); #endif diff --git a/tests/basics/tests/slice-bstr1.py b/tests/basics/tests/slice-bstr1.py index 0bed959141..74dbc20617 100644 --- a/tests/basics/tests/slice-bstr1.py +++ b/tests/basics/tests/slice-bstr1.py @@ -22,6 +22,11 @@ print(b"123"[0:]) print(b"123"[:0]) print(b"123"[:-3]) print(b"123"[:-4]) +# Range check testing, don't segfault, please ;-) +print(b"123"[:1000000]) +print(b"123"[1000000:]) +print(b"123"[:-1000000]) +print(b"123"[-1000000:]) # No IndexError! print(b""[1:1]) print(b""[-1:-1]) From 2c62e262b2c84b077355d67c6302bd02f6a28552 Mon Sep 17 00:00:00 2001 From: stevie67 Date: Sat, 4 Jan 2014 03:02:32 +0100 Subject: [PATCH 2/3] Fix issue #62: Cache loses data Use the storage cache not only for writing but also for reading. This avoids reading stale data and thus data loss. --- stm/storage.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/stm/storage.c b/stm/storage.c index 6878de22e6..7342a53ac3 100644 --- a/stm/storage.c +++ b/stm/storage.c @@ -49,6 +49,16 @@ static uint8_t *cache_get_addr_for_write(uint32_t flash_addr) { return (uint8_t*)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start; } +static uint8_t *cache_get_addr_for_read(uint32_t flash_addr) { + uint32_t flash_sector_start; + uint32_t flash_sector_size; + uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size); + if (cache_flash_sector_id == flash_sector_id) + return (uint8_t*)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start; + // not in cache, copy straight from flash + return (uint8_t*)flash_addr; +} + void storage_init(void) { if (!is_initialised) { cache_flash_sector_id = 0; @@ -131,8 +141,8 @@ bool storage_read_block(uint8_t *dest, uint32_t block) { return true; } else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) { - // non-MBR block, just copy straight from flash - uint8_t *src = (uint8_t*)FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK) * BLOCK_SIZE; + uint32_t flash_addr = FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK) * BLOCK_SIZE; + uint8_t *src = cache_get_addr_for_read(flash_addr); memcpy(dest, src, BLOCK_SIZE); return true; From 2a5e6538b941d1fc9c1c0ef0bb0827b5ed2425d2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 4 Jan 2014 12:34:36 +0000 Subject: [PATCH 3/3] stm: Add comments for storage read from cache. --- stm/storage.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/stm/storage.c b/stm/storage.c index 7342a53ac3..daee4adb5e 100644 --- a/stm/storage.c +++ b/stm/storage.c @@ -53,8 +53,10 @@ static uint8_t *cache_get_addr_for_read(uint32_t flash_addr) { uint32_t flash_sector_start; uint32_t flash_sector_size; uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size); - if (cache_flash_sector_id == flash_sector_id) + if (cache_flash_sector_id == flash_sector_id) { + // in cache, copy from there return (uint8_t*)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start; + } // not in cache, copy straight from flash return (uint8_t*)flash_addr; } @@ -141,6 +143,7 @@ bool storage_read_block(uint8_t *dest, uint32_t block) { return true; } else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) { + // non-MBR block, get data from flash memory, possibly via cache uint32_t flash_addr = FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK) * BLOCK_SIZE; uint8_t *src = cache_get_addr_for_read(flash_addr); memcpy(dest, src, BLOCK_SIZE);