Skip to content

Commit dedaed3

Browse files
committed
Fix incorrect posix_spawn* error checking
The posix_spawn functions do not use `-1` as the sentinel for errors, rather `0` is return on success and every other return value is an error. Adds a test case to test posix_spawn failure.
1 parent a0869f9 commit dedaed3

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

src/spawn.rs

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ impl PosixSpawnAttr {
2525
let mut attr = mem::MaybeUninit::uninit();
2626
let res = unsafe { libc::posix_spawnattr_init(attr.as_mut_ptr()) };
2727

28-
Errno::result(res)?;
28+
if res != 0 {
29+
return Err(Errno::from_raw(res));
30+
}
2931

3032
let attr = unsafe { attr.assume_init() };
3133
Ok(PosixSpawnAttr { attr })
@@ -43,14 +45,18 @@ impl PosixSpawnAttr {
4345
&mut self.attr as *mut libc::posix_spawnattr_t,
4446
)
4547
};
46-
Errno::result(res)?;
48+
if res != 0 {
49+
return Err(Errno::from_raw(res));
50+
}
4751

4852
let res = unsafe {
4953
libc::posix_spawnattr_init(
5054
&mut self.attr as *mut libc::posix_spawnattr_t,
5155
)
5256
};
53-
Errno::result(res)?;
57+
if res != 0 {
58+
return Err(Errno::from_raw(res));
59+
}
5460

5561
Ok(self)
5662
}
@@ -65,7 +71,9 @@ impl PosixSpawnAttr {
6571
flags.bits() as libc::c_short,
6672
)
6773
};
68-
Errno::result(res)?;
74+
if res != 0 {
75+
return Err(Errno::from_raw(res));
76+
}
6977

7078
Ok(())
7179
}
@@ -81,7 +89,9 @@ impl PosixSpawnAttr {
8189
&mut flags,
8290
)
8391
};
84-
Errno::result(res)?;
92+
if res != 0 {
93+
return Err(Errno::from_raw(res));
94+
}
8595

8696
Ok(PosixSpawnFlags::from_bits_truncate(flags.into()))
8797
}
@@ -96,7 +106,9 @@ impl PosixSpawnAttr {
96106
pgroup.as_raw(),
97107
)
98108
};
99-
Errno::result(res)?;
109+
if res != 0 {
110+
return Err(Errno::from_raw(res));
111+
}
100112

101113
Ok(())
102114
}
@@ -113,7 +125,9 @@ impl PosixSpawnAttr {
113125
&mut pid,
114126
)
115127
};
116-
Errno::result(res)?;
128+
if res != 0 {
129+
return Err(Errno::from_raw(res));
130+
}
117131

118132
Ok(Pid::from_raw(pid))
119133
}
@@ -130,7 +144,9 @@ impl PosixSpawnAttr {
130144
sigdefault.as_ref(),
131145
)
132146
};
133-
Errno::result(res)?;
147+
if res != 0 {
148+
return Err(Errno::from_raw(res));
149+
}
134150

135151
Ok(())
136152
}
@@ -147,7 +163,9 @@ impl PosixSpawnAttr {
147163
sigset.as_mut_ptr(),
148164
)
149165
};
150-
Errno::result(res)?;
166+
if res != 0 {
167+
return Err(Errno::from_raw(res));
168+
}
151169

152170
let sigdefault =
153171
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
@@ -164,7 +182,9 @@ impl PosixSpawnAttr {
164182
sigdefault.as_ref(),
165183
)
166184
};
167-
Errno::result(res)?;
185+
if res != 0 {
186+
return Err(Errno::from_raw(res));
187+
}
168188

169189
Ok(())
170190
}
@@ -181,7 +201,9 @@ impl PosixSpawnAttr {
181201
sigset.as_mut_ptr(),
182202
)
183203
};
184-
Errno::result(res)?;
204+
if res != 0 {
205+
return Err(Errno::from_raw(res));
206+
}
185207

186208
let sigdefault =
187209
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
@@ -242,7 +264,10 @@ impl PosixSpawnFileActions {
242264
let res = unsafe {
243265
libc::posix_spawn_file_actions_init(actions.as_mut_ptr())
244266
};
245-
Errno::result(res)?;
267+
if res != 0 {
268+
return Err(Errno::from_raw(res));
269+
}
270+
246271
Ok(unsafe {
247272
PosixSpawnFileActions {
248273
fa: actions.assume_init(),
@@ -262,14 +287,18 @@ impl PosixSpawnFileActions {
262287
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
263288
)
264289
};
265-
Errno::result(res)?;
290+
if res != 0 {
291+
return Err(Errno::from_raw(res));
292+
}
266293

267294
let res = unsafe {
268295
libc::posix_spawn_file_actions_init(
269296
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
270297
)
271298
};
272-
Errno::result(res)?;
299+
if res != 0 {
300+
return Err(Errno::from_raw(res));
301+
}
273302

274303
Ok(self)
275304
}
@@ -285,7 +314,9 @@ impl PosixSpawnFileActions {
285314
newfd,
286315
)
287316
};
288-
Errno::result(res)?;
317+
if res != 0 {
318+
return Err(Errno::from_raw(res));
319+
}
289320

290321
Ok(())
291322
}
@@ -311,7 +342,9 @@ impl PosixSpawnFileActions {
311342
mode.bits(),
312343
)
313344
})?;
314-
Errno::result(res)?;
345+
if res != 0 {
346+
return Err(Errno::from_raw(res));
347+
}
315348

316349
Ok(())
317350
}
@@ -327,7 +360,9 @@ impl PosixSpawnFileActions {
327360
fd,
328361
)
329362
};
330-
Errno::result(res)?;
363+
if res != 0 {
364+
return Err(Errno::from_raw(res));
365+
}
331366

332367
Ok(())
333368
}
@@ -383,8 +418,10 @@ pub fn posix_spawn<SA: AsRef<CStr>, SE: AsRef<CStr>>(
383418
env_p.as_ptr(),
384419
)
385420
};
421+
if res != 0 {
422+
return Err(Errno::from_raw(res));
423+
}
386424

387-
Errno::result(res)?;
388425
Ok(Pid::from_raw(pid))
389426
}
390427

@@ -412,7 +449,9 @@ pub fn posix_spawnp<SA: AsRef<CStr>, SE: AsRef<CStr>>(
412449
env_p.as_ptr(),
413450
)
414451
};
452+
if res != 0 {
453+
return Err(Errno::from_raw(res));
454+
}
415455

416-
Errno::result(res)?;
417456
Ok(Pid::from_raw(pid))
418457
}

test/test_spawn.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,17 @@ fn spawn_sleep() {
6363
}
6464
};
6565
}
66+
67+
#[test]
68+
fn spawn_fail() {
69+
let bin = &CString::new("3f0ffc950ccd2fb8").unwrap();
70+
let args = &[
71+
CString::new("3f0ffc950ccd2fb8").unwrap(),
72+
];
73+
let vars: &[CString] = &[];
74+
let actions = PosixSpawnFileActions::init().unwrap();
75+
let attr = PosixSpawnAttr::init().unwrap();
76+
77+
let result = spawn::posix_spawnp(bin, &actions, &attr, args, vars);
78+
assert!(result.is_err());
79+
}

0 commit comments

Comments
 (0)