From efea921e60b7e094bd49f27d90cca0cb4a153514 Mon Sep 17 00:00:00 2001 From: Kevin Ahrendt Date: Fri, 8 May 2026 12:34:41 +0000 Subject: [PATCH] Force construction through the ``create`` method --- esphome/components/audio/audio_transfer_buffer.cpp | 3 ++- esphome/components/audio/audio_transfer_buffer.h | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/esphome/components/audio/audio_transfer_buffer.cpp b/esphome/components/audio/audio_transfer_buffer.cpp index 518aefa291..db317099ab 100644 --- a/esphome/components/audio/audio_transfer_buffer.cpp +++ b/esphome/components/audio/audio_transfer_buffer.cpp @@ -212,7 +212,8 @@ std::unique_ptr RingBufferAudioSource::create( if (ring_buffer == nullptr || max_fill_bytes == 0 || alignment_bytes == 0 || alignment_bytes > MAX_ALIGNMENT_BYTES) { return nullptr; } - return make_unique(std::move(ring_buffer), max_fill_bytes, alignment_bytes); + return std::unique_ptr( + new RingBufferAudioSource(std::move(ring_buffer), max_fill_bytes, alignment_bytes)); } RingBufferAudioSource::~RingBufferAudioSource() { diff --git a/esphome/components/audio/audio_transfer_buffer.h b/esphome/components/audio/audio_transfer_buffer.h index c78d491f77..74fce57596 100644 --- a/esphome/components/audio/audio_transfer_buffer.h +++ b/esphome/components/audio/audio_transfer_buffer.h @@ -235,10 +235,6 @@ class RingBufferAudioSource : public AudioReadableBuffer { static std::unique_ptr create(std::shared_ptr ring_buffer, size_t max_fill_bytes, uint8_t alignment_bytes = 1); - explicit RingBufferAudioSource(std::shared_ptr ring_buffer, size_t max_fill_bytes, - uint8_t alignment_bytes) - : ring_buffer_(std::move(ring_buffer)), max_fill_bytes_(max_fill_bytes), alignment_bytes_(alignment_bytes) {} - ~RingBufferAudioSource() override; // AudioReadableBuffer interface @@ -257,6 +253,12 @@ class RingBufferAudioSource : public AudioReadableBuffer { uint8_t *mutable_data() { return this->current_data_; } protected: + /// @brief Constructs a new ring-buffer-backed audio source. Use create() instead, which validates + /// arguments before construction. + explicit RingBufferAudioSource(std::shared_ptr ring_buffer, size_t max_fill_bytes, + uint8_t alignment_bytes) + : ring_buffer_(std::move(ring_buffer)), max_fill_bytes_(max_fill_bytes), alignment_bytes_(alignment_bytes) {} + /// @brief Releases the currently held ring buffer item, first copying any trailing sub-frame bytes /// into the splice buffer so they can be stitched with the next chunk. void release_item_();