Skip to content

Commit 72dec29

Browse files
committed
Addressed review comments and added comments for a potential bug
1 parent a274109 commit 72dec29

File tree

3 files changed

+103
-90
lines changed

3 files changed

+103
-90
lines changed

src/interface/misc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ pub fn sigcheck() -> bool {
204204
}
205205

206206
pub fn fillrandom(bufptr: *mut u8, count: usize) -> i32 {
207+
// Potential Bug: The fillrandom function is reading from the /dev/urandom
208+
// file, where it should read from "/dev/random" file. And, there should be
209+
// a seperate function for reading from "/dev/random" file.
207210
let slice = unsafe { std::slice::from_raw_parts_mut(bufptr, count) };
208211
let mut f = std::fs::OpenOptions::new()
209212
.read(true)

src/safeposix/syscalls/fs_calls.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,14 +1471,11 @@ impl Cage {
14711471
// object is updated by adding the number of bytes read (bytesread).
14721472
// This ensures that the next read operation will start from the correct
14731473
// position.
1474-
if let Ok(bytesread) = fileobject.readat(buf, count, position) {
1475-
//move position forward by the number of bytes we've read
1476-
normalfile_filedesc_obj.position += bytesread;
1477-
bytesread as i32
1478-
} else {
1479-
0 //0 bytes read, but not an error value that
1480-
// can/should be passed to the user
1481-
}
1474+
let bytesread = fileobject.readat(buf, count, position as usize).unwrap();
1475+
//move position forward by the number of bytes we've read
1476+
normalfile_filedesc_obj.position += bytesread;
1477+
// Return the number of bytes read.
1478+
bytesread as i32
14821479
}
14831480

14841481
// For `CharDev` type inode, the reading happens from the Character Device
@@ -1519,6 +1516,12 @@ impl Cage {
15191516
"read",
15201517
"reading from stdin not implemented yet",
15211518
),
1519+
// Reading from `Epoll` type file descriptors is not supported.
1520+
Epoll(_) => syscall_error(
1521+
Errno::EINVAL,
1522+
"read",
1523+
"fd is attached to an object which is unsuitable for reading",
1524+
),
15221525
// The `Pipe` type file descriptor handles read through blocking and non-blocking
15231526
// modes differently to ensure appropriate behavior based on the flags set on the
15241527
// pipe. In blocking mode, the read_from_pipe function will wait until data is
@@ -1576,12 +1579,6 @@ impl Cage {
15761579
return ret;
15771580
}
15781581
}
1579-
// Reading from `Epoll` type file descriptors is not supported.
1580-
Epoll(_) => syscall_error(
1581-
Errno::EINVAL,
1582-
"read",
1583-
"fd is attached to an object which is unsuitable for reading",
1584-
),
15851582
}
15861583
} else {
15871584
syscall_error(Errno::EBADF, "read", "invalid file descriptor")
@@ -1674,13 +1671,11 @@ impl Cage {
16741671
let fileobject =
16751672
FILEOBJECTTABLE.get(&normalfile_filedesc_obj.inode).unwrap();
16761673

1677-
// Attempt to read from the file at the specified offset
1678-
if let Ok(bytesread) = fileobject.readat(buf, count, offset as usize) {
1679-
bytesread as i32
1680-
} else {
1681-
0 //0 bytes read, but not an error value that
1682-
// can/should be passed to the user
1683-
}
1674+
// `readat` function reads from file at specified offset into provided
1675+
// C-buffer.
1676+
let bytesread = fileobject.readat(buf, count, offset as usize).unwrap();
1677+
// Return the number of bytes read.
1678+
bytesread as i32
16841679
}
16851680

16861681
// For `CharDev` type inode, the reading happens from the Character Device
@@ -1790,6 +1785,7 @@ impl Cage {
17901785
// reading from /dev/random fills the buffer with random bytes
17911786
RANDOMDEVNO => interface::fillrandom(buf, count),
17921787
// reading from /dev/urandom also fills the buffer with random bytes
1788+
// Note: This might have to be changed in future.
17931789
URANDOMDEVNO => interface::fillrandom(buf, count),
17941790
// for any device number not specifically handled above,
17951791
// we return an error

0 commit comments

Comments
 (0)