Skip to content

Commit a4af1a7

Browse files
committed
Use withUnsafeBytes(of:) properly for getting the address of a local variable
1 parent 76959b1 commit a4af1a7

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
@@ -218,9 +218,11 @@ public struct TaskGroup<ChildTaskResult> {
218218
flags.addPendingGroupTaskUnconditionally = true
219219

220220
var groupOption = TaskOptionRecord.TaskGroup(group: _group)
221-
222-
// Create the asynchronous task future.
223-
_ = Builtin.createAsyncTask(flags.bits, UnsafeRawPointer(&groupOption)._rawValue, operation)
221+
withUnsafeBytes(of: &groupOption) { optionsBuffer in
222+
// Create the asynchronous task future.
223+
_ = Builtin.createAsyncTask(
224+
flags.bits, optionsBuffer.baseAddress?._rawValue, operation)
225+
}
224226
}
225227

226228
/// Add a child task to the group.
@@ -255,9 +257,11 @@ public struct TaskGroup<ChildTaskResult> {
255257
flags.enqueueJob = true
256258

257259
var groupOption = TaskOptionRecord.TaskGroup(group: _group)
258-
259-
// Create the asynchronous task future.
260-
_ = Builtin.createAsyncTask(flags.bits, UnsafeRawPointer(&groupOption)._rawValue, operation)
260+
withUnsafeBytes(of: &groupOption) { optionsBuffer in
261+
// Create the asynchronous task future.
262+
_ = Builtin.createAsyncTask(
263+
flags.bits, optionsBuffer.baseAddress?._rawValue, operation)
264+
}
261265

262266
return true
263267
}
@@ -443,9 +447,11 @@ public struct ThrowingTaskGroup<ChildTaskResult, Failure: Error> {
443447
flags.addPendingGroupTaskUnconditionally = true
444448

445449
var groupOption = TaskOptionRecord.TaskGroup(group: _group)
446-
447-
// Create the asynchronous task future.
448-
_ = Builtin.createAsyncTask(flags.bits, UnsafeRawPointer(&groupOption)._rawValue, operation)
450+
withUnsafeBytes(of: &groupOption) { optionsBuffer in
451+
// Create the asynchronous task future.
452+
_ = Builtin.createAsyncTask(
453+
flags.bits, optionsBuffer.baseAddress?._rawValue, operation)
454+
}
449455
}
450456

451457
/// Add a child task to the group.
@@ -480,9 +486,11 @@ public struct ThrowingTaskGroup<ChildTaskResult, Failure: Error> {
480486
flags.enqueueJob = true
481487

482488
var groupOption = TaskOptionRecord.TaskGroup(group: _group)
483-
484-
// Create the asynchronous task future.
485-
_ = Builtin.createAsyncTask(flags.bits, UnsafeRawPointer(&groupOption)._rawValue, operation)
489+
withUnsafeBytes(of: &groupOption) { optionsBuffer in
490+
// Create the asynchronous task future.
491+
_ = Builtin.createAsyncTask(
492+
flags.bits, optionsBuffer.baseAddress?._rawValue, operation)
493+
}
486494

487495
return true
488496
}

0 commit comments

Comments
 (0)