Skip to content

Commit

Permalink
fixes buffer overflow in color in pasteTiles()
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoquesada committed Jan 19, 2017
1 parent cc1ca53 commit a96ecac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/commands.cpp
Expand Up @@ -101,6 +101,8 @@ PasteCommand::PasteCommand(State* state, int charIndex, const State::CopyRange&
if (copyRange.type == State::CopyRange::CHARS || copyRange.type == State::CopyRange::TILES)
{
sizeToCopy = State::CHAR_BUFFER_SIZE + State::TILE_COLORS_BUFFER_SIZE;
Q_ASSERT(copyRange.bufferSize == sizeToCopy && "Invalid bufferSize");

_copyBuffer = (quint8*)malloc(sizeToCopy);
_origBuffer = (quint8*)malloc(sizeToCopy);
}
Expand Down
9 changes: 6 additions & 3 deletions src/state.cpp
Expand Up @@ -694,10 +694,11 @@ void State::_setMapSize(const QSize& mapSize)
{
const int newSizeInBytes = mapSize.width() * mapSize.height();
quint8* newMap = (quint8*) malloc(newSizeInBytes);
Q_ASSERT(newMap && "No memory");

for (int i=0; i<newSizeInBytes; ++i)
newMap[i] = _tileIndex;

Q_ASSERT(newMap && "No memory");

for (int row=0; row<mapSize.height(); ++row)
{
Expand Down Expand Up @@ -984,8 +985,10 @@ void State::_pasteTiles(int charIndex, const CopyRange& copyRange, const quint8*
int srcidx = (tileSrcIdx + i + srcskip) * interleavedFactorSrc;
int dstidx = (tileDstIdx + i + dstskip) * interleavedFactorDst;

// copy colors
_tileColors[tileDstIdx + i + dstskip] = colorsBuffer[tileSrcIdx + i + srcskip];
int colorIdx = tileDstIdx + i + dstskip;
// avoid overflow
if (colorIdx < 256)
_tileColors[colorIdx] = colorsBuffer[tileSrcIdx + i + srcskip];

// when interleaved, break the copy to prevent ugly artifacts
if (_tileProperties.interleaved != 1 && dstidx >= (256 / tileSize))
Expand Down

1 comment on commit a96ecac

@ricardoquesada
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes issue #27

Please sign in to comment.