Report out of memory instead of crashing in SDL_LoadFile_RW()

Fixes https://github.com/libsdl-org/SDL/issues/8935

(cherry picked from commit 52975961326797558d1c66a02cb32870ff7be6d4)
This commit is contained in:
Sam Lantinga 2024-01-27 12:15:43 -08:00
parent 55caed2011
commit aac7d1c2ae

View file

@ -718,12 +718,12 @@ void *SDL_LoadFile_RW(SDL_RWops *src, size_t *datasize, int freesrc)
{
static const Sint64 FILE_CHUNK_SIZE = 1024;
Sint64 size;
size_t size_read, size_total;
size_t size_read, size_total = 0;
void *data = NULL, *newdata;
if (!src) {
SDL_InvalidParamError("src");
return NULL;
goto done;
}
size = SDL_RWsize(src);
@ -731,8 +731,11 @@ void *SDL_LoadFile_RW(SDL_RWops *src, size_t *datasize, int freesrc)
size = FILE_CHUNK_SIZE;
}
data = SDL_malloc((size_t)(size + 1));
if (!data) {
SDL_OutOfMemory();
goto done;
}
size_total = 0;
for (;;) {
if ((((Sint64)size_total) + FILE_CHUNK_SIZE) > size) {
size = (size_total + FILE_CHUNK_SIZE);
@ -753,12 +756,12 @@ void *SDL_LoadFile_RW(SDL_RWops *src, size_t *datasize, int freesrc)
size_total += size_read;
}
if (datasize) {
*datasize = size_total;
}
((char *)data)[size_total] = '\0';
done:
if (datasize) {
*datasize = size_total;
}
if (freesrc && src) {
SDL_RWclose(src);
}