Changeset - r26711:a4f8e9c41dea
[Not reviewed]
master
0 26 0
Patric Stout - 17 months ago 2023-01-02 20:30:02
truebrain@openttd.org
Codechange: address CodeQL issue "Multiplication result converted to larger type" (#10306)

Most are very unlikely to ever be triggered in our codebase; two
stand out: linkgraph and money cheat. Those, potentially, could
wrap earlier than expected.
26 files changed with 74 insertions and 66 deletions:
0 comments (0 inline, 0 general)
src/blitter/32bpp_anim.cpp
Show inline comments
 
@@ -475,15 +475,15 @@ void Blitter_32bppAnim::ScrollBuffer(voi
 
		}
 
	}
 

	
 
	Blitter_32bppBase::ScrollBuffer(video, left, top, width, height, scroll_x, scroll_y);
 
}
 

	
 
int Blitter_32bppAnim::BufferSize(int width, int height)
 
size_t Blitter_32bppAnim::BufferSize(uint width, uint height)
 
{
 
	return width * height * (sizeof(uint32) + sizeof(uint16));
 
	return (sizeof(uint32) + sizeof(uint16)) * width * height;
 
}
 

	
 
void Blitter_32bppAnim::PaletteAnimate(const Palette &palette)
 
{
 
	assert(!_screen_disable_anim);
 

	
src/blitter/32bpp_anim.hpp
Show inline comments
 
@@ -40,13 +40,13 @@ public:
 
	void SetPixel(void *video, int x, int y, uint8 colour) override;
 
	void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash) override;
 
	void DrawRect(void *video, int width, int height, uint8 colour) override;
 
	void CopyFromBuffer(void *video, const void *src, int width, int height) override;
 
	void CopyToBuffer(const void *video, void *dst, int width, int height) override;
 
	void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) override;
 
	int BufferSize(int width, int height) override;
 
	size_t BufferSize(uint width, uint height) override;
 
	void PaletteAnimate(const Palette &palette) override;
 
	Blitter::PaletteAnimation UsePaletteAnimation() override;
 

	
 
	const char *GetName() override { return "32bpp-anim"; }
 
	int GetBytesPerPixel() override { return 6; }
 
	void PostResize() override;
src/blitter/32bpp_base.cpp
Show inline comments
 
@@ -137,15 +137,15 @@ void Blitter_32bppBase::ScrollBuffer(voi
 
			src += _screen.pitch;
 
			dst += _screen.pitch;
 
		}
 
	}
 
}
 

	
 
int Blitter_32bppBase::BufferSize(int width, int height)
 
size_t Blitter_32bppBase::BufferSize(uint width, uint height)
 
{
 
	return width * height * sizeof(uint32);
 
	return sizeof(uint32) * width * height;
 
}
 

	
 
void Blitter_32bppBase::PaletteAnimate(const Palette &palette)
 
{
 
	/* By default, 32bpp doesn't have palette animation */
 
}
src/blitter/32bpp_base.hpp
Show inline comments
 
@@ -24,13 +24,13 @@ public:
 
	void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash) override;
 
	void DrawRect(void *video, int width, int height, uint8 colour) override;
 
	void CopyFromBuffer(void *video, const void *src, int width, int height) override;
 
	void CopyToBuffer(const void *video, void *dst, int width, int height) override;
 
	void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch) override;
 
	void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) override;
 
	int BufferSize(int width, int height) override;
 
	size_t BufferSize(uint width, uint height) override;
 
	void PaletteAnimate(const Palette &palette) override;
 
	Blitter::PaletteAnimation UsePaletteAnimation() override;
 
	int GetBytesPerPixel() override { return 4; }
 

	
 
	/**
 
	 * Look up the colour in the current palette.
src/blitter/40bpp_anim.cpp
Show inline comments
 
@@ -28,15 +28,16 @@ static const Colour _black_colour(0, 0, 
 

	
 
void Blitter_40bppAnim::SetPixel(void *video, int x, int y, uint8 colour)
 
{
 
	if (_screen_disable_anim) {
 
		Blitter_32bppOptimized::SetPixel(video, x, y, colour);
 
	} else {
 
		*((Colour *)video + x + y * _screen.pitch) = _black_colour;
 
		size_t y_offset = static_cast<size_t>(y) * _screen.pitch;
 
		*((Colour *)video + x + y_offset) = _black_colour;
 

	
 
		VideoDriver::GetInstance()->GetAnimBuffer()[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * _screen.pitch] = colour;
 
		VideoDriver::GetInstance()->GetAnimBuffer()[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y_offset] = colour;
 
	}
 
}
 

	
 
void Blitter_40bppAnim::DrawRect(void *video, int width, int height, uint8 colour)
 
{
 
	if (_screen_disable_anim) {
 
@@ -497,15 +498,15 @@ void Blitter_40bppAnim::ScrollBuffer(voi
 
		}
 
	}
 

	
 
	Blitter_32bppBase::ScrollBuffer(video, left, top, width, height, scroll_x, scroll_y);
 
}
 

	
 
int Blitter_40bppAnim::BufferSize(int width, int height)
 
size_t Blitter_40bppAnim::BufferSize(uint width, uint height)
 
{
 
	return width * height * (sizeof(uint32) + sizeof(uint8));
 
	return (sizeof(uint32) + sizeof(uint8)) * width * height;
 
}
 

	
 
Blitter::PaletteAnimation Blitter_40bppAnim::UsePaletteAnimation()
 
{
 
	return Blitter::PALETTE_ANIMATION_VIDEO_BACKEND;
 
}
src/blitter/40bpp_anim.hpp
Show inline comments
 
@@ -26,13 +26,13 @@ public:
 
	void CopyToBuffer(const void *video, void *dst, int width, int height) override;
 
	void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch) override;
 
	void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) override;
 
	void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override;
 
	void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal) override;
 
	Sprite *Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator) override;
 
	int BufferSize(int width, int height) override;
 
	size_t BufferSize(uint width, uint height) override;
 
	Blitter::PaletteAnimation UsePaletteAnimation() override;
 
	bool NeedsAnimationBuffer() override;
 

	
 
	const char *GetName()  override { return "40bpp-anim"; }
 
	int GetBytesPerPixel()  override { return 5; }
 

	
src/blitter/8bpp_base.cpp
Show inline comments
 
@@ -141,15 +141,15 @@ void Blitter_8bppBase::ScrollBuffer(void
 
			src += _screen.pitch;
 
			dst += _screen.pitch;
 
		}
 
	}
 
}
 

	
 
int Blitter_8bppBase::BufferSize(int width, int height)
 
size_t Blitter_8bppBase::BufferSize(uint width, uint height)
 
{
 
	return width * height;
 
	return static_cast<size_t>(width) * height;
 
}
 

	
 
void Blitter_8bppBase::PaletteAnimate(const Palette &palette)
 
{
 
	/* Video backend takes care of the palette animation */
 
}
src/blitter/8bpp_base.hpp
Show inline comments
 
