From 43d82c1f21a2432d290e44bf223d2879f5af8837 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 16 Sep 2020 11:33:56 +0200 Subject: [PATCH] Add security checks when loading data from memory --- src/raudio.c | 9 ++++++--- src/text.c | 12 ++++++++---- src/textures.c | 15 +++++++++------ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/raudio.c b/src/raudio.c index f2fd5dcf..3c4c0a6a 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -694,10 +694,13 @@ Wave LoadWave(const char *fileName) unsigned int fileSize = 0; unsigned char *fileData = LoadFileData(fileName, &fileSize); - // Loading wave from memory data - wave = LoadWaveFromMemory(GetFileExtension(fileName), fileData, fileSize); + if (fileData != NULL) + { + // Loading wave from memory data + wave = LoadWaveFromMemory(GetFileExtension(fileName), fileData, fileSize); - RL_FREE(fileData); + RL_FREE(fileData); + } return wave; } diff --git a/src/text.c b/src/text.c index cce8d98a..52f2f46a 100644 --- a/src/text.c +++ b/src/text.c @@ -340,10 +340,14 @@ Font LoadFontEx(const char *fileName, int fontSize, int *fontChars, int charsCou unsigned int fileSize = 0; unsigned char *fileData = LoadFileData(fileName, &fileSize); - // Loading font from memory data - font = LoadFontFromMemory(GetFileExtension(fileName), fileData, fileSize, fontSize, fontChars, charsCount); - - RL_FREE(fileData); + if (fileData != NULL) + { + // Loading font from memory data + font = LoadFontFromMemory(GetFileExtension(fileName), fileData, fileSize, fontSize, fontChars, charsCount); + + RL_FREE(fileData); + } + else font = GetFontDefault(); return font; } diff --git a/src/textures.c b/src/textures.c index e375b263..c1c60d08 100644 --- a/src/textures.c +++ b/src/textures.c @@ -211,13 +211,16 @@ Image LoadImage(const char *fileName) unsigned int fileSize = 0; unsigned char *fileData = LoadFileData(fileName, &fileSize); - // Loading image from memory data - image = LoadImageFromMemory(GetFileExtension(fileName), fileData, fileSize); + if (fileData != NULL) + { + // Loading image from memory data + image = LoadImageFromMemory(GetFileExtension(fileName), fileData, fileSize); - if (image.data != NULL) TRACELOG(LOG_INFO, "IMAGE: [%s] Data loaded successfully (%ix%i)", fileName, image.width, image.height); - else TRACELOG(LOG_WARNING, "IMAGE: [%s] Failed to load data", fileName); - - RL_FREE(fileData); + if (image.data != NULL) TRACELOG(LOG_INFO, "IMAGE: [%s] Data loaded successfully (%ix%i)", fileName, image.width, image.height); + else TRACELOG(LOG_WARNING, "IMAGE: [%s] Failed to load data", fileName); + + RL_FREE(fileData); + } return image; }