Skip to content

Commit cd19886

Browse files
committed
[Demangling] Avoid UB call to memcpy().
When appending to an empty `CharVector`, we were inadvertently relying on `memcpy(NewObjects, NULL, 0)` being a no-op, whereas in practice the C standard says it's UB because of the `NULL` pointer. Fix by only calling `memcpy()` if we actually have bytes to copy. rdar://114447171
1 parent 6de8597 commit cd19886

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

include/swift/Demangling/Demangler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ class NodeFactory {
206206
if (Growth < Capacity * 2)
207207
Growth = Capacity * 2;
208208
T *NewObjects = Allocate<T>(Capacity + Growth);
209-
memcpy(NewObjects, Objects, OldAllocSize);
209+
if (OldAllocSize)
210+
memcpy(NewObjects, Objects, OldAllocSize);
210211
Objects = NewObjects;
211212
Capacity += Growth;
212213
}

0 commit comments

Comments
 (0)