Skip to content

Commit 64a97fc

Browse files
committed
Use withUnsafeBytes(of:) properly for getting the address of a local variable
1 parent cd75691 commit 64a97fc

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,11 @@ public struct TaskGroup<ChildTaskResult> {
246246
flags.addPendingGroupTaskUnconditionally = true
247247

248248
var groupOption = TaskOptionRecord.TaskGroup(group: _group)
249-
250-
// Create the asynchronous task future.
251-
_ = Builtin.createAsyncTask(flags.bits, UnsafeRawPointer(&groupOption)._rawValue, operation)
249+
withUnsafeBytes(of: &groupOption) { optionsBuffer in
250+
// Create the asynchronous task future.
251+
_ = Builtin.createAsyncTask(
252+
flags.bits, optionsBuffer.baseAddress?._rawValue, operation)
253+
}
252254
}
253255

254256
/// Add a child task to the group.
@@ -283,9 +285,11 @@ public struct TaskGroup<ChildTaskResult> {
283285
flags.enqueueJob = true
284286

285287
var groupOption = TaskOptionRecord.TaskGroup(group: _group)
286-
287-
// Create the asynchronous task future.
288-
_ = Builtin.createAsyncTask(flags.bits, UnsafeRawPointer(&groupOption)._rawValue, operation)
288+
withUnsafeBytes(of: &groupOption) { optionsBuffer in
289+
// Create the asynchronous task future.
290+
_ = Builtin.createAsyncTask(
291+
flags.bits, optionsBuffer.baseAddress?._rawValue, operation)
292+
}
289293

290294
return true
291295
}
@@ -463,9 +467,11 @@ public struct ThrowingTaskGroup<ChildTaskResult, Failure: Error> {
463467
flags.addPendingGroupTaskUnconditionally = true
464468

465469
var groupOption = TaskOptionRecord.TaskGroup(group: _group)
466-
467-
// Create the asynchronous task future.
468-
_ = Builtin.createAsyncTask(flags.bits, UnsafeRawPointer(&groupOption)._rawValue, operation)
470+
withUnsafeBytes(of: &groupOption) { optionsBuffer in
471+
// Create the asynchronous task future.
472+
_ = Builtin.createAsyncTask(
473+
flags.bits, optionsBuffer.baseAddress?._rawValue, operation)
474+
}
469475
}
470476

471477
/// Add a child task to the group.
@@ -500,9 +506,11 @@ public struct ThrowingTaskGroup<ChildTaskResult, Failure: Error> {
500506
flags.enqueueJob = true
501507

502508
var groupOption = TaskOptionRecord.TaskGroup(group: _group)
503-
504-
// Create the asynchronous task future.
505-
_ = Builtin.createAsyncTask(flags.bits, UnsafeRawPointer(&groupOption)._rawValue, operation)
509+
withUnsafeBytes(of: &groupOption) { optionsBuffer in
510+
// Create the asynchronous task future.
511+
_ = Builtin.createAsyncTask(
512+
flags.bits, optionsBuffer.baseAddress?._rawValue, operation)
513+
}
506514

507515
return true
508516
}

0 commit comments

Comments
 (0)