Skip to content
This repository was archived by the owner on Apr 23, 2026. It is now read-only.

Return an empty slice on safe slice casting error#462

Merged
jdm merged 1 commit into
servo:mainfrom
tharkum:safely-slice-casting-wrong-alignment
Nov 18, 2025
Merged

Return an empty slice on safe slice casting error#462
jdm merged 1 commit into
servo:mainfrom
tharkum:safely-slice-casting-wrong-alignment

Conversation

@tharkum
Copy link
Copy Markdown
Contributor

@tharkum tharkum commented Nov 18, 2025

The cargo byte-slice-cast crate supports safe u8 -> f32 slice casting through the as_slice_of trait function. However, for dynamic linked crate from rust toolchain (byte-slice-cast-0.2.0) in case if source u8 slice is empty, the WrongAlignment error will be returned, so need to handle this edge case properly and return the empty f32 slice on error.

Please notice that the newest version (byte-slice-cast-1.2.3) is handling this case without alignment error (return an empty slice).

https://docs.rs/byte-slice-cast/0.2.0/src/byte_slice_cast/lib.rs.html#206
https://docs.rs/byte-slice-cast/1.2.3/src/byte_slice_cast/lib.rs.html#263

The cargo `byte-slice-cast` crate supports safe `u8 -> f32`
slice casting through the `as_slice_of` trait function.
However, for dynamic linked crate from rust toolchain
(`byte-slice-cast-0.2.0`) in case if source `u8` slice is empty,
the `WrongAlignment` error will be returned, so need to handle
this edge case properly and return the empty `f32` slice on error.

Please notice that the newest version (`byte-slice-cast-1.2.3`)
is handling this case without alignment error (return an empty slice).

https://docs.rs/byte-slice-cast/0.2.0/src/byte_slice_cast/lib.rs.html#206
https://docs.rs/byte-slice-cast/1.2.3/src/byte_slice_cast/lib.rs.html#263

Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
@tharkum
Copy link
Copy Markdown
Contributor Author

tharkum commented Nov 18, 2025

There are observable CRASH in the following test:
./mach test-wpt tests/wpt/tests/html/semantics/embedded-content/media-elements/preserves-pitch.html -r


0:03.75 pid:3902622 called `Result::unwrap()` on an `Err` value: WrongAlignment (thread <unnamed>, at /home/XXXXX/workspace/projects/servo/media/backends/gstreamer/player.rs:122)
 0:03.77 pid:3902622    0: servoshell::backtrace::print
 0:03.77 pid:3902622    1: servoshell::panic_hook::panic_hook
 0:03.77 pid:3902622    2: std::panicking::panic_with_hook
 0:03.77 pid:3902622    3: std::panicking::panic_handler::{{closure}}
 0:03.77 pid:3902622    4: std::sys::backtrace::__rust_end_short_backtrace
 0:03.77 pid:3902622    5: __rustc::rust_begin_unwind
 0:03.77 pid:3902622    6: core::panicking::panic_fmt
 0:03.77 pid:3902622    7: core::result::unwrap_failed
 0:03.77 pid:3902622    8: <servo_media_gstreamer::player::GStreamerAudioChunk as core::convert::AsRef<[f32]>>::as_ref
 0:03.77 pid:3902622    9: <servo_media_audio::media_element_source_node::MediaElementSourceNodeRenderer as servo_media_player::audio::AudioRenderer>::render
 0:03.77 pid:3902622   10: servo_media_gstreamer::player::GStreamerPlayer::setup::{{closure}}
 0:03.77 pid:3902622   11: gstreamer_app::app_sink::trampoline_new_sample

@tharkum
Copy link
Copy Markdown
Contributor Author

tharkum commented Nov 18, 2025

@jdm , @sdroege < Please take a look...

@sdroege
Copy link
Copy Markdown
Contributor

sdroege commented Nov 18, 2025

Why not update from byte-slice-cast 0.2 to 1?

@tharkum
Copy link
Copy Markdown
Contributor Author

tharkum commented Nov 18, 2025

The byte-slice-cast-1.2.3 could also return the following AlignmentMismatch and LengthMismatch errors (chance is too low, but...), so we must also handle these potential cases by returning an empty slice.

So I decided don't upgrade crate and make it statically linked without significant reason.

@jdm jdm added this pull request to the merge queue Nov 18, 2025
@sdroege
Copy link
Copy Markdown
Contributor

sdroege commented Nov 18, 2025

So I decided don't upgrade crate and make it statically linked without significant reason.

Right, but updating seems like a good idea regardless :) There were a couple of bugs fixed after the last couple of years between these two versions.

Merged via the queue into servo:main with commit 7ba5e7d Nov 18, 2025
3 checks passed
@tharkum tharkum deleted the safely-slice-casting-wrong-alignment branch November 18, 2025 14:22
impl AsRef<[f32]> for GStreamerAudioChunk {
fn as_ref(&self) -> &[f32] {
self.0.as_ref().as_slice_of::<f32>().unwrap()
self.0.as_ref().as_slice_of::<f32>().unwrap_or_default()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure this is the right way of handling this. You're going to silently ignore bugs here (it should always be aligned correctly, etc) which are going to be very annoying to debug if you just make this an empty slice.

Also note that backends/gstreamer/audio_decoder.rs has basically the same code but still unwrap()s, so at the very least this is inconsistent.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And backends/gstreamer/audio_stream_reader.rs converts it into an Err but as above, this should never really happen and if it happens then there is a bug elsewhere that needs to be fixed.

At the very least these cases should be logged.

@sdroege
Copy link
Copy Markdown
Contributor

sdroege commented Nov 20, 2025

byte-slice-cast update is in #469

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants