From 5aedca6e1b940552f69a4e4e7ab731bf0cec8fc6 Mon Sep 17 00:00:00 2001 From: Tony Parker Date: Mon, 18 Mar 2024 16:13:01 -0700 Subject: [PATCH] Use a CShim to avoid an unescapable warning about using mktemp --- .../Data/Data+Writing.swift | 2 +- Sources/_CShims/data_shims.c | 21 +++++++++++++++++++ Sources/_CShims/include/data_shims.h | 20 ++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 Sources/_CShims/data_shims.c create mode 100644 Sources/_CShims/include/data_shims.h diff --git a/Sources/FoundationEssentials/Data/Data+Writing.swift b/Sources/FoundationEssentials/Data/Data+Writing.swift index 1781d4325..f6dabe0ac 100644 --- a/Sources/FoundationEssentials/Data/Data+Writing.swift +++ b/Sources/FoundationEssentials/Data/Data+Writing.swift @@ -158,7 +158,7 @@ private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL, // The warning diligently tells us we shouldn't be using mktemp() because blindly opening the returned path opens us up to a TOCTOU race. However, in this case, we're being careful by doing O_CREAT|O_EXCL and repeating, just like the implementation of mkstemp. // Furthermore, we can't compatibly switch to mkstemp() until we have the ability to set fchmod correctly, which requires the ability to query the current umask, which we don't have. (22033100) - guard mktemp(templateFileSystemRep) != nil else { + guard _datashims_mktemp(templateFileSystemRep) != nil else { throw CocoaError.errorWithFilePath(inPath, errno: errno, reading: false) } diff --git a/Sources/_CShims/data_shims.c b/Sources/_CShims/data_shims.c new file mode 100644 index 000000000..b1ea205ab --- /dev/null +++ b/Sources/_CShims/data_shims.c @@ -0,0 +1,21 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#include +#include "data_shims.h" + +INTERNAL char *_datashims_mktemp(char *arg) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated" + return mktemp(arg); +#pragma GCC diagnostic pop +} diff --git a/Sources/_CShims/include/data_shims.h b/Sources/_CShims/include/data_shims.h new file mode 100644 index 000000000..881f1892a --- /dev/null +++ b/Sources/_CShims/include/data_shims.h @@ -0,0 +1,20 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#ifndef CSHIMS_DATA_H +#define CSHIMS_DATA_H + +#include "_CShimsMacros.h" + +INTERNAL char *_datashims_mktemp(char *arg); + +#endif