From 0a9fa85e865b4df6d26ffc3dd86de7f0b9458e0f Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 27 Jun 2023 12:04:29 +0100 Subject: [PATCH] LibAudio: Use ReadonlyBytes instead of Bytes for buffer parameters Bytes will implicitly cast to StringView, but not to ReadonlyBytes. That means that if you call `Audio::Loader::create_plugin(mapped_file->bytes())` it will silently use the `create_plugin(StringView path)` overload. Reading audio data does not require that data to be writable, so let's use ReadonlyBytes for it and avoid the footgun. --- Userland/Libraries/LibAudio/Loader.cpp | 3 ++- Userland/Libraries/LibAudio/Loader.h | 2 +- Userland/Libraries/LibAudio/WavLoader.cpp | 2 +- Userland/Libraries/LibAudio/WavLoader.h | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibAudio/Loader.cpp b/Userland/Libraries/LibAudio/Loader.cpp index 3abcf7c7fe..c3080a9842 100644 --- a/Userland/Libraries/LibAudio/Loader.cpp +++ b/Userland/Libraries/LibAudio/Loader.cpp @@ -47,7 +47,8 @@ ErrorOr, LoaderError> Loader::create(StringView path) auto stream = TRY(Core::InputBufferedFile::create(TRY(Core::File::open(path, Core::File::OpenMode::Read)))); return adopt_ref(*new (nothrow) Loader(TRY(Loader::create_plugin(move(stream))))); } -ErrorOr, LoaderError> Loader::create(Bytes buffer) + +ErrorOr, LoaderError> Loader::create(ReadonlyBytes buffer) { auto stream = TRY(try_make(buffer)); return adopt_ref(*new (nothrow) Loader(TRY(Loader::create_plugin(move(stream))))); diff --git a/Userland/Libraries/LibAudio/Loader.h b/Userland/Libraries/LibAudio/Loader.h index 2e103416fb..d6d5b9e3e8 100644 --- a/Userland/Libraries/LibAudio/Loader.h +++ b/Userland/Libraries/LibAudio/Loader.h @@ -86,7 +86,7 @@ protected: class Loader : public RefCounted { public: static ErrorOr, LoaderError> create(StringView path); - static ErrorOr, LoaderError> create(Bytes buffer); + static ErrorOr, LoaderError> create(ReadonlyBytes buffer); // Will only read less samples if we're at the end of the stream. LoaderSamples get_more_samples(size_t samples_to_read_from_input = 128 * KiB); diff --git a/Userland/Libraries/LibAudio/WavLoader.cpp b/Userland/Libraries/LibAudio/WavLoader.cpp index 60c5df3789..1d9993f213 100644 --- a/Userland/Libraries/LibAudio/WavLoader.cpp +++ b/Userland/Libraries/LibAudio/WavLoader.cpp @@ -104,7 +104,7 @@ static ErrorOr read_sample(Stream& stream) } } -LoaderSamples WavLoaderPlugin::samples_from_pcm_data(Bytes const& data, size_t samples_to_read) const +LoaderSamples WavLoaderPlugin::samples_from_pcm_data(ReadonlyBytes data, size_t samples_to_read) const { FixedArray samples = TRY(FixedArray::create(samples_to_read)); FixedMemoryStream stream { data }; diff --git a/Userland/Libraries/LibAudio/WavLoader.h b/Userland/Libraries/LibAudio/WavLoader.h index 3d19d5cfba..49da79e685 100644 --- a/Userland/Libraries/LibAudio/WavLoader.h +++ b/Userland/Libraries/LibAudio/WavLoader.h @@ -48,7 +48,7 @@ private: MaybeLoaderError parse_header(); MaybeLoaderError load_wav_info_block(Vector info_chunks); - LoaderSamples samples_from_pcm_data(Bytes const& data, size_t samples_to_read) const; + LoaderSamples samples_from_pcm_data(ReadonlyBytes data, size_t samples_to_read) const; template MaybeLoaderError read_samples_from_stream(Stream& stream, SampleReader read_sample, FixedArray& samples) const;