more guards to corrupt voxel files

This commit is contained in:
ZappoMan 2014-09-05 01:40:41 -07:00
parent a189f077b8
commit 5ec98b8dec
4 changed files with 47 additions and 4 deletions

View file

@ -246,6 +246,13 @@ int Octree::readElementData(OctreeElement* destinationElement, const unsigned ch
return bytesAvailable; // assume we read the entire buffer...
}
if (destinationElement->getScale() < SMALLEST_REASONABLE_OCTREE_ELEMENT_SCALE) {
qDebug() << "UNEXPECTED: readElementData() destination element is unreasonably small ["
<< destinationElement->getScale() * (float)TREE_SCALE << " meters] "
<< " Discarding " << bytesAvailable << " remaining bytes.";
return bytesAvailable; // assume we read the entire buffer...
}
unsigned char colorInPacketMask = *nodeData;
bytesRead += sizeof(colorInPacketMask);
bytesLeftToRead -= sizeof(colorInPacketMask);
@ -366,7 +373,12 @@ void Octree::readBitstreamToTree(const unsigned char * bitstream, unsigned long
OctreeElement* bitstreamRootElement = nodeForOctalCode(args.destinationElement, (unsigned char *)bitstreamAt, NULL);
int numberOfThreeBitSectionsInStream = numberOfThreeBitSectionsInCode(bitstreamAt, bufferSizeBytes);
int octalCodeBytesInStream = bytesRequiredForCodeLength(numberOfThreeBitSectionsInStream);
if (numberOfThreeBitSectionsInStream == OVERFLOWED_OCTCODE_BUFFER) {
qDebug() << "UNEXPECTED: parsing of the octal code would overflow the buffer. This buffer is corrupt. Returning.";
return;
}
int numberOfThreeBitSectionsFromNode = numberOfThreeBitSectionsInCode(bitstreamRootElement->getOctalCode());
if (numberOfThreeBitSectionsInStream != numberOfThreeBitSectionsFromNode) {
@ -384,7 +396,7 @@ void Octree::readBitstreamToTree(const unsigned char * bitstream, unsigned long
}
}
int octalCodeBytes = octalCodeBytesInStream;
int octalCodeBytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInStream);
int theseBytesRead = 0;
theseBytesRead += octalCodeBytes;

View file

@ -11,11 +11,10 @@
#include <algorithm>
#include <QtCore/QDebug>
#include <QDebug>
#include <QImage>
#include <QRgb>
#include "VoxelTree.h"
#include "Tags.h"
@ -567,3 +566,26 @@ int VoxelTree::processEditPacketData(PacketType packetType, const unsigned char*
return 0;
}
}
class VoxelTreeDebugOperator : public RecurseOctreeOperator {
public:
virtual bool preRecursion(OctreeElement* element);
virtual bool postRecursion(OctreeElement* element) { return true; }
};
bool VoxelTreeDebugOperator::preRecursion(OctreeElement* element) {
VoxelTreeElement* treeElement = static_cast<VoxelTreeElement*>(element);
qDebug() << "VoxelTreeElement [" << treeElement << ":" << treeElement->getAACube() << "]";
qDebug() << " isLeaf:" << treeElement->isLeaf();
qDebug() << " color:" << treeElement->getColor()[0] << ", "
<< treeElement->getColor()[1] << ", "
<< treeElement->getColor()[2];
return true;
}
void VoxelTree::dumpTree() {
// First, look for the existing entity in the tree..
VoxelTreeDebugOperator theOperator;
recurseTreeWithOperator(&theOperator);
}

View file

@ -57,6 +57,8 @@ public:
const unsigned char* editData, int maxLength, const SharedNodePointer& node);
virtual bool recurseChildrenWithData() const { return false; }
virtual void dumpTree();
private:
// helper functions for nudgeSubTree
void recurseNodeForNudge(VoxelTreeElement* element, RecurseOctreeOperation operation, void* extraData);

View file

@ -73,6 +73,13 @@ OctreeElement::AppendState VoxelTreeElement::appendElementData(OctreePacketData*
int VoxelTreeElement::readElementDataFromBuffer(const unsigned char* data, int bytesLeftToRead,
ReadBitstreamToTreeParams& args) {
const int BYTES_PER_COLOR = 3;
if (bytesLeftToRead < BYTES_PER_COLOR) {
qDebug() << "UNEXPECTED: readElementDataFromBuffer() only had " << bytesLeftToRead << " bytes. "
"Not enough for meaningful data.";
return bytesLeftToRead;
}
// pull the color for this child
nodeColor newColor = { 128, 128, 128, 1};