Skip to content

Commit

Permalink
Merge branch 'main' into webidl-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Jan 4, 2025
2 parents 871020f + 726b802 commit 5970aab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:

- name: Test (valgrind)
id: test-valgrind
if: inputs.job == 'test-valgrind'
if: inputs.is_main && inputs.job == 'test-valgrind'
uses: ./.github/workflows/ci-test-valgrind

- name: Publish (dry-run)
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ jobs:
config: |-
linux:
os: ubuntu-24.04
jobs: [coverage, lint, lint-deps, test, test-miri, test-ops, test-publish]
linux-xl:
os: ubuntu-22.04-xl
jobs: [test-valgrind]
jobs: [coverage, lint, lint-deps, test, test-miri, test-ops, test-publish, test-valgrind]
linux-arm:
os: ubuntu-22.04-arm64
jobs: [lint, test, test-miri]
Expand Down
44 changes: 20 additions & 24 deletions core/runtime/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,16 @@ pub fn to_string_ptr(string: &v8::fast_api::FastApiOneByteString) -> String {

// SAFETY: We're allocating a buffer of 2x the input size, writing valid UTF-8, then turning that into a string
unsafe {
// Create an uninitialized buffer of `capacity` bytes. We need to be careful here to avoid
// accidentally creating a slice of u8 which would be invalid.
let layout = std::alloc::Layout::from_size_align(capacity, 1).unwrap();
let out = std::alloc::alloc(layout);
// Create an uninitialized buffer of `capacity` bytes.
let mut buffer = Vec::<u8>::with_capacity(capacity);

let written = latin1_to_utf8(input_buf.len(), input_buf.as_ptr(), out);
let written =
latin1_to_utf8(input_buf.len(), input_buf.as_ptr(), buffer.as_mut_ptr());

debug_assert!(written <= capacity);
buffer.set_len(written);
// We know it's valid UTF-8, so make a string
String::from_raw_parts(out, written, capacity)
String::from_utf8_unchecked(buffer)
}
}

Expand Down Expand Up @@ -312,25 +312,21 @@ pub fn to_cow_one_byte(
return Err("expected one-byte String");
}

// Create an uninitialized buffer of `capacity` bytes. We need to be careful here to avoid
// accidentally creating a slice of u8 which would be invalid.
unsafe {
let layout = std::alloc::Layout::from_size_align(capacity, 1).unwrap();
let out = std::alloc::alloc(layout);

// Write the buffer to a slice made from this uninitialized data
{
let buffer = std::slice::from_raw_parts_mut(out as _, capacity);
string.write_one_byte_uninit(
scope,
buffer,
0,
WriteOptions::NO_NULL_TERMINATION,
);
}
// Create an uninitialized buffer of `capacity` bytes.
let mut buffer = Vec::<u8>::with_capacity(capacity);
// Write the buffer to a slice made from this uninitialized data
string.write_one_byte_uninit(
scope,
buffer.spare_capacity_mut(),
0,
WriteOptions::NO_NULL_TERMINATION,
);

Ok(Vec::from_raw_parts(out, capacity, capacity).into())
}
// SAFETY: We initialized bytes from `0..capacity` in
// `write_one_byte_uninit` above.
unsafe { buffer.set_len(capacity) };

Ok(Cow::Owned(buffer))
}

/// Converts from a raw [`v8::Value`] to the expected V8 data type.
Expand Down

0 comments on commit 5970aab

Please sign in to comment.