Skip to content

Commit c12ef47

Browse files
authored
Make FLAC decoder input pointers const-correct (#31)
Update FLAC decoder API to use const uint8_t* for input buffers, making it explicit that input data is not modified. Changes: - read_header() and decode_frame() now take const uint8_t* buffers - Internal buffer_ member is now const-qualified - Replaced unsafe reinterpret_cast with std::memcpy in refill_bit_buffer() for proper const-correctness and strict aliasing compliance This improves API safety and follows C++ const-correctness best practices while maintaining full backward compatibility.
1 parent 202856d commit c12ef47

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

include/flac_decoder.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class FLACDecoder {
209209
/// @return FLAC_DECODER_SUCCESS when header is complete
210210
/// FLAC_DECODER_HEADER_OUT_OF_DATA when more data is needed (streaming)
211211
/// Error code on failure
212-
FLACDecoderResult read_header(uint8_t *buffer, size_t buffer_length);
212+
FLACDecoderResult read_header(const uint8_t *buffer, size_t buffer_length);
213213

214214
/// @brief Decode a single FLAC frame into PCM samples
215215
///
@@ -223,7 +223,7 @@ class FLACDecoder {
223223
/// @return FLAC_DECODER_SUCCESS on success
224224
/// FLAC_DECODER_NO_MORE_FRAMES when end of stream reached
225225
/// Error code on failure
226-
FLACDecoderResult decode_frame(uint8_t *buffer, size_t buffer_length, uint8_t *output_buffer, uint32_t *num_samples);
226+
FLACDecoderResult decode_frame(const uint8_t *buffer, size_t buffer_length, uint8_t *output_buffer, uint32_t *num_samples);
227227

228228
// ========================================
229229
// Stream Information Getters
@@ -409,7 +409,7 @@ class FLACDecoder {
409409
// ========================================
410410
// Input Buffer State
411411
// ========================================
412-
uint8_t *buffer_ = nullptr; // Pointer to current input buffer
412+
const uint8_t *buffer_ = nullptr; // Pointer to current input buffer
413413
std::size_t buffer_index_ = 0; // Current position in input buffer
414414
std::size_t bytes_left_ = 0; // Bytes remaining in input buffer
415415
std::size_t frame_start_index_ = 0; // Start position of current frame (for CRC)

src/decode/flac/flac_decoder.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static const std::vector<int32_t> FIXED_COEFFICIENTS[] = {{}, {1}, {-1, 2}, {1,
3232
// Header Parsing
3333
// ============================================================================
3434

35-
FLACDecoderResult FLACDecoder::read_header(uint8_t *buffer, size_t buffer_length) {
35+
FLACDecoderResult FLACDecoder::read_header(const uint8_t *buffer, size_t buffer_length) {
3636
this->buffer_ = buffer;
3737
this->buffer_index_ = 0;
3838
this->bytes_left_ = buffer_length;
@@ -182,7 +182,7 @@ FLACDecoderResult FLACDecoder::read_header(uint8_t *buffer, size_t buffer_length
182182
// Force O3 optimization for decode_frame (performance critical)
183183
// This ensures PlatformIO builds also get optimal performance
184184
FLAC_OPTIMIZE_O3
185-
FLACDecoderResult FLACDecoder::decode_frame(uint8_t *buffer, size_t buffer_length, uint8_t *output_buffer,
185+
FLACDecoderResult FLACDecoder::decode_frame(const uint8_t *buffer, size_t buffer_length, uint8_t *output_buffer,
186186
uint32_t *num_samples) {
187187
this->buffer_ = buffer;
188188
this->buffer_index_ = 0;
@@ -955,7 +955,8 @@ void FLACDecoder::align_to_byte() {
955955

956956
inline bool FLACDecoder::refill_bit_buffer() {
957957
if (this->bytes_left_ >= 4) {
958-
const uint32_t new_word = *reinterpret_cast<uint32_t *>(&this->buffer_[this->buffer_index_]);
958+
uint32_t new_word;
959+
std::memcpy(&new_word, &this->buffer_[this->buffer_index_], sizeof(uint32_t));
959960
this->bit_buffer_ = __builtin_bswap32(new_word);
960961
this->bit_buffer_length_ = 32;
961962
this->buffer_index_ += 4;

0 commit comments

Comments
 (0)