Skip to content

Commit 22979b9

Browse files
committed
fix an uninitialized pointer in BridgedSourceLoc
It's so easy to run into undefined behavior in C++
1 parent 0cf8e4c commit 22979b9

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

SwiftCompilerSources/Sources/Basic/SourceLoc.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public struct SourceLoc {
2727
guard bridged.isValid() else {
2828
return nil
2929
}
30-
self.locationInFile = bridged.opaquePointer!
30+
self.locationInFile = bridged.uint8Pointer()!
3131
}
3232

3333
public var bridged: BridgedSourceLoc {
34-
.init(opaquePointer: locationInFile)
34+
.init(locationInFile)
3535
}
3636
}
3737

include/swift/Basic/BasicBridging.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,19 @@ class BridgedOwnedString {
8888
void destroy() const;
8989
};
9090

91-
struct BridgedSourceLoc {
92-
const uint8_t * _Nullable opaquePointer;
91+
class BridgedSourceLoc {
92+
const void * _Nullable opaquePointer;
93+
public:
94+
BridgedSourceLoc() : opaquePointer(nullptr) {}
95+
BridgedSourceLoc(const void * _Nullable loc) : opaquePointer(loc) {}
96+
9397
bool isValid() const { return opaquePointer != nullptr; }
98+
SWIFT_IMPORT_UNSAFE const uint8_t * _Nullable uint8Pointer() const {
99+
return (const uint8_t * _Nullable)opaquePointer;
100+
}
101+
const char * _Nullable getLoc() const {
102+
return (const char * _Nullable)opaquePointer;
103+
}
94104
};
95105

96106
struct BridgedArrayRef {

lib/AST/ASTBridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static_assert(sizeof(BridgedDiagnosticFixIt) >= sizeof(DiagnosticInfo::FixIt),
3838
"BridgedDiagnosticArgument has wrong size");
3939

4040
static SourceLoc getSourceLoc(BridgedSourceLoc bridgedLoc) {
41-
return SourceLoc(llvm::SMLoc::getFromPointer((const char *)bridgedLoc.opaquePointer));
41+
return SourceLoc(llvm::SMLoc::getFromPointer(bridgedLoc.getLoc()));
4242
}
4343

4444
BridgedDiagnosticFixIt::BridgedDiagnosticFixIt(BridgedSourceLoc start, uint32_t length, BridgedStringRef text)
@@ -60,7 +60,7 @@ void DiagnosticEngine_diagnose(
6060
for (auto arg : getArrayRef<BridgedDiagnosticArgument>(bridgedArguments)) {
6161
arguments.push_back(arg.get());
6262
}
63-
auto inflight = D->diagnose(SourceLoc(llvm::SMLoc::getFromPointer((const char *)loc.opaquePointer)), diagID, arguments);
63+
auto inflight = D->diagnose(SourceLoc(llvm::SMLoc::getFromPointer(loc.getLoc())), diagID, arguments);
6464

6565
// Add highlight.
6666
if (highlightStart.isValid()) {

0 commit comments

Comments
 (0)