Skip to content

Commit baa6d55

Browse files
author
Vytautas Astrauskas
committed
In Thread::new, add a comment that a panic could cause a memory leak.
1 parent 5382347 commit baa6d55

File tree

5 files changed

+14
-5
lines changed

5 files changed

+14
-5
lines changed

src/libstd/sys/cloudabi/thread.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ impl Thread {
3131
assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);
3232

3333
let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
34+
// Note: if the thread creation fails and this assert fails, then p will
35+
// be leaked. However, an alternative design could cause double-free
36+
// which is clearly worse.
3437
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
3538

3639
return if ret != 0 {
3740
// The thread failed to start and as a result p was not consumed. Therefore, it is
3841
// safe to reconstruct the box so that it gets deallocated.
39-
let _ = Box::from_raw(p);
42+
drop(Box::from_raw(p));
4043
Err(io::Error::from_raw_os_error(ret))
4144
} else {
4245
Ok(Thread { id: native })

src/libstd/sys/hermit/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Thread {
6161
return if ret != 0 {
6262
// The thread failed to start and as a result p was not consumed. Therefore, it is
6363
// safe to reconstruct the box so that it gets deallocated.
64-
let _ = Box::from_raw(p);
64+
drop(Box::from_raw(p));
6565
Err(io::Error::new(io::ErrorKind::Other, "Unable to create thread!"))
6666
} else {
6767
Ok(Thread { tid: tid })

src/libstd/sys/unix/thread.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ impl Thread {
6464
};
6565

6666
let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
67+
// Note: if the thread creation fails and this assert fails, then p will
68+
// be leaked. However, an alternative design could cause double-free
69+
// which is clearly worse.
6770
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
6871

6972
return if ret != 0 {
7073
// The thread failed to start and as a result p was not consumed. Therefore, it is
7174
// safe to reconstruct the box so that it gets deallocated.
72-
let _ = Box::from_raw(p);
75+
drop(Box::from_raw(p));
7376
Err(io::Error::from_raw_os_error(ret))
7477
} else {
7578
Ok(Thread { id: native })

src/libstd/sys/vxworks/thread.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ impl Thread {
5252
};
5353

5454
let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
55+
// Note: if the thread creation fails and this assert fails, then p will
56+
// be leaked. However, an alternative design could cause double-free
57+
// which is clearly worse.
5558
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
5659

5760
return if ret != 0 {
5861
// The thread failed to start and as a result p was not consumed. Therefore, it is
5962
// safe to reconstruct the box so that it gets deallocated.
60-
let _ = Box::from_raw(p);
63+
drop(Box::from_raw(p));
6164
Err(io::Error::from_raw_os_error(ret))
6265
} else {
6366
Ok(Thread { id: native })

src/libstd/sys/windows/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Thread {
4141
return if ret as usize == 0 {
4242
// The thread failed to start and as a result p was not consumed. Therefore, it is
4343
// safe to reconstruct the box so that it gets deallocated.
44-
let _ = Box::from_raw(p);
44+
drop(Box::from_raw(p));
4545
Err(io::Error::last_os_error())
4646
} else {
4747
Ok(Thread { handle: Handle::new(ret) })

0 commit comments

Comments
 (0)