From 325bedcf3f2ee4eeeb4994d572512727ae1d87e6 Mon Sep 17 00:00:00 2001 From: trompetin17 Date: Mon, 23 Jun 2025 11:46:48 -0500 Subject: [PATCH] Use std::array for OggData buffer handling Replaced the raw C array for m_Buffer with std::array to improve bounds safety and code clarity. - Used .data() and .at() for buffer access - Added check for bufferCount exceeding OGG_MAX_BUFFER_COUNT - Left FetchDataIntoBuffer unchanged to avoid breaking external usage (e.g. CStreamItem) This refactor improves safety while keeping external API stable. --- source/soundmanager/data/OggData.cpp | 19 +++++++------------ source/soundmanager/data/OggData.h | 8 +++++++- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/source/soundmanager/data/OggData.cpp b/source/soundmanager/data/OggData.cpp index 9b8da6aa6b..0d5a955367 100644 --- a/source/soundmanager/data/OggData.cpp +++ b/source/soundmanager/data/OggData.cpp @@ -34,11 +34,6 @@ */ constexpr int OGG_DEFAULT_BUFFER_SIZE = 98304; -/* -* 50 buffers of 98304 bytes each gives us 4.9 seconds of audio, which is a good amount to have buffered at once. -*/ -constexpr int OGG_DEFAULT_BUFFER_COUNT = 50; - COggData::COggData() : m_Format(0), m_Frequency(0), m_OneShot(false), m_BuffersUsed(0) { @@ -48,7 +43,7 @@ COggData::~COggData() { AL_CHECK; if ( m_BuffersUsed > 0 ) - alDeleteBuffers(m_BuffersUsed, &m_Buffer[0]); + alDeleteBuffers(m_BuffersUsed, &m_Buffer.at(0)); AL_CHECK; m_BuffersUsed = 0; @@ -76,7 +71,7 @@ bool COggData::InitOggFile(const VfsPath& itemPath) SetFileName(itemPath); AL_CHECK; - alGenBuffers(OGG_DEFAULT_BUFFER_COUNT, m_Buffer); + alGenBuffers(m_Buffer.size(), m_Buffer.data()); ALenum err{alGetError()}; if (err != AL_NO_ERROR) @@ -85,13 +80,13 @@ bool COggData::InitOggFile(const VfsPath& itemPath) return false; } - m_BuffersUsed = FetchDataIntoBuffer(OGG_DEFAULT_BUFFER_COUNT, m_Buffer); + m_BuffersUsed = FetchDataIntoBuffer(m_Buffer.size(), m_Buffer.data()); if (m_FileFinished) { m_OneShot = true; if (m_BuffersUsed < OGG_DEFAULT_BUFFER_COUNT) { - alDeleteBuffers(OGG_DEFAULT_BUFFER_COUNT - m_BuffersUsed, &m_Buffer[m_BuffersUsed]); + alDeleteBuffers(OGG_DEFAULT_BUFFER_COUNT - m_BuffersUsed, &m_Buffer.at(m_BuffersUsed)); } } AL_CHECK; @@ -133,7 +128,7 @@ int COggData::FetchDataIntoBuffer(int count, ALuint* buffers) if (totalRet > 0) { buffersWritten++; - alBufferData(buffers[i], m_Format, PCMOut, (ALsizei)totalRet, (int)m_Frequency); + alBufferData( buffers[i], m_Format, PCMOut, static_cast(totalRet), static_cast(m_Frequency)); } } delete[] PCMOut; @@ -143,12 +138,12 @@ int COggData::FetchDataIntoBuffer(int count, ALuint* buffers) ALuint COggData::GetBuffer() { - return m_Buffer[0]; + return m_Buffer.at(0); } ALuint* COggData::GetBufferPtr() { - return m_Buffer; + return m_Buffer.data(); } #endif // CONFIG2_AUDIO diff --git a/source/soundmanager/data/OggData.h b/source/soundmanager/data/OggData.h index d84a35f89d..2f2dd131af 100644 --- a/source/soundmanager/data/OggData.h +++ b/source/soundmanager/data/OggData.h @@ -29,6 +29,12 @@ #include #include +#include + +/* +* 50 buffers of 98304 bytes each gives us 4.9 seconds of audio, which is a good amount to have buffered at once. +*/ +constexpr int OGG_DEFAULT_BUFFER_COUNT = 50; class COggData : public CSoundData { @@ -51,7 +57,7 @@ protected: OggStreamPtr m_Stream; bool m_FileFinished; bool m_OneShot; - ALuint m_Buffer[100]; + std::array m_Buffer{}; int m_BuffersUsed; void SetFormatAndFreq(int form, ALsizei freq);