@@ -22,13 +22,13 @@ public:
 
	void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash) override;
 
	void DrawRect(void *video, int width, int height, uint8 colour) override;
 
	void CopyFromBuffer(void *video, const void *src, int width, int height) override;
 
	void CopyToBuffer(const void *video, void *dst, int width, int height) override;
 
	void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch) override;
 
	void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) override;
 
	int BufferSize(int width, int height) override;
 
	size_t BufferSize(uint width, uint height) override;
 
	void PaletteAnimate(const Palette &palette) override;
 
	Blitter::PaletteAnimation UsePaletteAnimation() override;
 
	int GetBytesPerPixel() override { return 1; }
 
};
 

	
 
#endif /* BLITTER_8BPP_BASE_HPP */
src/blitter/base.hpp
Show inline comments
 
@@ -167,13 +167,13 @@ public:
 
	/**
 
	 * Calculate how much memory there is needed for an image of this size in the video-buffer.
 
	 * @param width The width of the buffer-to-be.
 
	 * @param height The height of the buffer-to-be.
 
	 * @return The size needed for the buffer.
 
	 */
 
	virtual int BufferSize(int width, int height) = 0;
 
	virtual size_t BufferSize(uint width, uint height) = 0;
 

	
 
	/**
 
	 * Called when the 8bpp palette is changed; you should redraw all pixels on the screen that
 
	 *  are equal to the 8bpp palette indexes 'first_dirty' to 'first_dirty + count_dirty'.
 
	 * @param palette The new palette.
 
	 */
src/blitter/null.hpp
Show inline comments
 
@@ -24,13 +24,13 @@ public:
 
	void DrawRect(void *video, int width, int height, uint8 colour) override {};
 
	void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash) override {};
 
	void CopyFromBuffer(void *video, const void *src, int width, int height) override {};
 
	void CopyToBuffer(const void *video, void *dst, int width, int height) override {};
 
	void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch) override {};
 
	void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) override {};
 
	int BufferSize(int width, int height) override { return 0; };
 
	size_t BufferSize(uint width, uint height) override { return 0; };
 
	void PaletteAnimate(const Palette &palette) override { };
 
	Blitter::PaletteAnimation UsePaletteAnimation() override { return Blitter::PALETTE_ANIMATION_NONE; };
 

	
 
	const char *GetName() override { return "null"; }
 
	int GetBytesPerPixel() override { return 0; }
 
};
src/bmp.cpp
Show inline comments
 
