Skip to content

Commit b90ef2b

Browse files
committed
[5.5][RemoteMirror] Add NULL checks to Remote Mirror malloc calls.
These calls can fail when passed absurd sizes, which can happen when we try to read data that's corrupt or doesn't contain what we think it should. Fail gracefully instead of crashing. rdar://78210820
1 parent 0731f9e commit b90ef2b

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

include/swift/Remote/MemoryReader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class MemoryReader {
100100
virtual ReadBytesResult
101101
readBytes(RemoteAddress address, uint64_t size) {
102102
auto *Buf = malloc(size);
103+
if (!Buf)
104+
return ReadBytesResult{};
103105
ReadBytesResult Result(Buf, [](const void *ptr) {
104106
free(const_cast<void *>(ptr));
105107
});

include/swift/Remote/MetadataReader.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,9 @@ class MetadataReader {
18021802
MetadataRef _readMetadata(StoredPointer address, size_t sizeAfter) {
18031803
auto size = sizeAfter;
18041804
uint8_t *buffer = (uint8_t *) malloc(size);
1805+
if (!buffer)
1806+
return nullptr;
1807+
18051808
if (!Reader->readBytes(RemoteAddress(address), buffer, size)) {
18061809
free(buffer);
18071810
return nullptr;
@@ -2522,6 +2525,8 @@ class MetadataReader {
25222525
std::string readObjCProtocolName(StoredPointer Address) {
25232526
auto Size = sizeof(TargetObjCProtocolPrefix<Runtime>);
25242527
auto Buffer = (uint8_t *)malloc(Size);
2528+
if (!Buffer)
2529+
return std::string();
25252530
SWIFT_DEFER {
25262531
free(Buffer);
25272532
};

0 commit comments

Comments
 (0)