1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:17:34 +00:00

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.
This commit is contained in:
Sam Atkins 2023-06-27 12:04:29 +01:00 committed by Sam Atkins
parent 8e0fa57e5b
commit 0a9fa85e86
4 changed files with 5 additions and 4 deletions

View file

@ -47,7 +47,8 @@ ErrorOr<NonnullRefPtr<Loader>, LoaderError> Loader::create(StringView path)
auto stream = TRY(Core::InputBufferedFile::create(TRY(Core::File::open(path, Core::File::OpenMode::Read)))); 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))))); return adopt_ref(*new (nothrow) Loader(TRY(Loader::create_plugin(move(stream)))));
} }
ErrorOr<NonnullRefPtr<Loader>, LoaderError> Loader::create(Bytes buffer)
ErrorOr<NonnullRefPtr<Loader>, LoaderError> Loader::create(ReadonlyBytes buffer)
{ {
auto stream = TRY(try_make<FixedMemoryStream>(buffer)); auto stream = TRY(try_make<FixedMemoryStream>(buffer));
return adopt_ref(*new (nothrow) Loader(TRY(Loader::create_plugin(move(stream))))); return adopt_ref(*new (nothrow) Loader(TRY(Loader::create_plugin(move(stream)))));

View file

@ -86,7 +86,7 @@ protected:
class Loader : public RefCounted<Loader> { class Loader : public RefCounted<Loader> {
public: public:
static ErrorOr<NonnullRefPtr<Loader>, LoaderError> create(StringView path); static ErrorOr<NonnullRefPtr<Loader>, LoaderError> create(StringView path);
static ErrorOr<NonnullRefPtr<Loader>, LoaderError> create(Bytes buffer); static ErrorOr<NonnullRefPtr<Loader>, LoaderError> create(ReadonlyBytes buffer);
// Will only read less samples if we're at the end of the stream. // 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); LoaderSamples get_more_samples(size_t samples_to_read_from_input = 128 * KiB);

View file

@ -104,7 +104,7 @@ static ErrorOr<double> 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<Sample> samples = TRY(FixedArray<Sample>::create(samples_to_read)); FixedArray<Sample> samples = TRY(FixedArray<Sample>::create(samples_to_read));
FixedMemoryStream stream { data }; FixedMemoryStream stream { data };

View file

@ -48,7 +48,7 @@ private:
MaybeLoaderError parse_header(); MaybeLoaderError parse_header();
MaybeLoaderError load_wav_info_block(Vector<RIFF::Chunk> info_chunks); MaybeLoaderError load_wav_info_block(Vector<RIFF::Chunk> 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<typename SampleReader> template<typename SampleReader>
MaybeLoaderError read_samples_from_stream(Stream& stream, SampleReader read_sample, FixedArray<Sample>& samples) const; MaybeLoaderError read_samples_from_stream(Stream& stream, SampleReader read_sample, FixedArray<Sample>& samples) const;