@@ -387,13 +387,13 @@ bool BmpReadHeader(BmpBuffer *buffer, Bm
 
 * 1 bpp and 4 bpp bitmaps are converted to 8 bpp bitmaps
 
 */
 
bool BmpReadBitmap(BmpBuffer *buffer, BmpInfo *info, BmpData *data)
 
{
 
	assert(info != nullptr && data != nullptr);
 

	
 
	data->bitmap = CallocT<byte>(info->width * info->height * ((info->bpp == 24) ? 3 : 1));
 
	data->bitmap = CallocT<byte>(static_cast<size_t>(info->width) * info->height * ((info->bpp == 24) ? 3 : 1));
 

	
 
	/* Load image */
 
	SetStreamOffset(buffer, info->offset);
 
	switch (info->compression) {
 
	case 0: // no compression
 
		switch (info->bpp) {
src/cheat_gui.cpp
Show inline comments
 
@@ -53,13 +53,13 @@ static int32 _money_cheat_amount = 10000
 
 * @param p1 not used.
 
 * @param p2 is -1 or +1 (down/up)
 
 * @return Amount of money cheat.
 
 */
 
static int32 ClickMoneyCheat(int32 p1, int32 p2)
 
{
 
	Command<CMD_MONEY_CHEAT>::Post(p2 * _money_cheat_amount);
 
	Command<CMD_MONEY_CHEAT>::Post(Money(_money_cheat_amount) * p2);
 
	return _money_cheat_amount;
 
}
 

	
 
/**
 
 * Handle changing of company.
 
 * @param p1 company to set to
src/fontcache/freetypefontcache.cpp
Show inline comments
 
@@ -237,13 +237,13 @@ const Sprite *FreeTypeFontCache::Interna
 

	
 
	/* Limit glyph size to prevent overflows later on. */
 
	if (width > MAX_GLYPH_DIM || height > MAX_GLYPH_DIM) usererror("Font glyph is too large");
 

	
 
	/* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
 
	SpriteLoader::Sprite sprite;
 
	sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
 
	sprite.AllocateData(ZOOM_LVL_NORMAL, static_cast<size_t>(width) * height);
 
	sprite.type = ST_FONT;
 
	sprite.colours = (aa ? SCC_PAL | SCC_ALPHA : SCC_PAL);
 
	sprite.width = width;
 
	sprite.height = height;
 
	sprite.x_offs = slot->bitmap_left;
 
	sprite.y_offs = this->ascender - slot->bitmap_top;
src/gfx.cpp
Show inline comments
 
@@ -1219,32 +1219,35 @@ std::unique_ptr<uint32[]> DrawSpriteToRg
 
	if (blitter->GetScreenDepth() != 8 && blitter->GetScreenDepth() != 32) return nullptr;
 

	
 
	/* Gather information about the sprite to write, reserve memory */
 
	const SpriteID real_sprite = GB(spriteId, 0, SPRITE_WIDTH);
 
	const Sprite *sprite = GetSprite(real_sprite, ST_NORMAL);
 
	Dimension dim = GetSpriteSize(real_sprite, nullptr, zoom);
 
	std::unique_ptr<uint32[]> result(new uint32[dim.width * dim.height]);
 
	size_t dim_size = static_cast<size_t>(dim.width) * dim.height;
 
	std::unique_ptr<uint32[]> result(new uint32[dim_size]);
 
	/* Set buffer to fully transparent. */
 
	MemSetT(result.get(), 0, dim.width * dim.height);
 
	MemSetT(result.get(), 0, dim_size);
 

	
 
	/* Prepare new DrawPixelInfo - Normally this would be the screen but we want to draw to another buffer here.
 
	 * Normally, pitch would be scaled screen width, but in our case our "screen" is only the sprite width wide. */
 
	DrawPixelInfo dpi;
 
	dpi.dst_ptr = result.get();
 
	dpi.pitch = dim.width;
 
	dpi.left = 0;
 
	dpi.top = 0;
 
	dpi.width = dim.width;
 
	dpi.height = dim.height;
 
	dpi.zoom = zoom;
 

	
 
	dim_size = static_cast<size_t>(dim.width) * dim.height;
 

	
 
	/* If the current blitter is a paletted blitter, we have to render to an extra buffer and resolve the palette later. */
 
	std::unique_ptr<byte[]> pal_buffer{};
 
	if (blitter->GetScreenDepth() == 8) {
 
		pal_buffer.reset(new byte[dim.width * dim.height]);
 
		MemSetT(pal_buffer.get(), 0, dim.width * dim.height);
 
		pal_buffer.reset(new byte[dim_size]);
 
		MemSetT(pal_buffer.get(), 0, dim_size);
 

	
 
		dpi.dst_ptr = pal_buffer.get();
 
	}
 

	
 
	/* Temporarily disable screen animations while blitting - This prevents 40bpp_anim from writing to the animation buffer. */
 
	Backup<bool> disable_anim(_screen_disable_anim, true, FILE_LINE);
 
@@ -1252,13 +1255,13 @@ std::unique_ptr<uint32[]> DrawSpriteToRg
 
	disable_anim.Restore();
 

	
 
	if (blitter->GetScreenDepth() == 8) {
 
		/* Resolve palette. */
 
		uint32 *dst = result.get();
 
		const byte *src = pal_buffer.get();
 
		for (size_t i = 0; i < dim.height * dim.width; ++i) {
 
		for (size_t i = 0; i < dim_size; ++i) {
 
			*dst++ = _cur_palette.palette[*src++].data;
 
		}
 
	}
 

	
 
	return result;
 
}
 
@@ -1503,13 +1506,13 @@ void GetBroadestDigit(uint *front, uint 
 
	}
 
}
 

	
 
void ScreenSizeChanged()
 
{
 
	_dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
 
	_dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
 
	_dirty_blocks = ReallocT<byte>(_dirty_blocks, static_cast<size_t>(_dirty_bytes_per_line) * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
 

	
 
	/* check the dirty rect */
 
	if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
 
	if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
 

	
 
	/* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
src/ground_vehicle.cpp
Show inline comments
 
@@ -138,13 +138,13 @@ int GroundVehicle<T, Type>::GetAccelerat
 
		/* Static resistance plus rolling friction. */
 
		resistance = this->gcache.cached_axle_resistance;
 
		resistance += mass * v->GetRollingFriction();
 
	}
 
	/* Air drag; the air drag coefficient is in an arbitrary NewGRF-unit,
 
	 * so we need some magic conversion factor. */
 
	resistance += (area * this->gcache.cached_air_drag * speed * speed) / 1000;
 
	resistance += static_cast<int64>(area) * this->gcache.cached_air_drag * speed * speed / 1000;
 

	
 
	resistance += this->GetSlopeResistance();
 

	
 
	/* This value allows to know if the vehicle is accelerating or braking. */
 
	AccelStatus mode = v->GetAccelerationStatus();
 

	
src/heightmap.cpp
Show inline comments
 
@@ -185,13 +185,13 @@ static bool ReadHeightmapPNG(const char 
 
		fclose(fp);
 
		png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
 
		return false;
 
	}
 

	
 
	if (map != nullptr) {
 
		*map = MallocT<byte>(width * height);
 
		*map = MallocT<byte>(static_cast<size_t>(width) * height);
 
		ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
 
	}
 

	
 
	*x = width;
 
	*y = height;
 

	
 
@@ -300,13 +300,13 @@ static bool ReadHeightmapBMP(const char 
 
			ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
 
			fclose(f);
 
			BmpDestroyData(&data);
 
			return false;
 
		}
 

	
 
		*map = MallocT<byte>(info.width * info.height);
 
		*map = MallocT<byte>(static_cast<size_t>(info.width) * info.height);
 
		ReadHeightmapBMPImageData(*map, &info, &data);
 
	}
 

	
 
	BmpDestroyData(&data);
 

	
 
	*x = info.width;
src/linkgraph/linkgraph.cpp
Show inline comments
 
@@ -201,13 +201,13 @@ void LinkGraph::Node::AddEdge(NodeID to,
 
{
 
	assert(this->index != to);
 
	BaseEdge &edge = this->edges[to];
 
	BaseEdge &first = this->edges[this->index];
 
	edge.capacity = capacity;
 
	edge.usage = usage;
 
	edge.travel_time_sum = travel_time * capacity;
 
	edge.travel_time_sum = static_cast<uint64>(travel_time) * capacity;
 
	edge.next_edge = first.next_edge;
 
	first.next_edge = to;
 
	if (mode & EUM_UNRESTRICTED)  edge.last_unrestricted_update = _date;
 
	if (mode & EUM_RESTRICTED) edge.last_restricted_update = _date;
 
}
 

	
 
@@ -272,24 +272,24 @@ void LinkGraph::Edge::Update(uint capaci
 
{
 
	assert(this->edge.capacity > 0);
 
	assert(capacity >= usage);
 

	
 
	if (mode & EUM_INCREASE) {
 
		if (this->edge.travel_time_sum == 0) {
 
			this->edge.travel_time_sum = (this->edge.capacity + capacity) * travel_time;
 
			this->edge.travel_time_sum = static_cast<uint64>(this->edge.capacity + capacity) * travel_time;
 
		} else if (travel_time == 0) {
 
			this->edge.travel_time_sum += this->edge.travel_time_sum / this->edge.capacity * capacity;
 
		} else {
 
			this->edge.travel_time_sum += travel_time * capacity;
 
			this->edge.travel_time_sum += static_cast<uint64>(travel_time) * capacity;
 
		}
 
		this->edge.capacity += capacity;
 
		this->edge.usage += usage;
 
	} else if (mode & EUM_REFRESH) {
 
		if (this->edge.travel_time_sum == 0) {
 
			this->edge.capacity = std::max(this->edge.capacity, capacity);
 
			this->edge.travel_time_sum = travel_time * this->edge.capacity;
 
			this->edge.travel_time_sum = static_cast<uint64>(travel_time) * this->edge.capacity;
 
		} else if (capacity > this->edge.capacity) {
 
			this->edge.travel_time_sum = this->edge.travel_time_sum / this->edge.capacity * capacity;
 
			this->edge.capacity = capacity;
 
		}
 
		this->edge.usage = std::max(this->edge.usage, usage);
 
	}
src/network/network_chat_gui.cpp
Show inline comments
 
@@ -105,13 +105,13 @@ void CDECL NetworkAddChatMessage(TextCol
 

	
 
/** Initialize all font-dependent chat box sizes. */
 
void NetworkReInitChatBoxSize()
 
{
 
	_chatmsg_box.y       = 3 * FONT_HEIGHT_NORMAL;
 
	_chatmsg_box.height  = MAX_CHAT_MESSAGES * (FONT_HEIGHT_NORMAL + ScaleGUITrad(NETWORK_CHAT_LINE_SPACING)) + ScaleGUITrad(4);
 
	_chatmessage_backup  = ReallocT(_chatmessage_backup, _chatmsg_box.width * _chatmsg_box.height * BlitterFactory::GetCurrentBlitter()->GetBytesPerPixel());
 
	_chatmessage_backup  = ReallocT(_chatmessage_backup, static_cast<size_t>(_chatmsg_box.width) * _chatmsg_box.height * BlitterFactory::GetCurrentBlitter()->GetBytesPerPixel());
 
}
 

	
 
/** Initialize all buffers of the chat visualisation. */
 
void NetworkInitChatMessage()
 
{
 
	MAX_CHAT_MESSAGES    = _settings_client.gui.network_chat_box_height;
 
@@ -211,13 +211,13 @@ void NetworkDrawChatMessage()
 
	}
 
	if (x + width >= _screen.width) {
 
		width = _screen.width - x;
 
	}
 
	if (width <= 0 || height <= 0) return;
 

	
 
	assert(blitter->BufferSize(width, height) <= (int)(_chatmsg_box.width * _chatmsg_box.height * blitter->GetBytesPerPixel()));
 
	assert(blitter->BufferSize(width, height) <= static_cast<size_t>(_chatmsg_box.width) * _chatmsg_box.height * blitter->GetBytesPerPixel());
 

	
 
	/* Make a copy of the screen as it is before painting (for undraw) */
 
	blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height);
 

	
 
	_cur_dpi = &_screen; // switch to _screen painting
 

	
src/screenshot.cpp
Show inline comments
 
@@ -134,13 +134,13 @@ static bool MakeBMPImage(const char *nam
 
	/* Size of palette. Only present for 8bpp mode */
 
	uint pal_size = pixelformat == 8 ? sizeof(RgbQuad) * 256 : 0;
 

	
 
	/* Setup the file header */
 
	BitmapFileHeader bfh;
 
	bfh.type = TO_LE16('MB');
 
	bfh.size = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size + bytewidth * h);
 
	bfh.size = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size + static_cast<size_t>(bytewidth) * h);
 
	bfh.reserved = 0;
 
	bfh.off_bits = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size);
 

	
 
	/* Setup the info header */
 
	BitmapInfoHeader bih;
 
	bih.size = TO_LE32(sizeof(BitmapInfoHeader));
 
@@ -371,13 +371,13 @@ static bool MakePNGImage(const char *nam
 
	}
 

	
 
	/* use by default 64k temp memory */
 
	maxlines = Clamp(65536 / w, 16, 128);
 

	
 
	/* now generate the bitmap bits */
 
	void *buff = CallocT<uint8>(w * maxlines * bpp); // by default generate 128 lines at a time.
 
	void *buff = CallocT<uint8>(static_cast<size_t>(w) * maxlines * bpp); // by default generate 128 lines at a time.
 

	
 
	y = 0;
 
	do {
 
		/* determine # lines to write */
 
		n = std::min(h - y, maxlines);
 

	
 
@@ -478,13 +478,13 @@ static bool MakePCXImage(const char *nam
 
	}
 

	
 
	/* use by default 64k temp memory */
 
	maxlines = Clamp(65536 / w, 16, 128);
 

	
 
	/* now generate the bitmap bits */
 
	uint8 *buff = CallocT<uint8>(w * maxlines); // by default generate 128 lines at a time.
 
	uint8 *buff = CallocT<uint8>(static_cast<size_t>(w) * maxlines); // by default generate 128 lines at a time.
 

	
 
	y = 0;
 
	do {
 
		/* determine # lines to write */
 
		uint n = std::min(h - y, maxlines);
 
		uint i;
src/spritecache.cpp
Show inline comments
 
@@ -233,13 +233,13 @@ static bool ResizeSpriteIn(SpriteLoader:
 
	sprite[tgt].width  = sprite[src].width  * scaled_1;
 
	sprite[tgt].height = sprite[src].height * scaled_1;
 
	sprite[tgt].x_offs = sprite[src].x_offs * scaled_1;
 
	sprite[tgt].y_offs = sprite[src].y_offs * scaled_1;
 
	sprite[tgt].colours = sprite[src].colours;
 

	
 
	sprite[tgt].AllocateData(tgt, sprite[tgt].width * sprite[tgt].height);
 
	sprite[tgt].AllocateData(tgt, static_cast<size_t>(sprite[tgt].width) * sprite[tgt].height);
 

	
 
	SpriteLoader::CommonPixel *dst = sprite[tgt].data;
 
	for (int y = 0; y < sprite[tgt].height; y++) {
 
		const SpriteLoader::CommonPixel *src_ln = &sprite[src].data[y / scaled_1 * sprite[src].width];
 
		for (int x = 0; x < sprite[tgt].width; x++) {
 
			*dst = src_ln[x / scaled_1];
 
@@ -256,13 +256,13 @@ static void ResizeSpriteOut(SpriteLoader
 
	sprite[zoom].width  = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].width,  zoom);
 
	sprite[zoom].height = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].height, zoom);
 
	sprite[zoom].x_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].x_offs, zoom);
 
	sprite[zoom].y_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].y_offs, zoom);
 
	sprite[zoom].colours = sprite[ZOOM_LVL_NORMAL].colours;
 

	
 
	sprite[zoom].AllocateData(zoom, sprite[zoom].height * sprite[zoom].width);
 
	sprite[zoom].AllocateData(zoom, static_cast<size_t>(sprite[zoom].height) * sprite[zoom].width);
 

	
 
	SpriteLoader::CommonPixel *dst = sprite[zoom].data;
 
	const SpriteLoader::CommonPixel *src = sprite[zoom - 1].data;
 
	[[maybe_unused]] const SpriteLoader::CommonPixel *src_end = src + sprite[zoom - 1].height * sprite[zoom - 1].width;
 

	
 
	for (uint y = 0; y < sprite[zoom].height; y++) {
 
@@ -287,15 +287,16 @@ static bool PadSingleSprite(SpriteLoader
 
	uint width  = sprite->width + pad_left + pad_right;
 
	uint height = sprite->height + pad_top + pad_bottom;
 

	
 
	if (width > UINT16_MAX || height > UINT16_MAX) return false;
 

	
 
	/* Copy source data and reallocate sprite memory. */
 
	SpriteLoader::CommonPixel *src_data = MallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height);
 
	MemCpyT(src_data, sprite->data, sprite->width * sprite->height);
 
	sprite->AllocateData(zoom, width * height);
 
	size_t sprite_size = static_cast<size_t>(sprite->width) * sprite->height;
 
	SpriteLoader::CommonPixel *src_data = MallocT<SpriteLoader::CommonPixel>(sprite_size);
 
	MemCpyT(src_data, sprite->data, sprite_size);
 
	sprite->AllocateData(zoom, static_cast<size_t>(width) * height);
 

	
 
	/* Copy with padding to destination. */
 
	SpriteLoader::CommonPixel *src = src_data;
 
	SpriteLoader::CommonPixel *data = sprite->data;
 
	for (uint y = 0; y < height; y++) {
 
		if (y < pad_top || pad_bottom + y >= height) {
src/spriteloader/grf.cpp
Show inline comments
 
@@ -89,13 +89,13 @@ bool DecodeSingleSprite(SpriteLoader::Sp
 
			}
 
		}
 
	}
 

	
 
	if (num != 0) return WarnCorruptSprite(file, file_pos, __LINE__);
 

	
 
	sprite->AllocateData(zoom_lvl, sprite->width * sprite->height);
 
	sprite->AllocateData(zoom_lvl, static_cast<size_t>(sprite->width) * sprite->height);
 

	
 
	/* Convert colour depth to pixel size. */
 
	int bpp = 0;
 
	if (colour_fmt & SCC_RGB)   bpp += 3; // Has RGB data.
 
	if (colour_fmt & SCC_ALPHA) bpp++;    // Has alpha data.
 
	if (colour_fmt & SCC_PAL)   bpp++;    // Has palette data.
 
@@ -165,19 +165,20 @@ bool DecodeSingleSprite(SpriteLoader::Sp
 
					}
 
					data++;
 
				}
 
			} while (!last_item);
 
		}
 
	} else {
 
		if (dest_size < sprite->width * sprite->height * bpp) {
 
		int64 sprite_size = static_cast<int64>(sprite->width) * sprite->height * bpp;
 
		if (dest_size < sprite_size) {
 
			return WarnCorruptSprite(file, file_pos, __LINE__);
 
		}
 

	
 
		if (dest_size > sprite->width * sprite->height * bpp) {
 
		if (dest_size > sprite_size) {
 
			static byte warning_level = 0;
 
			Debug(sprite, warning_level, "Ignoring {} unused extra bytes from the sprite from {} at position {}", dest_size - sprite->width * sprite->height * bpp, file.GetSimplifiedFilename(), file_pos);
 
			Debug(sprite, warning_level, "Ignoring {} unused extra bytes from the sprite from {} at position {}", dest_size - sprite_size, file.GetSimplifiedFilename(), file_pos);
 
			warning_level = 6;
 
		}
 

	
 
		dest = dest_orig.get();
 

	
 
		for (int i = 0; i < sprite->width * sprite->height; i++) {
src/station_cmd.cpp
Show inline comments
 
@@ -1123,13 +1123,13 @@ void GetStationLayout(byte *layout, uint
 
{
 
	if (statspec != nullptr && statspec->layouts.size() >= plat_len &&
 
			statspec->layouts[plat_len - 1].size() >= numtracks &&
 
			!statspec->layouts[plat_len - 1][numtracks - 1].empty()) {
 
		/* Custom layout defined, follow it. */
 
		memcpy(layout, statspec->layouts[plat_len - 1][numtracks - 1].data(),
 
			plat_len * numtracks);
 
			static_cast<size_t>(plat_len) * numtracks);
 
		return;
 
	}
 

	
 
	if (plat_len == 1) {
 
		CreateSingle(layout, numtracks);
 
	} else {
 
@@ -1861,13 +1861,13 @@ CommandCost CmdBuildRoadStop(DoCommandFl
 

	
 
	Station *st = nullptr;
 
	ret = FindJoiningRoadStop(est, station_to_join, adjacent, roadstop_area, &st);
 
	if (ret.Failed()) return ret;
 

	
 
	/* Check if this number of road stops can be allocated. */
 
	if (!RoadStop::CanAllocateItem(roadstop_area.w * roadstop_area.h)) return_cmd_error(is_truck_stop ? STR_ERROR_TOO_MANY_TRUCK_STOPS : STR_ERROR_TOO_MANY_BUS_STOPS);
 
	if (!RoadStop::CanAllocateItem(static_cast<size_t>(roadstop_area.w) * roadstop_area.h)) return_cmd_error(is_truck_stop ? STR_ERROR_TOO_MANY_TRUCK_STOPS : STR_ERROR_TOO_MANY_BUS_STOPS);
 

	
 
	ret = BuildStationPart(&st, flags, reuse, roadstop_area, STATIONNAMING_ROAD);
 
	if (ret.Failed()) return ret;
 

	
 
	if (flags & DC_EXEC) {
 
		/* Check every tile in the area. */
src/tgp.cpp
Show inline comments
 
@@ -323,13 +323,13 @@ static inline bool AllocHeightMap()
 
	assert(_height_map.h.empty());
 

	
 
	_height_map.size_x = MapSizeX();
 
	_height_map.size_y = MapSizeY();
 

	
 
	/* Allocate memory block for height map row pointers */
 
	size_t total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
 
	size_t total_size = static_cast<size_t>(_height_map.size_x + 1) * (_height_map.size_y + 1);
 
	_height_map.dim_x = _height_map.size_x + 1;
 
	_height_map.h.resize(total_size);
 

	
 
	return true;
 
}
 

	
 
@@ -577,13 +577,13 @@ static void HeightMapCurves(uint level)
 
	MemSetT(ht, 0, lengthof(ht));
 

	
 
	/* Set up a grid to choose curve maps based on location; attempt to get a somewhat square grid */
 
	float factor = sqrt((float)_height_map.size_x / (float)_height_map.size_y);
 
	uint sx = Clamp((int)(((1 << level) * factor) + 0.5), 1, 128);
 
	uint sy = Clamp((int)(((1 << level) / factor) + 0.5), 1, 128);
 
	byte *c = AllocaM(byte, sx * sy);
 
	byte *c = AllocaM(byte, static_cast<size_t>(sx) * sy);
 

	
 
	for (uint i = 0; i < sx * sy; i++) {
 
		c[i] = Random() % lengthof(curve_maps);
 
	}
 

	
 
	/* Apply curves */
src/video/allegro_v.cpp
Show inline comments
 
@@ -201,13 +201,13 @@ static bool CreateMainSurface(uint w, ui
 
	_screen.width = _allegro_screen->w;
 
	_screen.height = _allegro_screen->h;
 
	_screen.pitch = ((byte*)screen->line[1] - (byte*)screen->line[0]) / (bpp / 8);
 
	_screen.dst_ptr = _allegro_screen->line[0];
 

	
 
	/* Initialise the screen so we don't blit garbage to the screen */
 
	memset(_screen.dst_ptr, 0, _screen.height * _screen.pitch);
 
	memset(_screen.dst_ptr, 0, static_cast<size_t>(_screen.height) * _screen.pitch);
 

	
 
	/* Set the mouse at the place where we expect it */
 
	poll_mouse();
 
	_cursor.pos.x = mouse_x;
 
	_cursor.pos.y = mouse_y;
 

	
src/video/dedicated_v.cpp
Show inline comments
 
@@ -136,13 +136,13 @@ static FVideoDriver_Dedicated iFVideoDri
 

	
 
const char *VideoDriver_Dedicated::Start(const StringList &parm)
 
{
 
	this->UpdateAutoResolution();
 

	
 
	int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
 
	_dedicated_video_mem = (bpp == 0) ? nullptr : MallocT<byte>(_cur_resolution.width * _cur_resolution.height * (bpp / 8));
 
	_dedicated_video_mem = (bpp == 0) ? nullptr : MallocT<byte>(static_cast<size_t>(_cur_resolution.width) * _cur_resolution.height * (bpp / 8));
 

	
 
	_screen.width  = _screen.pitch = _cur_resolution.width;
 
	_screen.height = _cur_resolution.height;
 
	_screen.dst_ptr = _dedicated_video_mem;
 
	ScreenSizeChanged();
 
	BlitterFactory::GetCurrentBlitter()->PostResize();
src/video/opengl.cpp
Show inline comments
 
@@ -915,43 +915,44 @@ static void ClearPixelBuffer(size_t len,
 
bool OpenGLBackend::Resize(int w, int h, bool force)
 
{
 
	if (!force && _screen.width == w && _screen.height == h) return false;
 

	
 
	int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
 
	int pitch = Align(w, 4);
 
	size_t line_pixel_count = static_cast<size_t>(pitch) * h;
 

	
 
	_glViewport(0, 0, w, h);
 

	
 
	_glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
 

	
 
	this->vid_buffer = nullptr;
 
	if (this->persistent_mapping_supported) {
 
		_glDeleteBuffers(1, &this->vid_pbo);
 
		_glGenBuffers(1, &this->vid_pbo);
 
		_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
 
		_glBufferStorage(GL_PIXEL_UNPACK_BUFFER, pitch * h * bpp / 8, nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_CLIENT_STORAGE_BIT);
 
		_glBufferStorage(GL_PIXEL_UNPACK_BUFFER, line_pixel_count * bpp / 8, nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_CLIENT_STORAGE_BIT);
 
	} else {
 
		/* Re-allocate video buffer texture and backing store. */
 
		_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
 
		_glBufferData(GL_PIXEL_UNPACK_BUFFER, pitch * h * bpp / 8, nullptr, GL_DYNAMIC_DRAW);
 
		_glBufferData(GL_PIXEL_UNPACK_BUFFER, line_pixel_count * bpp / 8, nullptr, GL_DYNAMIC_DRAW);
 
	}
 

	
 
	if (bpp == 32) {
 
		/* Initialize backing store alpha to opaque for 32bpp modes. */
 
		Colour black(0, 0, 0);
 
		if (_glClearBufferSubData != nullptr) {
 
			_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_RGBA8, 0, pitch * h * bpp / 8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &black.data);
 
			_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_RGBA8, 0, line_pixel_count * bpp / 8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &black.data);
 
		} else {
 
			ClearPixelBuffer<uint32>(pitch * h, black.data);
 
			ClearPixelBuffer<uint32>(line_pixel_count, black.data);
 
		}
 
	} else if (bpp == 8) {
 
		if (_glClearBufferSubData != nullptr) {
 
			byte b = 0;
 
			_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, pitch * h, GL_RED, GL_UNSIGNED_BYTE, &b);
 
			_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, line_pixel_count, GL_RED, GL_UNSIGNED_BYTE, &b);
 
		} else {
 
			ClearPixelBuffer<byte>(pitch * h, 0);
 
			ClearPixelBuffer<byte>(line_pixel_count, 0);
 
		}
 
	}
 

	
 
	_glActiveTexture(GL_TEXTURE0);
 
	_glBindTexture(GL_TEXTURE_2D, this->vid_texture);
 
	switch (bpp) {
 
@@ -969,24 +970,24 @@ bool OpenGLBackend::Resize(int w, int h,
 
	if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
 
		this->anim_buffer = nullptr;
 
		if (this->persistent_mapping_supported) {
 
			_glDeleteBuffers(1, &this->anim_pbo);
 
			_glGenBuffers(1, &this->anim_pbo);
 
			_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
 
			_glBufferStorage(GL_PIXEL_UNPACK_BUFFER, pitch * h, nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_CLIENT_STORAGE_BIT);
 
			_glBufferStorage(GL_PIXEL_UNPACK_BUFFER, line_pixel_count, nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_CLIENT_STORAGE_BIT);
 
		} else {
 
			_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
 
			_glBufferData(GL_PIXEL_UNPACK_BUFFER, pitch * h, nullptr, GL_DYNAMIC_DRAW);
 
			_glBufferData(GL_PIXEL_UNPACK_BUFFER, line_pixel_count, nullptr, GL_DYNAMIC_DRAW);
 
		}
 

	
 
		/* Initialize buffer as 0 == no remap. */
 
		if (_glClearBufferSubData != nullptr) {
 
			byte b = 0;
 
			_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, pitch * h, GL_RED, GL_UNSIGNED_BYTE, &b);
 
			_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, line_pixel_count, GL_RED, GL_UNSIGNED_BYTE, &b);
 
		} else {
 
			ClearPixelBuffer<byte>(pitch * h, 0);
 
			ClearPixelBuffer<byte>(line_pixel_count, 0);
 
		}
 

	
 
		_glBindTexture(GL_TEXTURE_2D, this->anim_texture);
 
		_glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
 
		_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
 
	} else {
 
@@ -1188,13 +1189,13 @@ uint8 *OpenGLBackend::GetAnimBuffer()
 

	
 
	if (!this->persistent_mapping_supported) {
 
		_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
 
		this->anim_buffer = _glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
 
	} else if (this->anim_buffer == nullptr) {
 
		_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
 
		this->anim_buffer = _glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, _screen.pitch * _screen.height, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
 
		this->anim_buffer = _glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, static_cast<GLsizeiptr>(_screen.pitch) * _screen.height, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
 
	}
 

	
 
	return (uint8 *)this->anim_buffer;
 
}
 

	
 
/**
 
@@ -1488,27 +1489,28 @@ void OpenGLSprite::Update(uint width, ui
 
	_glActiveTexture(GL_TEXTURE0);
 
	_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
 
	_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
 

	
 
	if (this->tex[TEX_RGBA] != 0) {
 
		/* Unpack pixel data */
 
		Colour *rgba = buf_rgba.Allocate(width * height);
 
		for (size_t i = 0; i < width * height; i++) {
 
		size_t size = static_cast<size_t>(width) * height;
 
		Colour *rgba = buf_rgba.Allocate(size);
 
		for (size_t i = 0; i < size; i++) {
 
			rgba[i].r = data[i].r;
 
			rgba[i].g = data[i].g;
 
			rgba[i].b = data[i].b;
 
			rgba[i].a = data[i].a;
 
		}
 

	
 
		_glBindTexture(GL_TEXTURE_2D, this->tex[TEX_RGBA]);
 
		_glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, rgba);
 
	}
 

	
 
	if (this->tex[TEX_REMAP] != 0) {
 
		/* Unpack and align pixel data. */
 
		int pitch = Align(width, 4);
 
		size_t pitch = Align(width, 4);
 

	
 
		uint8 *pal = buf_pal.Allocate(pitch * height);
 
		const SpriteLoader::CommonPixel *row = data;
 
		for (uint y = 0; y < height; y++, pal += pitch, row += width) {
 
			for (uint x = 0; x < width; x++) {
 
				pal[x] = row[x].m;
0 comments (0 inline, 0 general)