From 75b0264f35d8895c1e64c7c7bc85d1572d167cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Coelho?= Date: Tue, 29 Oct 2019 14:57:19 +0000 Subject: [PATCH] fix various problems, thanks CppCheck :) (#1005) * explained a bit more the core_window_letterbox example * fixed a few 'ups' moments that could lead to mild head pain and time loss --- src/models.c | 4 ++++ src/physac.h | 27 +++++++++++++++++++++------ src/raudio.c | 3 ++- src/rlgl.h | 7 +++++++ src/shapes.c | 2 +- src/text.c | 2 +- src/textures.c | 2 +- 7 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/models.c b/src/models.c index 501f1e706..f70dd4092 100644 --- a/src/models.c +++ b/src/models.c @@ -929,12 +929,16 @@ ModelAnimation *LoadModelAnimations(const char *filename, int *animCount) { TraceLog(LOG_ERROR, "Magic Number \"%s\"does not match.", iqm.magic); fclose(iqmFile); + + return NULL; } if (iqm.version != IQM_VERSION) { TraceLog(LOG_ERROR, "IQM version %i is incorrect.", iqm.version); fclose(iqmFile); + + return NULL; } // Get bones data diff --git a/src/physac.h b/src/physac.h index 4119feaed..b23fb1afe 100644 --- a/src/physac.h +++ b/src/physac.h @@ -842,9 +842,17 @@ PHYSACDEF void DestroyPhysicsBody(PhysicsBody body) } } - #if defined(PHYSAC_DEBUG) - if (index == -1) printf("[PHYSAC] cannot find body id %i in pointers array\n", id); - #endif + + if (index == -1){ + + #if defined(PHYSAC_DEBUG) + printf("[PHYSAC] cannot find body id %i in pointers array\n", id); + #endif + + // prevent access to index -1 + return; + } + // Free body allocated memory PHYSAC_FREE(body); @@ -1249,9 +1257,16 @@ static void DestroyPhysicsManifold(PhysicsManifold manifold) } } - #if defined(PHYSAC_DEBUG) - if (index == -1) printf("[PHYSAC] cannot find manifold id %i in pointers array\n", id); - #endif + + if (index == -1) { + #if defined(PHYSAC_DEBUG) + printf("[PHYSAC] cannot find manifold id %i in pointers array\n", id); + #endif + + //prevent access to index -1 + return; + } + // Free manifold allocated memory PHYSAC_FREE(manifold); diff --git a/src/raudio.c b/src/raudio.c index 188c05329..f6a1e5f65 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -592,13 +592,14 @@ void SetMasterVolume(float volume) AudioBuffer *InitAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 bufferSizeInFrames, int usage) { AudioBuffer *audioBuffer = (AudioBuffer *)RL_CALLOC(1, sizeof(AudioBuffer)); - audioBuffer->buffer = RL_CALLOC(bufferSizeInFrames*channels*ma_get_bytes_per_sample(format), 1); if (audioBuffer == NULL) { TraceLog(LOG_ERROR, "InitAudioBuffer() : Failed to allocate memory for audio buffer"); return NULL; } + + audioBuffer->buffer = RL_CALLOC(bufferSizeInFrames*channels*ma_get_bytes_per_sample(format), 1); // Audio data runs through a format converter ma_pcm_converter_config dspConfig; diff --git a/src/rlgl.h b/src/rlgl.h index b83da6e8c..6eb215c08 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -1887,6 +1887,13 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi #endif #endif // GRAPHICS_API_OPENGL_11 + if( data == NULL ){ + //ups! + TraceLog(LOG_WARNING, "Got asked to load texture from a NULL pointer!"); + + return id; + } + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, &id); // Generate texture id diff --git a/src/shapes.c b/src/shapes.c index 4fd4eff57..fc0fb00c3 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -1524,7 +1524,7 @@ static float EaseCubicInOut(float t, float b, float c, float d) // Get texture to draw shapes (RAII) static Texture2D GetShapesTexture(void) { - if (texShapes.id <= 0) + if (texShapes.id == 0) //this variable is an unsigned int, will never be negative { #if defined(SUPPORT_FONT_TEXTURE) texShapes = GetFontDefault().texture; // Use font texture white character diff --git a/src/text.c b/src/text.c index 6d55fa8dc..3f8e22b10 100644 --- a/src/text.c +++ b/src/text.c @@ -1394,7 +1394,7 @@ char *TextToUtf8(int *codepoints, int length) } // Resize memory to text length + string NULL terminator - realloc(text, size + 1); + text = realloc(text, size + 1); return text; } diff --git a/src/textures.c b/src/textures.c index ec08e3ac8..967b6108a 100644 --- a/src/textures.c +++ b/src/textures.c @@ -662,7 +662,7 @@ Vector4 *GetImageDataNormalized(Image image) pixels[i].w = 1.0f; k += 3; - } + } break; case UNCOMPRESSED_R32G32B32A32: { pixels[i].x = ((float *)image.data)[k];