Skip to content

Commit

Permalink
Small cleanup on BMPImageReader.cpp
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=253123
rdar://problem/106387835

Reviewed by Fujii Hironori.

Merge: https://chromium.googlesource.com/chromium/blink/+/a057769943417c61acd574458034ac6546ed6f6a

'm_headerOffset + m_infoHeader.biSize' is used often in BMPImageReader.cpp.
It is even recalulated several time in a same function. This patch cleans up
this kind of repeated code.

* Source/WebCore/platform/image-decoders/bmp/BMPImageReader.cpp:
(BMPImageReader::BMPImageReader):
(BMPImageReader::decodeBMP):
(BMPImageReader::readInfoHeaderSize):
(BMPImageReader::processBitmasks):
(BMPImageReader::processColorTable):
* Source/WebCore/platform/image-decoders/bmp/BMPImageReader.h: Remove 'm_tableSizeInBytes'

Canonical link: https://commits.webkit.org/265704@main
  • Loading branch information
Ahmad-S792 authored and Ahmad Saleem committed Jul 3, 2023
1 parent ab10a90 commit d156132
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
28 changes: 16 additions & 12 deletions Source/WebCore/platform/image-decoders/bmp/BMPImageReader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2009, Google Inc. All rights reserved.
* Copyright (c) 2008-2015 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -44,7 +44,6 @@ BMPImageReader::BMPImageReader(ScalableImageDecoder* parent, size_t decodedAndHe
, m_isTopDown(false)
, m_needToProcessBitmasks(false)
, m_needToProcessColorTable(false)
, m_tableSizeInBytes(0)
, m_seenNonZeroAlphaPixel(false)
, m_seenZeroAlphaPixel(false)
, m_andMaskState(usesAndMask ? NotYetDecoded : None)
Expand All @@ -59,8 +58,9 @@ bool BMPImageReader::decodeBMP(bool onlySize)
if (!m_infoHeader.biSize && !readInfoHeaderSize())
return false;

const size_t headerEnd = m_headerOffset + m_infoHeader.biSize;
// Read and process info header.
if ((m_decodedOffset < (m_headerOffset + m_infoHeader.biSize)) && !processInfoHeader())
if ((m_decodedOffset < headerEnd) && !processInfoHeader())
return false;

// processInfoHeader() set the size, so if that's all we needed, we're done.
Expand Down Expand Up @@ -134,7 +134,8 @@ bool BMPImageReader::readInfoHeaderSize()
// Don't allow the header to overflow (which would be harmless here, but
// problematic or at least confusing in other places), or to overrun the
// image data.
if (((m_headerOffset + m_infoHeader.biSize) < m_headerOffset) || (m_imgDataOffset && (m_imgDataOffset < (m_headerOffset + m_infoHeader.biSize))))
const size_t headerEnd = m_headerOffset + m_infoHeader.biSize;
if ((headerEnd < m_headerOffset) || (m_imgDataOffset && (m_imgDataOffset < headerEnd)))
return m_parent->setFailed();

// See if this is a header size we understand:
Expand Down Expand Up @@ -392,20 +393,22 @@ bool BMPImageReader::processBitmasks()
// we read the info header.

// Fail if we don't have enough file space for the bitmasks.
static const size_t SIZEOF_BITMASKS = 12;
if (((m_headerOffset + m_infoHeader.biSize + SIZEOF_BITMASKS) < (m_headerOffset + m_infoHeader.biSize)) || (m_imgDataOffset && (m_imgDataOffset < (m_headerOffset + m_infoHeader.biSize + SIZEOF_BITMASKS))))
const size_t headerEnd = m_headerOffset + m_infoHeader.biSize;
static constexpr size_t bitmasksSize = 12;
const size_t bitmasksEnd = headerEnd + bitmasksSize;
if ((bitmasksEnd < headerEnd) || (m_imgDataOffset && (m_imgDataOffset < bitmasksEnd)))
return m_parent->setFailed();

// Read bitmasks.
if ((m_data->size() - m_decodedOffset) < SIZEOF_BITMASKS)
if ((m_data->size() - m_decodedOffset) < bitmasksSize)
return false;
m_bitMasks[0] = readUint32(0);
m_bitMasks[1] = readUint32(4);
m_bitMasks[2] = readUint32(8);
// No alpha in anything other than Windows V4+.
m_bitMasks[3] = 0;

m_decodedOffset += SIZEOF_BITMASKS;
m_decodedOffset += bitmasksSize;
}

// We've now decoded all the non-image data we care about. Skip anything
Expand Down Expand Up @@ -462,14 +465,15 @@ bool BMPImageReader::processBitmasks()

bool BMPImageReader::processColorTable()
{
m_tableSizeInBytes = m_infoHeader.biClrUsed * (m_isOS21x ? 3 : 4);

// Fail if we don't have enough file space for the color table.
if (((m_headerOffset + m_infoHeader.biSize + m_tableSizeInBytes) < (m_headerOffset + m_infoHeader.biSize)) || (m_imgDataOffset && (m_imgDataOffset < (m_headerOffset + m_infoHeader.biSize + m_tableSizeInBytes))))
const size_t headerEnd = m_headerOffset + m_infoHeader.biSize;
const size_t tableSizeInBytes = m_infoHeader.biClrUsed * (m_isOS21x ? 3 : 4);
const size_t tableEnd = headerEnd + tableSizeInBytes;
if ((tableEnd < headerEnd) || (m_imgDataOffset && (m_imgDataOffset < tableEnd)))
return m_parent->setFailed();

// Read color table.
if ((m_decodedOffset > m_data->size()) || ((m_data->size() - m_decodedOffset) < m_tableSizeInBytes))
if ((m_decodedOffset > m_data->size()) || ((m_data->size() - m_decodedOffset) < tableSizeInBytes))
return false;
m_colorTable.resize(m_infoHeader.biClrUsed);
for (size_t i = 0; i < m_infoHeader.biClrUsed; ++i) {
Expand Down
3 changes: 1 addition & 2 deletions Source/WebCore/platform/image-decoders/bmp/BMPImageReader.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2009, Google Inc. All rights reserved.
* Copyright (c) 2008-2015 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -325,7 +325,6 @@ class BMPImageReader {
int m_bitShiftsLeft[4];

// The color palette, for paletted formats.
size_t m_tableSizeInBytes;
Vector<RGBTriple> m_colorTable;

// The coordinate to which we've decoded the image.
Expand Down

0 comments on commit d156132

Please sign in to comment.