Skip to content

Commit 85b7ae0

Browse files
authored
Merge pull request #204 from ryanbreen/fix/bsh-getcwd-panic
fix: guard getcwd return value to prevent bsh panic on ARM64
2 parents a549484 + dbd579f commit 85b7ae0

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

userspace/programs/src/bsh.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,15 @@ fn native_pwd(
180180
) -> JsResult<JsValue> {
181181
let mut buf = [0u8; 1024];
182182
match libbreenix::process::getcwd(&mut buf) {
183-
Ok(len) => {
184-
let path = core::str::from_utf8(&buf[..len]).unwrap_or("");
183+
Ok(len) if len <= buf.len() => {
184+
let path = core::str::from_utf8(&buf[..len]).unwrap_or("/");
185185
let id = strings.intern(path);
186186
Ok(JsValue::string(id))
187187
}
188-
Err(_) => Err(JsError::runtime("pwd: failed to get working directory")),
188+
_ => {
189+
let id = strings.intern("/");
190+
Ok(JsValue::string(id))
191+
}
189192
}
190193
}
191194

@@ -1588,7 +1591,7 @@ fn source_file(ctx: &mut Context, path: &str) {
15881591
fn get_short_cwd() -> Option<String> {
15891592
let mut buf = [0u8; 1024];
15901593
match libbreenix::process::getcwd(&mut buf) {
1591-
Ok(len) => {
1594+
Ok(len) if len <= buf.len() => {
15921595
let cwd = std::str::from_utf8(&buf[..len]).ok()?;
15931596
if cwd == "/" {
15941597
Some(String::from("/"))
@@ -1597,7 +1600,7 @@ fn get_short_cwd() -> Option<String> {
15971600
cwd.rsplit('/').next().map(String::from)
15981601
}
15991602
}
1600-
Err(_) => None,
1603+
_ => None,
16011604
}
16021605
}
16031606

0 commit comments

Comments
 (0)