mirror of
https://github.com/taglib/taglib.git
synced 2026-11-18 20:38:57 -05:00
Merge pull request #1453 from ufleisch/wav-32-bit
RIFF: avoid 32-bit offset_t overflow in chunk size clamping (#1446)
This commit is contained in:
@@ -9,14 +9,16 @@ jobs:
|
||||
build:
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, macos-latest, windows-latest, windows-2025]
|
||||
os: [ubuntu-latest, macos-latest, windows-latest, windows-2025, ubuntu-26.04]
|
||||
include:
|
||||
- os: windows-latest
|
||||
cmake_extra_args: '-DCMAKE_TOOLCHAIN_FILE="$env:VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake"'
|
||||
- os: windows-2025
|
||||
cmake_extra_args: '-G "MinGW Makefiles" -DCPPUNIT_INCLUDE_DIR="$env:GITHUB_WORKSPACE/pkg/usr/local/include" -DCPPUNIT_LIBRARIES="$($env:GITHUB_WORKSPACE -replace "\\", "/")/pkg/usr/local/lib/libcppunit.dll.a" -DCPPUNIT_INSTALLED_VERSION="1.15.1"'
|
||||
# The windows-2025 runner is used for MinGW.
|
||||
name: ${{ matrix.os == 'windows-2025' && 'mingw' || matrix.os }}
|
||||
- os: ubuntu-26.04
|
||||
cmake_extra_args: '-DCMAKE_C_FLAGS="-m32" -DCMAKE_CXX_FLAGS="-m32"'
|
||||
# The windows-2025 runner is used for MinGW. The ubuntu-26.04 runner is used for 32-bit.
|
||||
name: ${{ matrix.os == 'windows-2025' && 'mingw' || matrix.os == 'ubuntu-26.04' && 'i386' || matrix.os }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Checkout
|
||||
@@ -54,6 +56,13 @@ jobs:
|
||||
(cd build_cppunit && mingw32-make -j$(nproc) install DESTDIR=$(cd .. && pwd)/pkg)
|
||||
if: matrix.os == 'windows-2025'
|
||||
|
||||
- name: Set up Ubuntu i386
|
||||
run: |
|
||||
sudo dpkg --add-architecture i386
|
||||
sudo apt update
|
||||
sudo apt install -y gcc-multilib g++-multilib libc6-dev:i386 libcppunit-dev:i386 libutfcpp-dev zlib1g-dev:i386
|
||||
if: matrix.os == 'ubuntu-26.04'
|
||||
|
||||
- name: Configure
|
||||
run: >
|
||||
cmake -B${{github.workspace}}/build
|
||||
|
||||
@@ -53,7 +53,7 @@ void EBML::BinaryElement::setValue(const ByteVector& val)
|
||||
bool EBML::BinaryElement::read(File &file)
|
||||
{
|
||||
value = file.readBlock(dataSize);
|
||||
if(value.size() != dataSize) {
|
||||
if(static_cast<offset_t>(value.size()) != dataSize) {
|
||||
debug("Failed to read binary element");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ void EBML::FloatElement::setValue(FloatVariantType val)
|
||||
bool EBML::FloatElement::read(File &file)
|
||||
{
|
||||
const ByteVector buffer = file.readBlock(dataSize);
|
||||
if(buffer.size() != dataSize) {
|
||||
if(static_cast<offset_t>(buffer.size()) != dataSize) {
|
||||
debug("Failed to read EBML Float element");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ ByteVector EBML::MasterElement::render()
|
||||
buffer.append(data);
|
||||
if(minRenderSize) {
|
||||
if(const auto bufferSize = buffer.size();
|
||||
minRenderSize >= bufferSize + MIN_VOID_ELEMENT_SIZE)
|
||||
minRenderSize >= static_cast<offset_t>(bufferSize + MIN_VOID_ELEMENT_SIZE))
|
||||
buffer.append(VoidElement::renderSize(minRenderSize - bufferSize));
|
||||
}
|
||||
return buffer;
|
||||
|
||||
@@ -46,7 +46,7 @@ void EBML::StringElement::setValue(const String& val)
|
||||
bool EBML::StringElement::read(File &file)
|
||||
{
|
||||
ByteVector buffer = file.readBlock(dataSize);
|
||||
if(buffer.size() != dataSize) {
|
||||
if(static_cast<offset_t>(buffer.size()) != dataSize) {
|
||||
debug("Failed to read string");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ void EBML::UIntElement::setValue(unsigned long long val)
|
||||
bool EBML::UIntElement::read(File &file)
|
||||
{
|
||||
const ByteVector buffer = file.readBlock(dataSize);
|
||||
if(buffer.size() != dataSize) {
|
||||
if(static_cast<offset_t>(buffer.size()) != dataSize) {
|
||||
debug("Failed to read EBML Uint element");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -129,9 +129,9 @@ bool Matroska::Element::render()
|
||||
const auto beforeSize = sizeRenderedOrWritten();
|
||||
const auto data = renderInternal();
|
||||
setNeedsRender(false);
|
||||
if(const auto afterSize = data.size(); afterSize != beforeSize) {
|
||||
if(const auto afterSize = data.size(); static_cast<offset_t>(afterSize) != beforeSize) {
|
||||
if(e->writeStyle == WriteStyle::AvoidInsert && !e->isLastElement
|
||||
&& afterSize > beforeSize && beforeSize > 0) {
|
||||
&& static_cast<offset_t>(afterSize) > beforeSize && beforeSize > 0) {
|
||||
// Record old slot for void-overwrite, move element to end of segment.
|
||||
e->voidAtOffset = e->offset;
|
||||
e->voidAtSize = beforeSize;
|
||||
|
||||
@@ -44,7 +44,7 @@ bool Matroska::Segment::render()
|
||||
const auto beforeSize = sizeLength;
|
||||
auto data = renderInternal();
|
||||
setNeedsRender(false);
|
||||
if(auto afterSize = data.size(); afterSize != beforeSize) {
|
||||
if(auto afterSize = data.size(); static_cast<offset_t>(afterSize) != beforeSize) {
|
||||
sizeLength = 8;
|
||||
data = renderInternal();
|
||||
setNeedsRender(false);
|
||||
|
||||
@@ -905,7 +905,7 @@ namespace
|
||||
if(chunkOffsets.empty())
|
||||
return {};
|
||||
// Sample count cannot be > file length / 4 as every sample consumes 4 bytes in stsz
|
||||
if(sizeInfo.sampleCount == 0 || sizeInfo.sampleCount > file->length() / 4)
|
||||
if(sizeInfo.sampleCount == 0 || static_cast<offset_t>(sizeInfo.sampleCount) > file->length() / 4)
|
||||
return {};
|
||||
|
||||
// Read stsc entries
|
||||
@@ -1052,7 +1052,7 @@ namespace
|
||||
const unsigned int trakSize = file->readBlock(4).toUInt();
|
||||
if(static_cast<offset_t>(trefSize) != trefLen
|
||||
|| static_cast<offset_t>(trakSize) != audioTrak->length()
|
||||
|| trefSize < cutLen || trakSize < cutLen)
|
||||
|| static_cast<offset_t>(trefSize) < cutLen || static_cast<offset_t>(trakSize) < cutLen)
|
||||
return;
|
||||
|
||||
file->removeBlock(cutOff, cutLen);
|
||||
|
||||
@@ -109,7 +109,11 @@ unsigned int RIFF::File::chunkCount() const
|
||||
|
||||
unsigned int RIFF::File::chunkDataSize(unsigned int i) const
|
||||
{
|
||||
return static_cast<unsigned int>(std::min<offset_t>(chunkDataSize64(i), 0xffffffff));
|
||||
// Do the clamping in an unsigned 64-bit type: offset_t may be a 32-bit
|
||||
// signed off_t (e.g. i386 without _FILE_OFFSET_BITS=64), where 0xffffffff
|
||||
// would convert to -1 and std::min<offset_t>() would return it.
|
||||
const auto size64 = static_cast<unsigned long long>(std::max<offset_t>(chunkDataSize64(i), 0));
|
||||
return static_cast<unsigned int>(std::min<unsigned long long>(size64, 0xffffffffULL));
|
||||
}
|
||||
|
||||
offset_t RIFF::File::chunkDataSize64(unsigned int i) const
|
||||
@@ -163,7 +167,8 @@ ByteVector RIFF::File::chunkData(unsigned int i)
|
||||
|
||||
// A ByteVector is limited to 32 bits. The only chunk that can be larger is the
|
||||
// "data" chunk of an RF64 file, which no caller reads through this API.
|
||||
return readBlock(static_cast<size_t>(std::min<offset_t>(d->chunks[i].size, 0xffffffff)));
|
||||
const auto size64 = static_cast<unsigned long long>(std::max<offset_t>(d->chunks[i].size, 0));
|
||||
return readBlock(static_cast<size_t>(std::min<unsigned long long>(size64, 0xffffffffULL)));
|
||||
}
|
||||
|
||||
void RIFF::File::setChunkData(unsigned int i, const ByteVector &data)
|
||||
@@ -358,18 +363,20 @@ void RIFF::File::read()
|
||||
}
|
||||
|
||||
const offset_t available = fileLength - offset - 8;
|
||||
offset_t chunkSize = declaredSize;
|
||||
// Work in 64 bits: a declared size above 2 GiB does not fit a 32-bit
|
||||
// signed offset_t and would turn negative before the clamping below.
|
||||
long long chunkSize = declaredSize;
|
||||
|
||||
if(d->isLongForm && chnkName == "data" && declaredSize == 0xffffffff && d->dataSize64 > 0) {
|
||||
// ds64 stores an unsigned 64-bit size, while the I/O API uses signed
|
||||
// offsets. Clamp before converting so a crafted value cannot become a
|
||||
// negative offset or overflow the chunk extent arithmetic below.
|
||||
chunkSize = d->dataSize64 > static_cast<unsigned long long>(available)
|
||||
? available
|
||||
: static_cast<offset_t>(d->dataSize64);
|
||||
? static_cast<long long>(available)
|
||||
: static_cast<long long>(d->dataSize64);
|
||||
}
|
||||
|
||||
if(chunkSize > available) {
|
||||
if(chunkSize > static_cast<long long>(available)) {
|
||||
// Clamp to available bytes rather than rejecting the chunk outright.
|
||||
// Some encoders write a correct data chunk but with a slightly too-large
|
||||
// declared size, or place the data chunk outside the declared RIFF boundary.
|
||||
@@ -380,7 +387,7 @@ void RIFF::File::read()
|
||||
|
||||
Chunk chunk;
|
||||
chunk.name = chnkName;
|
||||
chunk.size = chunkSize;
|
||||
chunk.size = static_cast<offset_t>(chunkSize);
|
||||
chunk.offset = offset + 8;
|
||||
chunk.padding = 0;
|
||||
|
||||
|
||||
@@ -167,8 +167,10 @@ void RIFF::WAV::Properties::read(File *file)
|
||||
if(d->format != FORMAT_PCM && (d->format != FORMAT_IEEE_FLOAT || totalSamples != 0))
|
||||
d->sampleFrames = totalSamples;
|
||||
else if(d->channels > 0 && d->bitsPerSample > 0) {
|
||||
const offset_t frames = streamLength / (d->channels * ((d->bitsPerSample + 7) / 8));
|
||||
d->sampleFrames = static_cast<unsigned int>(std::min<offset_t>(frames, 0xffffffff));
|
||||
// Clamp in 64 bits: with a 32-bit offset_t, 0xffffffff would become -1.
|
||||
const auto frames = static_cast<unsigned long long>(
|
||||
std::max<offset_t>(streamLength / (d->channels * ((d->bitsPerSample + 7) / 8)), 0));
|
||||
d->sampleFrames = static_cast<unsigned int>(std::min<unsigned long long>(frames, 0xffffffffULL));
|
||||
}
|
||||
|
||||
if(d->sampleFrames > 0 && d->sampleRate > 0) {
|
||||
|
||||
@@ -73,7 +73,7 @@ ByteVector ByteVectorStream::readBlock(size_t length)
|
||||
void ByteVectorStream::writeBlock(const ByteVector &data)
|
||||
{
|
||||
unsigned int size = data.size();
|
||||
if(d->position + size > length()) {
|
||||
if(static_cast<offset_t>(d->position + size) > length()) {
|
||||
truncate(d->position + size);
|
||||
}
|
||||
memcpy(d->data.data() + d->position, data.data(), size);
|
||||
|
||||
Reference in New Issue
Block a user