Skip to content

Commit

Permalink
Use a CShim to avoid an unescapable warning about using mktemp
Browse files Browse the repository at this point in the history
  • Loading branch information
parkera committed Mar 18, 2024
1 parent a04d992 commit 94cd4a2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/Data/Data+Writing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 _data_mktemp(templateFileSystemRep) != nil else {
throw CocoaError.errorWithFilePath(inPath, errno: errno, reading: false)
}

Expand Down
21 changes: 21 additions & 0 deletions Sources/_CShims/data_shims.c
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
#include "data_shims.h"

INTERNAL char *_data_mktemp(char *arg) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
return mktemp(arg);
#pragma GCC diagnostic pop
}
20 changes: 20 additions & 0 deletions Sources/_CShims/include/data_shims.h
Original file line number Diff line number Diff line change
@@ -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 *_data_mktemp(char *arg);

#endif

0 comments on commit 94cd4a2

Please sign in to comment.