From d17a8f36c74654d7dba372a096f04b3d6804b344 Mon Sep 17 00:00:00 2001 From: Julie Marchant Date: Thu, 23 May 2019 11:25:54 -0400 Subject: [PATCH] Better malloc practice, plus several missing warnings --- src/alien.c | 4 ++-- src/bullet.c | 5 ++++- src/collectable.c | 3 ++- src/engine.c | 8 ++++---- src/explosion.c | 4 +++- src/game.c | 7 ++++++- src/gfx.c | 2 +- src/screen.c | 9 ++++++++- src/title.c | 7 ++++++- 9 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/alien.c b/src/alien.c index 8e1f360..9b85fd9 100644 --- a/src/alien.c +++ b/src/alien.c @@ -1187,10 +1187,10 @@ int alien_add() int *alienArray; int numberOfAliens = 1; - alienArray = malloc(8 * sizeof(int)); + alienArray = malloc(8 * sizeof(*alienArray)); if (alienArray == NULL) { - engine_warn("WARNING: Failed to allocate memory for aliens"); + engine_warn("Failed to allocate memory for aliens"); return 0; } diff --git a/src/bullet.c b/src/bullet.c index dbc939e..3fc0da2 100644 --- a/src/bullet.c +++ b/src/bullet.c @@ -34,9 +34,12 @@ void bullet_add(Object *theWeapon, Object *attacker, int y, int dy) int imageIndex; int tempX, tempY, steps; - bullet = malloc(sizeof(Object)); + bullet = malloc(sizeof(*bullet)); if (bullet == NULL) + { + engine_warn("Failed to allocate memor for bullet"); return; + } if (attacker == &player) game.shots++; diff --git a/src/collectable.c b/src/collectable.c index bca1c91..68eaf55 100644 --- a/src/collectable.c +++ b/src/collectable.c @@ -37,6 +37,7 @@ Create a new collectable item based on supplied arguments. void collectable_add(float x, float y, int type, int value, int life) { int r; + Collectable *collectable; if (type == P_ANYTHING) { @@ -143,7 +144,7 @@ void collectable_add(float x, float y, int type, int value, int life) } } - Collectable *collectable = malloc(sizeof(Collectable)); + collectable = malloc(sizeof(*collectable)); if (collectable == NULL) { engine_warn("Failed to allocate memory for collectable"); diff --git a/src/engine.c b/src/engine.c index c1eefd7..0103b77 100644 --- a/src/engine.c +++ b/src/engine.c @@ -60,7 +60,7 @@ void engine_init() engine.smx = 0; engine.smy = 0; - engine.bulletHead = malloc(sizeof(Object)); + engine.bulletHead = malloc(sizeof(*engine.bulletHead)); if (engine.bulletHead == NULL) { engine_error("Failed to allocate memory for bullet head."); @@ -68,7 +68,7 @@ void engine_init() engine.bulletHead->next = NULL; engine.bulletTail = engine.bulletHead; - engine.explosionHead = malloc(sizeof(Object)); + engine.explosionHead = malloc(sizeof(*engine.explosionHead)); if (engine.explosionHead == NULL) { engine_error("Failed to allocate memory for explosion head."); @@ -76,7 +76,7 @@ void engine_init() engine.explosionHead->next = NULL; engine.explosionTail = engine.explosionHead; - engine.collectableHead = malloc(sizeof(Collectable)); + engine.collectableHead = malloc(sizeof(*engine.collectableHead)); if (engine.collectableHead == NULL) { engine_error("Failed to allocate memory for collectable head."); @@ -84,7 +84,7 @@ void engine_init() engine.collectableHead->next = NULL; engine.collectableTail = engine.collectableHead; - engine.debrisHead = malloc(sizeof(Object)); + engine.debrisHead = malloc(sizeof(*engine.debrisHead)); if (engine.debrisHead == NULL) { engine_error("Failed to allocate memory for debris head."); diff --git a/src/explosion.c b/src/explosion.c index 1273eaf..8a44e3a 100644 --- a/src/explosion.c +++ b/src/explosion.c @@ -33,7 +33,9 @@ to change frames on a 21, 14, 7 basis. */ void explosion_add(float x, float y, int type) { - Object *explosion = malloc(sizeof(Object)); + Object *explosion; + + explosion = malloc(sizeof(*explosion)); if (explosion == NULL) { engine_warn("Failed to allocate memory for explosion."); diff --git a/src/game.c b/src/game.c index 9061334..363d6d2 100644 --- a/src/game.c +++ b/src/game.c @@ -233,7 +233,12 @@ static void game_addDebris(int x, int y, int amount) for (int i = 0 ; i < amount ; i++) { - debris = malloc(sizeof(Object)); + debris = malloc(sizeof(*debris)); + if (debris == NULL) + { + engine_warn("Failed to allocate memory for debris"); + return; + } debris->next = NULL; debris->x = x; diff --git a/src/gfx.c b/src/gfx.c index 8c85fe8..6aabea8 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -44,7 +44,7 @@ SDL_Surface *gfx_messageBox; void gfx_init() { - screen_bufferHead = malloc(sizeof(LinkedRect)); + screen_bufferHead = malloc(sizeof(*screen_bufferHead)); if (screen_bufferHead == NULL) { engine_error("Failed to allocate memory for buffer head."); diff --git a/src/screen.c b/src/screen.c index 98f6d9e..f09a2bc 100644 --- a/src/screen.c +++ b/src/screen.c @@ -56,7 +56,14 @@ void screen_drawBackground() void screen_addBuffer(int x, int y, int w, int h) { - LinkedRect *rect = malloc(sizeof(LinkedRect)); + LinkedRect *rect; + + rect = malloc(sizeof(*rect)); + if (rect == NULL) + { + engine_warn("Failed to allocate memor for screen buffer"); + return; + } rect->next = NULL; rect->x = x; diff --git a/src/title.c b/src/title.c index 7071285..f97a9f7 100644 --- a/src/title.c +++ b/src/title.c @@ -663,7 +663,12 @@ void title_showCredits() // FIXME: It would be nice for the size of this array to be determined // by the number of lines in the text file. I'm not sure how to do // that at the moment, so just giving it a very large number for now. - credit = malloc(sizeof(TextObject) * 300); + credit = malloc(300 * sizeof(*credit)); + if (credit == NULL) + { + engine_warn("Failed to allocate memory for credits"); + return; + } while (fscanf(fp, "%d %[^\n]%*c", &yPos, text) == 2) {