Skip to content

Make from_bits in bitflags! safe; add from_bits_truncate #14208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/libnative/io/file_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,7 @@ fn mkstat(stat: &libc::stat) -> io::FileStat {
io::FileStat {
size: stat.st_size as u64,
kind: kind,
perm: unsafe {
io::FilePermission::from_bits(stat.st_mode as u32) & io::AllPermissions
},
perm: io::FilePermission::from_bits_truncate(stat.st_mode as u32),
created: mktime(stat.st_ctime as u64, stat.st_ctime_nsec as u64),
modified: mktime(stat.st_mtime as u64, stat.st_mtime_nsec as u64),
accessed: mktime(stat.st_atime as u64, stat.st_atime_nsec as u64),
Expand Down
4 changes: 1 addition & 3 deletions src/libnative/io/file_win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,7 @@ fn mkstat(stat: &libc::stat) -> io::FileStat {
io::FileStat {
size: stat.st_size as u64,
kind: kind,
perm: unsafe {
io::FilePermission::from_bits(stat.st_mode as u32) & io::AllPermissions
},
perm: io::FilePermission::from_bits_truncate(stat.st_mode as u32),
created: stat.st_ctime as u64,
modified: stat.st_mtime as u64,
accessed: stat.st_atime as u64,
Expand Down
4 changes: 1 addition & 3 deletions src/librustuv/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,7 @@ impl FsRequest {
FileStat {
size: stat.st_size as u64,
kind: kind,
perm: unsafe {
io::FilePermission::from_bits(stat.st_mode as u32) & io::AllPermissions
},
perm: io::FilePermission::from_bits_truncate(stat.st_mode as u32),
created: to_msec(stat.st_birthtim),
modified: to_msec(stat.st_mtim),
accessed: to_msec(stat.st_atim),
Expand Down
37 changes: 30 additions & 7 deletions src/libstd/bitflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,20 @@ macro_rules! bitflags(
self.bits
}

/// Convert from underlying bit representation. Unsafe because the
/// bits are not guaranteed to represent valid flags.
pub unsafe fn from_bits(bits: $T) -> $BitFlags {
$BitFlags { bits: bits }
/// Convert from underlying bit representation, unless that
/// representation contains bits that do not correspond to a flag.
pub fn from_bits(bits: $T) -> ::std::option::Option<$BitFlags> {
if (bits & !$BitFlags::all().bits()) != 0 {
::std::option::None
} else {
::std::option::Some($BitFlags { bits: bits })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use std::option; could make it cleaner.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to import the module as part of the macro, since that can pollute the macro client's namespace -- so I think the type signature, at least, needs to stay as is.

(Note that macros.rs in libstd follows the same pattern.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I have to admit I’m not used to declaring macros.

}
}

/// Convert from underlying bit representation, dropping any bits
/// that do not correspond to flags.
pub fn from_bits_truncate(bits: $T) -> $BitFlags {
$BitFlags { bits: bits } & $BitFlags::all()
}

/// Returns `true` if no flags are currently stored.
Expand Down Expand Up @@ -209,6 +219,7 @@ macro_rules! bitflags(

#[cfg(test)]
mod tests {
use option::{Some, None};
use ops::{BitOr, BitAnd, Sub, Not};

bitflags!(
Expand All @@ -231,9 +242,21 @@ mod tests {

#[test]
fn test_from_bits() {
assert!(unsafe { Flags::from_bits(0x00000000) } == Flags::empty());
assert!(unsafe { Flags::from_bits(0x00000001) } == FlagA);
assert!(unsafe { Flags::from_bits(0x00000111) } == FlagABC);
assert!(Flags::from_bits(0) == Some(Flags::empty()));
assert!(Flags::from_bits(0x1) == Some(FlagA));
assert!(Flags::from_bits(0x10) == Some(FlagB));
assert!(Flags::from_bits(0x11) == Some(FlagA | FlagB));
assert!(Flags::from_bits(0x1000) == None);
}

#[test]
fn test_from_bits_truncate() {
assert!(Flags::from_bits_truncate(0) == Flags::empty());
assert!(Flags::from_bits_truncate(0x1) == FlagA);
assert!(Flags::from_bits_truncate(0x10) == FlagB);
assert!(Flags::from_bits_truncate(0x11) == (FlagA | FlagB));
assert!(Flags::from_bits_truncate(0x1000) == Flags::empty());
assert!(Flags::from_bits_truncate(0x1001) == FlagA);
}

#[test]
Expand Down