Skip to content

Commit 9153c12

Browse files
authored
Merge pull request #1701 from sideeffects/sendupstream_vdbioclamp
Fix for potential crash when reading invalid .vdb files.
2 parents 8637382 + f02e7e1 commit 9153c12

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

openvdb/openvdb/io/Compression.cc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,23 @@ unzipFromStream(std::istream& is, char* data, size_t numBytes)
123123
{
124124
// Read the size of the compressed data.
125125
// A negative size indicates uncompressed data.
126-
Int64 numZippedBytes;
126+
Int64 numZippedBytes{0};
127127
is.read(reinterpret_cast<char*>(&numZippedBytes), 8);
128+
if (!is.good())
129+
OPENVDB_THROW(RuntimeError, "Stream failure reading the size of a zip chunk");
128130

129131
if (numZippedBytes <= 0) {
132+
// Check for an error
133+
if (size_t(-numZippedBytes) != numBytes) {
134+
OPENVDB_THROW(RuntimeError, "Expected to read a " << numBytes
135+
<< "-byte chunk, got a " << -numZippedBytes << "-byte chunk");
136+
}
130137
// Read the uncompressed data.
131138
if (data == nullptr) {
132139
is.seekg(-numZippedBytes, std::ios_base::cur);
133140
} else {
134141
is.read(data, -numZippedBytes);
135142
}
136-
if (size_t(-numZippedBytes) != numBytes) {
137-
OPENVDB_THROW(RuntimeError, "Expected to read a " << numBytes
138-
<< "-byte chunk, got a " << -numZippedBytes << "-byte chunk");
139-
}
140143
} else {
141144
if (data == nullptr) {
142145
// Seek over the compressed data.
@@ -268,20 +271,24 @@ bloscFromStream(std::istream& is, char* data, size_t numBytes)
268271
{
269272
// Read the size of the compressed data.
270273
// A negative size indicates uncompressed data.
271-
Int64 numCompressedBytes;
274+
Int64 numCompressedBytes{0};
272275
is.read(reinterpret_cast<char*>(&numCompressedBytes), 8);
273276

277+
if (!is.good())
278+
OPENVDB_THROW(RuntimeError, "Stream failure reading the size of a blosc chunk");
279+
274280
if (numCompressedBytes <= 0) {
281+
// Check for an error
282+
if (size_t(-numCompressedBytes) != numBytes) {
283+
OPENVDB_THROW(RuntimeError, "Expected to read a " << numBytes
284+
<< "-byte uncompressed chunk, got a " << -numCompressedBytes << "-byte chunk");
285+
}
275286
// Read the uncompressed data.
276287
if (data == nullptr) {
277288
is.seekg(-numCompressedBytes, std::ios_base::cur);
278289
} else {
279290
is.read(data, -numCompressedBytes);
280291
}
281-
if (size_t(-numCompressedBytes) != numBytes) {
282-
OPENVDB_THROW(RuntimeError, "Expected to read a " << numBytes
283-
<< "-byte uncompressed chunk, got a " << -numCompressedBytes << "-byte chunk");
284-
}
285292
} else {
286293
if (data == nullptr) {
287294
// Seek over the compressed data.

pendingchanges/iofix.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Bug Fixes:
2+
- Fix potential crash reading corrupt .vdb files with invalid
3+
blosc or zip chunks. [Fix thanks to Matthias Ueberheide]

0 commit comments

Comments
 (0)