diff --git a/android/app/src/main/cpp/flac_pcm_source.cpp b/android/app/src/main/cpp/flac_pcm_source.cpp index 77873462f..95e84e9ab 100644 --- a/android/app/src/main/cpp/flac_pcm_source.cpp +++ b/android/app/src/main/cpp/flac_pcm_source.cpp @@ -54,7 +54,10 @@ FLAC__StreamDecoderWriteStatus FlacPcmSource::write( source.failure = "FLAC format changed during decoding"; return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; } - source.block.resize(static_cast(header.blocksize) * source.channels); + if (!source.block.resize(static_cast(header.blocksize) * source.channels)) { + source.failure = "Cannot allocate FLAC block"; + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + } source.offset = 0; for (size_t i = 0; i < header.blocksize; ++i) { for (int channel = 0; channel < source.channels; ++channel) { diff --git a/android/app/src/main/cpp/flac_pcm_source.h b/android/app/src/main/cpp/flac_pcm_source.h index a12b7f5f1..140363872 100644 --- a/android/app/src/main/cpp/flac_pcm_source.h +++ b/android/app/src/main/cpp/flac_pcm_source.h @@ -1,12 +1,12 @@ #pragma once #include +#include "pcm_buffer.h" #include #include #include #include -#include // Owns its file descriptor, decodes one FLAC block at a time, and never passes // samples through Android's extractor (which may silently emit 16-bit PCM). @@ -15,7 +15,7 @@ class FlacPcmSource final { void operator()(FLAC__StreamDecoder *decoder) const; }; std::unique_ptr decoder; - std::vector block; + PcmBuffer block; size_t offset = 0; uint64_t position = 0; bool end = false; diff --git a/android/app/src/main/cpp/native_audio.cpp b/android/app/src/main/cpp/native_audio.cpp index a3b074fcd..11b600b0c 100644 --- a/android/app/src/main/cpp/native_audio.cpp +++ b/android/app/src/main/cpp/native_audio.cpp @@ -2,6 +2,7 @@ #include #include #include "flac_pcm_source.h" +#include "pcm_buffer.h" #include #include @@ -76,7 +77,7 @@ public: class ExclusiveOutput final : public oboe::AudioStreamDataCallback { std::shared_ptr stream; // Single producer (Kotlin worker), single consumer (audio callback). - std::vector ring; + PcmBuffer ring; std::atomic read{0}, written{0}, musicFrames{0}; std::atomic lastMusicEnd{0}; int64_t callbackFrames = 0; @@ -130,7 +131,9 @@ public: floatOutput = format == oboe::AudioFormat::Float; bits = format == oboe::AudioFormat::I16 ? 16 : format == oboe::AudioFormat::I24 ? 24 : 32; frameBytes = channels * (bits / 8); - ring.resize(static_cast(rate / 4) * frameBytes); + if (!ring.resize(static_cast(rate / 4) * frameBytes)) { + return fail(errorOut, "Cannot allocate output buffer"); + } return true; } appendFmt(attempts, " actual=%s/%dHz/%dch/%s, api=%s, device=%d", @@ -353,7 +356,12 @@ JNIEXPORT jbyteArray JNICALL JNI_AUDIO(readWavPack)(JNIEnv *env, jobject, jlong std::string error; auto d = decoders.get(handle, &error); if (!d) { report(env, error); return nullptr; } - std::vector data(static_cast(frames) * d->channels); + PcmBuffer data; + PcmBuffer bytes; + if (!data.resize(static_cast(frames) * d->channels) || !bytes.resize(data.size())) { + report(env, "Cannot allocate DSD buffer"); + return nullptr; + } auto count = WavpackUnpackSamples(d->context, data.data(), frames); if (WavpackGetNumErrors(d->context) != 0) { report(env, "Corrupt WavPack DSD data"); @@ -363,7 +371,7 @@ JNIEXPORT jbyteArray JNICALL JNI_AUDIO(readWavPack)(JNIEnv *env, jobject, jlong report(env, "Truncated WavPack DSD data"); return nullptr; } - std::vector bytes(static_cast(count) * d->channels); + bytes.resize(static_cast(count) * d->channels); // OPEN_DSD_NATIVE returns interleaved, MSB-first DSD bytes regardless // of the wrapper's original DSF bit-order/blocking flags. for (size_t i = 0; i < bytes.size(); ++i) bytes[i] = static_cast(data[i]); diff --git a/android/app/src/main/cpp/pcm_buffer.h b/android/app/src/main/cpp/pcm_buffer.h new file mode 100644 index 000000000..3a3f7dff0 --- /dev/null +++ b/android/app/src/main/cpp/pcm_buffer.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include +#include +#include + +// Sample storage whose growth reports failure. This library builds without +// exceptions, where a std::vector that cannot allocate ends the process. +template +class PcmBuffer final { + std::unique_ptr values; + size_t capacity = 0; + size_t count = 0; +public: + // Growing drops the old contents. On false the buffer is unchanged. + bool resize(size_t size) { + if (size > capacity) { + std::unique_ptr grown(new (std::nothrow) T[size]()); + if (!grown) return false; + values = std::move(grown); + capacity = size; + } + count = size; + return true; + } + void clear() { count = 0; } + size_t size() const { return count; } + T *data() { return values.get(); } + const T *data() const { return values.get(); } + T &operator[](size_t index) { return values[index]; } + const T &operator[](size_t index) const { return values[index]; } +}; diff --git a/android/app/src/main/cpp/tests/flac_pcm_source_test.cpp b/android/app/src/main/cpp/tests/flac_pcm_source_test.cpp index f6c606bdf..186bdbf73 100644 --- a/android/app/src/main/cpp/tests/flac_pcm_source_test.cpp +++ b/android/app/src/main/cpp/tests/flac_pcm_source_test.cpp @@ -6,8 +6,10 @@ #include #include #include +#include #include #include +#include namespace { void check(bool value, const char *message) { @@ -126,6 +128,15 @@ void retainsDescriptor() { check(source.seek(5001, &error), error.c_str()); compare(source, fixture, 5001, 24); } + +void reportsFailedAllocation() { + PcmBuffer buffer; + check(buffer.resize(8), "allocate buffer"); + buffer[7] = 42; + const size_t impossible = std::numeric_limits::max() / sizeof(int32_t); + check(!buffer.resize(impossible), "accepted an impossible size"); + check(buffer.size() == 8 && buffer[7] == 42, "failed growth changed the buffer"); +} } int main() { @@ -137,6 +148,7 @@ int main() { } rejectsTruncatedData(); retainsDescriptor(); + reportsFailedAllocation(); std::cout << "FLAC integer precision, padding, seek, EOF and truncation checks passed\n"; return EXIT_SUCCESS; } catch (const std::exception &error) {