py/obj.h: Fix mp_seq_replace_slice_no_grow to use memmove not memcpy.

Because the argument arrays may overlap, as show by the new tests in this
commit.

Also remove the debugging comments for these macros, add a new comment
about overlapping regions, and separate the macros by blank lines to make
them easier to read.

Fixes issue #6244.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2020-07-16 23:23:42 +10:00
parent 895b1dbdda
commit a853fff838
2 changed files with 28 additions and 4 deletions

View File

@ -992,17 +992,17 @@ bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, size_t len1, const mp
mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args);
mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value);
mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
// Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
#define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte *)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
// Note: dest and slice regions may overlap
#define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_sz) \
/*printf("memcpy(%p, %p, %d)\n", dest + beg, slice, slice_len * (item_sz));*/ \
memcpy(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \
/*printf("memmove(%p, %p, %d)\n", dest + (beg + slice_len), dest + end, (dest_len - end) * (item_sz));*/ \
memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \
memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), (dest_len - end) * (item_sz));
// Note: dest and slice regions may overlap
#define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_sz) \
/*printf("memmove(%p, %p, %d)\n", dest + beg + len_adj, dest + beg, (dest_len - beg) * (item_sz));*/ \
memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \
memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));

View File

@ -61,3 +61,27 @@ try:
memoryview(array.array('i'))[0:2] = b'1234'
except ValueError:
print('ValueError')
# test shift left of bytearray
b = bytearray(range(10))
mv = memoryview(b)
mv[1:] = mv[:-1]
print(b)
# test shift right of bytearray
b = bytearray(range(10))
mv = memoryview(b)
mv[:-1] = mv[1:]
print(b)
# test shift left of array
a = array.array('I', range(10))
mv = memoryview(a)
mv[1:] = mv[:-1]
print(a)
# test shift right of array
a = array.array('I', range(10))
mv = memoryview(a)
mv[:-1] = mv[1:]
print(a)