Fix the wrong value returned from getcwd

`getcwd` should return the length of buffer filled
This commit is contained in:
LI Qing 2021-07-27 11:35:38 +08:00 committed by Zongmin.Gu
parent fecef8eda6
commit a54de67431
2 changed files with 14 additions and 3 deletions

@ -369,14 +369,15 @@ pub fn do_getcwd(buf_ptr: *mut u8, size: usize) -> Result<isize> {
}; };
let cwd = fs_ops::do_getcwd()?; let cwd = fs_ops::do_getcwd()?;
if cwd.len() + 1 > buf.len() { if cwd.len() + 1 > buf.len() {
return_errno!(ERANGE, "buf is not long enough"); return_errno!(ERANGE, "buf is not long enough");
} }
buf[..cwd.len()].copy_from_slice(cwd.as_bytes()); buf[..cwd.len()].copy_from_slice(cwd.as_bytes());
buf[cwd.len()] = 0; buf[cwd.len()] = b'\0';
Ok(buf.len() as isize) // The user-level library returns the pointer of buffer, the kernel just returns
// the length of the buffer filled (which includes the ending '\0' character).
Ok((cwd.len() + 1) as isize)
} }
pub fn do_rename(oldpath: *const i8, newpath: *const i8) -> Result<isize> { pub fn do_rename(oldpath: *const i8, newpath: *const i8) -> Result<isize> {

@ -1,4 +1,5 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/syscall.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include "test_fs.h" #include "test_fs.h"
@ -116,6 +117,15 @@ static int __test_chdir(const char *dir_path) {
if (strcmp(buf, dir_path)) { if (strcmp(buf, dir_path)) {
THROW_ERROR("the cwd is incorrect after chdir"); THROW_ERROR("the cwd is incorrect after chdir");
} }
// Check getcwd via explicit syscall
int ret = syscall(__NR_getcwd, buf, sizeof(buf));
if (ret < 0) {
THROW_ERROR("failed to call via explicit syscall");
}
if (ret != strlen(dir_path) + 1) {
THROW_ERROR("failed to check the return value from kernel");
}
return 0; return 0;
} }