2
0
mirror of https://github.com/FWGS/xash3d-fwgs synced 2024-11-22 01:45:19 +01:00

filesystem: take FS_Read improvements from DarkPlaces and proper fix for buffer overflow

This commit is contained in:
Alibek Omarov 2024-05-15 03:43:02 +03:00
parent 052e0445ab
commit 24f4d410ce

View File

@ -2125,7 +2125,7 @@ fs_offset_t FS_Read( file_t *file, void *buffer, size_t buffersize )
{ {
fs_offset_t done; fs_offset_t done;
fs_offset_t nb; fs_offset_t nb;
size_t count; fs_offset_t count;
// nothing to copy // nothing to copy
if( buffersize == 0 ) return 1; if( buffersize == 0 ) return 1;
@ -2137,10 +2137,6 @@ fs_offset_t FS_Read( file_t *file, void *buffer, size_t buffersize )
buffersize--; buffersize--;
file->ungetc = EOF; file->ungetc = EOF;
done = 1; done = 1;
// we had one byte in the buffer, it was ungetc'ed, so exit
if( buffersize == 0 )
return 1;
} }
else done = 0; else done = 0;
@ -2148,12 +2144,13 @@ fs_offset_t FS_Read( file_t *file, void *buffer, size_t buffersize )
if( file->buff_ind < file->buff_len ) if( file->buff_ind < file->buff_len )
{ {
count = file->buff_len - file->buff_ind; count = file->buff_len - file->buff_ind;
count = ( buffersize > count ) ? count : (fs_offset_t)buffersize;
done += ( buffersize > count ) ? (fs_offset_t)count : (fs_offset_t)buffersize; done += count;
memcpy( buffer, &file->buff[file->buff_ind], done ); memcpy( buffer, &file->buff[file->buff_ind], count );
file->buff_ind += done; file->buff_ind += count;
buffersize -= done; buffersize -= count;
if( buffersize == 0 ) if( buffersize == 0 )
return done; return done;
} }
@ -2167,10 +2164,10 @@ fs_offset_t FS_Read( file_t *file, void *buffer, size_t buffersize )
// if we have a lot of data to get, put them directly into "buffer" // if we have a lot of data to get, put them directly into "buffer"
if( buffersize > sizeof( file->buff ) / 2 ) if( buffersize > sizeof( file->buff ) / 2 )
{ {
if( count > buffersize ) if( count > (fs_offset_t)buffersize )
count = buffersize; count = (fs_offset_t)buffersize;
lseek( file->handle, file->offset + file->position, SEEK_SET ); lseek( file->handle, file->offset + file->position, SEEK_SET );
nb = read( file->handle, (byte *)buffer + done, count ); nb = read( file->handle, &((byte *)buffer)[done], count );
if( nb > 0 ) if( nb > 0 )
{ {
@ -2182,8 +2179,8 @@ fs_offset_t FS_Read( file_t *file, void *buffer, size_t buffersize )
} }
else else
{ {
if( count > sizeof( file->buff )) if( count > (fs_offset_t)sizeof( file->buff ))
count = sizeof( file->buff ); count = (fs_offset_t)sizeof( file->buff );
lseek( file->handle, file->offset + file->position, SEEK_SET ); lseek( file->handle, file->offset + file->position, SEEK_SET );
nb = read( file->handle, file->buff, count ); nb = read( file->handle, file->buff, count );