Skip to content

Commit b61b533

Browse files
committed
[Builtin] Add _isValidAddress pointer-value check.
And adopt it for StringGuts, as it is more robust on 32-bit systems.
1 parent b8d8949 commit b61b533

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

stdlib/public/core/Builtin.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ internal func _class_getInstancePositiveExtentSize(_ theClass: AnyClass) -> Int
363363
#endif
364364
}
365365

366+
@_inlineable
367+
@_versioned
368+
internal
369+
func _isValidAddress(_ address: UInt) -> Bool {
370+
// TODO: define (and use) ABI max valid pointer value
371+
return address >= _swift_abi_LeastValidPointerValue
372+
}
373+
366374
//===--- Builtin.BridgeObject ---------------------------------------------===//
367375

368376
// TODO(<rdar://problem/34837023>): Get rid of superfluous UInt constructor

stdlib/public/core/StringGuts.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import SwiftShims
2929
// FIXME: what about ppc64 and s390x?
3030
//
3131

32-
33-
3432
@_fixed_layout
3533
public // FIXME
3634
struct _StringGuts {
@@ -78,7 +76,7 @@ extension _StringGuts {
7876
} else if _object.isUnmanaged {
7977
} else if _object.isCocoa {
8078
if _object.isContiguous {
81-
_sanityCheck(_otherBits != 0) // TODO: in ABI's address space
79+
_sanityCheck(_isValidAddress(_otherBits))
8280
} else {
8381
_sanityCheck(_otherBits == 0)
8482
}
@@ -238,7 +236,7 @@ extension _StringGuts {
238236
@inline(__always)
239237
get {
240238
_sanityCheck(_object.isContiguousCocoa)
241-
_sanityCheck(Int(bitPattern: _otherBits) >= 1) // TODO: ABI's min address
239+
_sanityCheck(_isValidAddress(_otherBits))
242240
return UnsafeRawPointer(
243241
bitPattern: _otherBits
244242
)._unsafelyUnwrappedUnchecked

0 commit comments

Comments
 (0)