Skip to content

Commit

Permalink
Statically check that COMMON_STRINGS cannot cause overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Jun 30, 2022
1 parent b00bdf6 commit 0c2ef02
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions boa_interner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ license = "Unlicense/MIT"
serde = { version = "1.0.137", features = ["derive"], optional = true }
phf = { version = "0.10.1", features = ["macros"] }
rustc-hash = "1.1.0"
static_assertions = "1.1.0"
6 changes: 4 additions & 2 deletions boa_interner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
rustdoc::missing_doc_code_examples
)]

extern crate static_assertions as sa;

mod fixed_string;
mod interned_str;
mod sym;
Expand Down Expand Up @@ -263,8 +265,8 @@ impl Interner {
COMMON_STRINGS.get_index(string).map(|idx|
// SAFETY: `idx >= 0`, since it's an `usize`, and `idx + 1 > 0`.
// In this case, we don't need to worry about overflows
// because `COMMON_STRINGS` would need to be of considerable
// size to cause an overflow, even on machines with `usize = u32`.
// because we have a static assertion in place checking that
// `COMMON_STRINGS.len() < usize::MAX`.
unsafe {
Sym::new_unchecked(idx + 1)
})
Expand Down
51 changes: 28 additions & 23 deletions boa_interner/src/sym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,32 @@ impl Sym {
///
/// `COMMON_STRINGS` and the constants defined in [`Sym`] must always
/// be in sync.
pub(super) static COMMON_STRINGS: phf::OrderedSet<&'static str> = phf::phf_ordered_set! {
"",
"arguments",
"await",
"yield",
"eval",
"default",
"null",
"RegExp",
"get",
"set",
"<main>",
"raw",
"static",
"prototype",
"constructor",
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
pub(super) static COMMON_STRINGS: phf::OrderedSet<&'static str> = {
const COMMON_STRINGS: phf::OrderedSet<&'static str> = phf::phf_ordered_set! {
"",
"arguments",
"await",
"yield",
"eval",
"default",
"null",
"RegExp",
"get",
"set",
"<main>",
"raw",
"static",
"prototype",
"constructor",
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
};
// A `COMMON_STRINGS` of size `usize::MAX` would cause an overflow on our `Interner`
sa::const_assert!(COMMON_STRINGS.len() < usize::MAX);
COMMON_STRINGS
};

0 comments on commit 0c2ef02

Please sign in to comment.