From 0c27a4c38cca76c4c366424e7244b934b0c25e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Laferri=C3=A8re?= Date: Wed, 4 Dec 2024 14:57:07 -0800 Subject: [PATCH] SwiftShims: memcmp should accept optional pointers on Darwin Darwin defines memcmp with optional pointers. Update SwiftShims to define it to the same type to avoid deserialization failures where we get one over the other and the types don't match anymore. rdar://140596571 --- stdlib/public/SwiftShims/swift/shims/LibcShims.h | 5 ++++- validation-test/ClangImporter/memcmp-definitions.swift | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/stdlib/public/SwiftShims/swift/shims/LibcShims.h b/stdlib/public/SwiftShims/swift/shims/LibcShims.h index 98cc9e2593530..f727e6cf71c7a 100644 --- a/stdlib/public/SwiftShims/swift/shims/LibcShims.h +++ b/stdlib/public/SwiftShims/swift/shims/LibcShims.h @@ -60,8 +60,11 @@ static inline __swift_size_t _swift_stdlib_strlen_unsigned(const unsigned char * SWIFT_READONLY static inline int _swift_stdlib_memcmp(const void *s1, const void *s2, __swift_size_t n) { +#if defined(__APPLE__) + // Darwin defines memcmp with optional pointers, preserve the same type here. + extern int memcmp(const void * _Nullable, const void * _Nullable, __swift_size_t); // FIXME: Is there a way to identify Glibc specifically? -#if (defined(__gnu_linux__) || defined(__ANDROID__)) && !defined(__musl__) +#elif (defined(__gnu_linux__) || defined(__ANDROID__)) && !defined(__musl__) extern int memcmp(const void * _Nonnull, const void * _Nonnull, __swift_size_t); #else extern int memcmp(const void * _Null_unspecified, const void * _Null_unspecified, __swift_size_t); diff --git a/validation-test/ClangImporter/memcmp-definitions.swift b/validation-test/ClangImporter/memcmp-definitions.swift index 2d0c0df212442..13e6abba38e33 100644 --- a/validation-test/ClangImporter/memcmp-definitions.swift +++ b/validation-test/ClangImporter/memcmp-definitions.swift @@ -1,4 +1,5 @@ -/// rdar://69876253 +/// Darwin's memcmp accepts nullable pointers, make sure the SwiftShims one +/// preserves the same type. // REQUIRES: VENDOR=apple // RUN: %target-build-swift %s -o %t.out @@ -9,4 +10,6 @@ func foo () { let a = UnsafeMutableRawPointer.allocate(byteCount: 4, alignment: 4) let b = UnsafeMutableRawPointer.allocate(byteCount: 4, alignment: 4) memcmp(a, b, 4) + + memcmp(nil, nil, 0) }