Skip to content

Runtime: Fix undersized allocation for out-of-line optional cast result. #4030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions stdlib/public/runtime/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2675,11 +2675,22 @@ static bool _dynamicCastClassToValueViaObjCBridgeable(
swift_unknownRetain(srcObject);
}

// The extra byte is for the tag.
auto targetSize = targetType->getValueWitnesses()->size + 1;
auto targetAlignMask = targetType->getValueWitnesses()->getAlignmentMask();

// Object that frees a buffer when it goes out of scope.
struct FreeBuffer {
void *Buffer = nullptr;
~FreeBuffer() { free(Buffer); }
} freeBuffer;
size_t size, alignMask;
FreeBuffer(size_t size, size_t alignMask) :
size(size), alignMask(alignMask) {}

~FreeBuffer() {
if (Buffer)
swift_slowDealloc(Buffer, size, alignMask);
}
} freeBuffer{targetSize, targetAlignMask};

// Allocate a buffer to store the T? returned by bridging.
// The extra byte is for the tag.
Expand All @@ -2691,7 +2702,7 @@ static bool _dynamicCastClassToValueViaObjCBridgeable(
optDestBuffer = inlineBuffer;
} else {
// Allocate a buffer.
optDestBuffer = malloc(targetType->getValueWitnesses()->size);
optDestBuffer = swift_slowAlloc(targetSize, targetAlignMask);
freeBuffer.Buffer = optDestBuffer;
}

Expand Down