Skip to content

Commit 67e51c0

Browse files
ChrisJeffersonfingolfin
authored andcommitted
Fix overlapping memcpy in APPEND_LIST
1 parent 7f93980 commit 67e51c0

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/listfunc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ Obj FuncAPPEND_LIST_INTR (
288288
SET_LEN_STRING(list1, len1 + len2);
289289
CLEAR_FILTS_LIST(list1);
290290
// copy data, including terminating zero byte
291-
memcpy(CHARS_STRING(list1) + len1, CHARS_STRING(list2), len2 + 1);
291+
// Can't use memcpy, in case list1 == list2
292+
SyMemmove(CHARS_STRING(list1) + len1, CHARS_STRING(list2), len2 + 1);
292293
return (Obj) 0;
293294
}
294295

tst/testinstall/listindex.tst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ gap> l := "cheese";; Append(l, [true]); l;
183183
[ 'c', 'h', 'e', 'e', 's', 'e', true ]
184184
gap> l := "cheese";; Append(l, []); l;
185185
"cheese"
186+
gap> Append(l, l); l;
187+
"cheesecheese"
188+
gap> l := "chee";; Append(l, l); l;
189+
"cheechee"
190+
gap> l := "cheeseXX";; Append(l, l); l;
191+
"cheeseXXcheeseXX"
192+
gap> l := [true];; Append(l, l); l;
193+
[ true, true ]
194+
gap> Append(l,l); l;
195+
[ true, true, true, true ]
196+
gap> l := [];; Append(l,l); l;
197+
[ ]
198+
gap> l := [1,2,3,4];; Append(l,l); l;
199+
[ 1, 2, 3, 4, 1, 2, 3, 4 ]
186200
gap> Append(Immutable([1,2,3]), [1,2,3]);
187201
Error, Append: <list1> must be a mutable list
188202
gap> Append([1,2,3], () );

0 commit comments

Comments
 (0)