Parcourir la source

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.
Sam Atkins il y a 2 ans
Parent
commit
0a9fa85e86

+ 2 - 1
Userland/Libraries/LibAudio/Loader.cpp

@@ -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))));
     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));
     return adopt_ref(*new (nothrow) Loader(TRY(Loader::create_plugin(move(stream)))));

+ 1 - 1
Userland/Libraries/LibAudio/Loader.h

@@ -86,7 +86,7 @@ protected:
 class Loader : public RefCounted<Loader> {
 public:
     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.
     LoaderSamples get_more_samples(size_t samples_to_read_from_input = 128 * KiB);

+ 1 - 1
Userland/Libraries/LibAudio/WavLoader.cpp

@@ -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));
     FixedMemoryStream stream { data };

+ 1 - 1
Userland/Libraries/LibAudio/WavLoader.h

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