From 21ec8c38ae67abde1b465913db9d89e0b86cbe6a Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 9 Nov 2021 11:49:03 +0100 Subject: [PATCH] Review variables initialization - All variables are initialized on declaration, some arrays were not properly initialized - Static array buffers require memset() for re-initialization on every function call --- src/raudio.c | 6 +++--- src/rcore.c | 4 ++-- src/rlgl.h | 4 ++-- src/rtext.c | 37 ++++++++++++++++++++----------------- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/raudio.c b/src/raudio.c index 223310f32..bf2af3f71 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -2013,7 +2013,7 @@ static ma_uint32 ReadAudioBufferFramesInInternalFormat(AudioBuffer *audioBuffer, // Another thread can update the processed state of buffers so // we just take a copy here to try and avoid potential synchronization problems - bool isSubBufferProcessed[2]; + bool isSubBufferProcessed[2] = { 0 }; isSubBufferProcessed[0] = audioBuffer->isSubBufferProcessed[0]; isSubBufferProcessed[1] = audioBuffer->isSubBufferProcessed[1]; @@ -2096,7 +2096,7 @@ static ma_uint32 ReadAudioBufferFramesInMixingFormat(AudioBuffer *audioBuffer, f // should be defined by the output format of the data converter. We do this until frameCount frames have been output. The important // detail to remember here is that we never, ever attempt to read more input data than is required for the specified number of output // frames. This can be achieved with ma_data_converter_get_required_input_frame_count(). - ma_uint8 inputBuffer[4096]; + ma_uint8 inputBuffer[4096] = { 0 }; ma_uint32 inputBufferFrameCap = sizeof(inputBuffer)/ma_get_bytes_per_frame(audioBuffer->converter.config.formatIn, audioBuffer->converter.config.channelsIn); ma_uint32 totalOutputFramesProcessed = 0; @@ -2165,7 +2165,7 @@ static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const while (framesToRead > 0) { - float tempBuffer[1024]; // 512 frames for stereo + float tempBuffer[1024] = { 0 }; // Frames for stereo ma_uint32 framesToReadRightNow = framesToRead; if (framesToReadRightNow > sizeof(tempBuffer)/sizeof(tempBuffer[0])/AUDIO_DEVICE_CHANNELS) diff --git a/src/rcore.c b/src/rcore.c index 3b4515520..d24b38f99 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -5633,8 +5633,8 @@ static void ProcessKeyboard(void) #define MAX_KEYBUFFER_SIZE 32 // Max size in bytes to read // Keyboard input polling (fill keys[256] array with status) - int bufferByteCount = 0; // Bytes available on the buffer - char keysBuffer[MAX_KEYBUFFER_SIZE]; // Max keys to be read at a time + int bufferByteCount = 0; // Bytes available on the buffer + char keysBuffer[MAX_KEYBUFFER_SIZE] = { 0 }; // Max keys to be read at a time // Read availables keycodes from stdin bufferByteCount = read(STDIN_FILENO, keysBuffer, MAX_KEYBUFFER_SIZE); // POSIX system call diff --git a/src/rlgl.h b/src/rlgl.h index 4f9d8fa05..1d1516745 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -4547,8 +4547,8 @@ static unsigned char *rlGenNextMipmapData(unsigned char *srcData, int srcWidth, { int x2 = 0; int y2 = 0; - unsigned char prow[4]; - unsigned char pcol[4]; + unsigned char prow[4] = { 0 }; + unsigned char pcol[4] = { 0 }; int width = srcWidth/2; int height = srcHeight/2; diff --git a/src/rtext.c b/src/rtext.c index b49a6fef0..137ad1864 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1142,12 +1142,11 @@ bool TextIsEqual(const char *text1, const char *text2) { bool result = false; - if (text1 == NULL || text2 == NULL) { - return false; + if ((text1 != NULL) && (text2 != NULL)) + { + if (strcmp(text1, text2) == 0) result = true; } - if (strcmp(text1, text2) == 0) result = true; - return result; } @@ -1155,6 +1154,7 @@ bool TextIsEqual(const char *text1, const char *text2) const char *TextSubtext(const char *text, int position, int length) { static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 }; + memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH); int textLength = TextLength(text); @@ -1178,21 +1178,21 @@ const char *TextSubtext(const char *text, int position, int length) } // Replace text string -// REQUIRES: strstr(), strncpy(), strcpy() +// REQUIRES: strlen(), strstr(), strncpy(), strcpy() // WARNING: Returned buffer must be freed by the user (if return != NULL) char *TextReplace(char *text, const char *replace, const char *by) { // Sanity checks and initialization if (!text || !replace || !by) return NULL; - char *result; + char *result = NULL; - char *insertPoint; // Next insert point - char *temp; // Temp pointer - int replaceLen; // Replace string length of (the string to remove) - int byLen; // Replacement length (the string to replace replace by) - int lastReplacePos; // Distance between replace and end of last replace - int count; // Number of replacements + char *insertPoint = NULL; // Next insert point + char *temp = NULL; // Temp pointer + int replaceLen = 0; // Replace string length of (the string to remove) + int byLen = 0; // Replacement length (the string to replace replace by) + int lastReplacePos = 0; // Distance between replace and end of last replace + int count = 0; // Number of replacements replaceLen = TextLength(replace); if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count @@ -1249,9 +1249,9 @@ char *TextInsert(const char *text, const char *insert, int position) // REQUIRES: memset(), memcpy() const char *TextJoin(const char **textList, int count, const char *delimiter) { - static char text[MAX_TEXT_BUFFER_LENGTH] = { 0 }; - memset(text, 0, MAX_TEXT_BUFFER_LENGTH); - char *textPtr = text; + static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 }; + memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH); + char *textPtr = buffer; int totalLength = 0; int delimiterLen = TextLength(delimiter); @@ -1276,7 +1276,7 @@ const char *TextJoin(const char **textList, int count, const char *delimiter) } } - return text; + return buffer; } // Split string into multiple strings @@ -1346,6 +1346,7 @@ int TextFindIndex(const char *text, const char *find) const char *TextToUpper(const char *text) { static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 }; + memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH); for (int i = 0; i < MAX_TEXT_BUFFER_LENGTH; i++) { @@ -1368,6 +1369,7 @@ const char *TextToUpper(const char *text) const char *TextToLower(const char *text) { static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 }; + memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH); for (int i = 0; i < MAX_TEXT_BUFFER_LENGTH; i++) { @@ -1387,6 +1389,7 @@ const char *TextToLower(const char *text) const char *TextToPascal(const char *text) { static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 }; + memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH); buffer[0] = (char)toupper(text[0]); @@ -1666,7 +1669,7 @@ static Font LoadBMFont(const char *fileName) int imWidth = 0; int imHeight = 0; - char imFileName[129]; + char imFileName[129] = { 0 }; int base = 0; // Useless data