mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-20 06:58:20 +00:00
Fix undefined behavior on loading empty PNG files
When empty PNG files are loaded, the `std::vector` for the file contents is resized to size 0, which results in undefined behavior when it is accessed with `front`. When `io_tell` fails, i.e. returns `-1`, this was incorrectly cast to an `unsigned` and therefore caused a very large allocation and potentially crashes due to lack of memory.
This commit is contained in:
parent
7d0e9e86c1
commit
a8f3b56850
|
@ -584,7 +584,13 @@ bool CGraphics_Threaded::LoadPNG(CImageInfo *pImg, const char *pFilename, int St
|
|||
if(File)
|
||||
{
|
||||
io_seek(File, 0, IOSEEK_END);
|
||||
unsigned int FileSize = io_tell(File);
|
||||
long int FileSize = io_tell(File);
|
||||
if(FileSize <= 0)
|
||||
{
|
||||
io_close(File);
|
||||
log_error("game/png", "failed to get file size (%ld). filename='%s'", FileSize, pFilename);
|
||||
return false;
|
||||
}
|
||||
io_seek(File, 0, IOSEEK_START);
|
||||
|
||||
TImageByteBuffer ByteBuffer;
|
||||
|
|
|
@ -12,7 +12,13 @@ int DilateFile(const char *pFilename)
|
|||
if(File)
|
||||
{
|
||||
io_seek(File, 0, IOSEEK_END);
|
||||
unsigned int FileSize = io_tell(File);
|
||||
long int FileSize = io_tell(File);
|
||||
if(FileSize <= 0)
|
||||
{
|
||||
io_close(File);
|
||||
dbg_msg("dilate", "failed to get file size (%ld). filename='%s'", FileSize, pFilename);
|
||||
return false;
|
||||
}
|
||||
io_seek(File, 0, IOSEEK_START);
|
||||
TImageByteBuffer ByteBuffer;
|
||||
SImageByteBuffer ImageByteBuffer(&ByteBuffer);
|
||||
|
|
|
@ -31,7 +31,13 @@ int LoadPNG(CImageInfo *pImg, const char *pFilename)
|
|||
if(File)
|
||||
{
|
||||
io_seek(File, 0, IOSEEK_END);
|
||||
unsigned int FileSize = io_tell(File);
|
||||
long int FileSize = io_tell(File);
|
||||
if(FileSize <= 0)
|
||||
{
|
||||
io_close(File);
|
||||
dbg_msg("map_convert_07", "failed to get file size (%ld). filename='%s'", FileSize, pFilename);
|
||||
return false;
|
||||
}
|
||||
io_seek(File, 0, IOSEEK_START);
|
||||
TImageByteBuffer ByteBuffer;
|
||||
SImageByteBuffer ImageByteBuffer(&ByteBuffer);
|
||||
|
|
|
@ -319,7 +319,13 @@ bool LoadPNG(CImageInfo *pImg, const char *pFilename)
|
|||
}
|
||||
|
||||
io_seek(File, 0, IOSEEK_END);
|
||||
unsigned int FileSize = io_tell(File);
|
||||
long int FileSize = io_tell(File);
|
||||
if(FileSize <= 0)
|
||||
{
|
||||
io_close(File);
|
||||
dbg_msg("map_create_pixelart", "ERROR: Failed to get file size (%ld). filename='%s'", FileSize, pFilename);
|
||||
return false;
|
||||
}
|
||||
io_seek(File, 0, IOSEEK_START);
|
||||
TImageByteBuffer ByteBuffer;
|
||||
SImageByteBuffer ImageByteBuffer(&ByteBuffer);
|
||||
|
|
|
@ -29,7 +29,12 @@ bool LoadPNG(CImageInfo *pImg, const char *pFilename)
|
|||
if(File)
|
||||
{
|
||||
io_seek(File, 0, IOSEEK_END);
|
||||
unsigned int FileSize = io_tell(File);
|
||||
long int FileSize = io_tell(File);
|
||||
if(FileSize <= 0)
|
||||
{
|
||||
io_close(File);
|
||||
return false;
|
||||
}
|
||||
io_seek(File, 0, IOSEEK_START);
|
||||
TImageByteBuffer ByteBuffer;
|
||||
SImageByteBuffer ImageByteBuffer(&ByteBuffer);
|
||||
|
|
Loading…
Reference in a new issue