Skip to content

Commit 6dbfba5

Browse files
committed
linux: Doc and check retval for no-fail pid calls
The switch from @bitcast() to @intcast() here safety-checks Linux's assertion that these 3 calls never return errors (negative values as pid_t). getppid() can legally return 0 if the parent is in a different pid namespace, but this is not an error.
1 parent 04071d6 commit 6dbfba5

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/std/os/linux.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,15 +1839,18 @@ pub fn setsid() usize {
18391839
}
18401840

18411841
pub fn getpid() pid_t {
1842-
return @bitCast(@as(u32, @truncate(syscall0(.getpid))));
1842+
// Casts result to a pid_t, safety-checking >= 0, because getpid() cannot fail
1843+
return @intCast(@as(u32, @truncate(syscall0(.getpid))));
18431844
}
18441845

18451846
pub fn getppid() pid_t {
1846-
return @bitCast(@as(u32, @truncate(syscall0(.getppid))));
1847+
// Casts result to a pid_t, safety-checking >= 0, because getppid() cannot fail
1848+
return @intCast(@as(u32, @truncate(syscall0(.getppid))));
18471849
}
18481850

18491851
pub fn gettid() pid_t {
1850-
return @bitCast(@as(u32, @truncate(syscall0(.gettid))));
1852+
// Casts result to a pid_t, safety-checking >= 0, because gettid() cannot fail
1853+
return @intCast(@as(u32, @truncate(syscall0(.gettid))));
18511854
}
18521855

18531856
pub fn sigprocmask(flags: u32, noalias set: ?*const sigset_t, noalias oldset: ?*sigset_t) usize {

0 commit comments

Comments
 (0)