Browse Source

Handle allocation errors (#1850)

Fixes #1847 by wrapping the `std::alloc::alloc()` call in `try_alloc()`, which checks that the returned pointer is non-null and handles allocation errors that way. It will now abort the process instead of executing UB in the error path
pull/1746/head
Timo 3 years ago
parent
commit
fabbf15dd3
  1. 14
      boa/src/string.rs

14
boa/src/string.rs

@ -4,7 +4,7 @@ use crate::{
};
use rustc_hash::FxHashSet;
use std::{
alloc::{alloc, dealloc, Layout},
alloc::{alloc, dealloc, handle_alloc_error, Layout},
borrow::Borrow,
cell::Cell,
hash::{Hash, Hasher},
@ -174,6 +174,14 @@ const MAX_CONSTANT_STRING_LENGTH: usize = {
max
};
unsafe fn try_alloc(layout: Layout) -> *mut u8 {
let ptr = alloc(layout);
if ptr.is_null() {
handle_alloc_error(layout);
}
ptr
}
thread_local! {
static CONSTANTS: FxHashSet<JsString> = {
let mut constants = FxHashSet::default();
@ -217,7 +225,7 @@ impl Inner {
.expect("failed to extend memory layout");
let inner = unsafe {
let inner = alloc(layout).cast::<Self>();
let inner = try_alloc(layout).cast::<Self>();
// Write the first part, the Inner.
inner.write(Self {
@ -257,7 +265,7 @@ impl Inner {
.expect("failed to extend memory layout");
let inner = unsafe {
let inner = alloc(layout).cast::<Self>();
let inner = try_alloc(layout).cast::<Self>();
// Write the first part, the Inner.
inner.write(Self {

Loading…
Cancel
Save