Added null checks for malloc

This commit is contained in:
2025-03-30 22:24:56 +03:00
parent 0e992c3be5
commit 1dc0040fb3
5 changed files with 39 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include "../globals.h" #include "../globals.h"
extern boardInfo brdInfo; extern boardInfo brdInfo;

View File

@@ -6,12 +6,13 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <stdbool.h> #include <stdbool.h>
#include "./utils/utils.h" #include "./utils/utils.h"
#include "./snake/snake.h" #include "./snake/snake.h"
#include "./board/board.h" #include "./board/board.h"
#include "globals.h" #include "globals.h"
const char VERSION[] = "1.1.5"; const char* VERSION = "1.1.6";
const char SNAKE_VIS = '#'; const char SNAKE_VIS = '#';
@@ -75,12 +76,18 @@ int main(int argc, char** argv) {
const int sleepInterval = 200; const int sleepInterval = 200;
// Snake head setup // Snake head setup
snakePart* snakeHead = (snakePart*) malloc(sizeof(snakePart)); snakePart* snakeHead = malloc(sizeof(snakePart));
if (!snakeHead) {
mallocError("snakeHead", "main.c", "main()");
}
snakeHead->x = randomX(initClock); snakeHead->x = randomX(initClock);
snakeHead->y = randomY(initClock); snakeHead->y = randomY(initClock);
snakeHead->visChar = '&'; snakeHead->visChar = '&';
snakeHead->dir = 'w'; snakeHead->dir = 'w';
snakeHead->order = (order*) malloc(sizeof(order)); snakeHead->order = malloc(sizeof(order));
if (!snakeHead->order) {
mallocError("snakeHead->order", "main.c", "main()");
}
snakeHead->order->dir = snakeHead->dir; snakeHead->order->dir = snakeHead->dir;
snakeHead->order->delay = -1; snakeHead->order->delay = -1;
snakeHead->order->next = NULL; snakeHead->order->next = NULL;

View File

@@ -1,8 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include "snake.h" #include "snake.h"
#include "../board/board.h" #include "../board/board.h"
#include "../utils/utils.h"
extern char SNAKE_VIS; extern char SNAKE_VIS;
extern boardInfo brdInfo; extern boardInfo brdInfo;
@@ -74,11 +76,18 @@ void addSnakePart(char board[][brdInfo.x], snakePart* head) {
while (tail->next != NULL) while (tail->next != NULL)
tail = tail->next; tail = tail->next;
snakePart* newTail = (snakePart*) malloc(sizeof(snakePart)); snakePart* newTail = malloc(sizeof(snakePart));
if (!newTail) {
mallocError("newTail", "snake.c", "addSnakePart()");
}
newTail->visChar = SNAKE_VIS; newTail->visChar = SNAKE_VIS;
newTail->dir = tail->dir; newTail->dir = tail->dir;
// Order head // Order head
newTail->order = (order*) malloc(sizeof(order)); newTail->order = malloc(sizeof(order));
if (!newTail->order) {
mallocError("newTail", "snake.c", "addSnakePart()");
}
newTail->order->dir = newTail->dir; newTail->order->dir = newTail->dir;
newTail->order->delay = -1; newTail->order->delay = -1;
// First order if exists // First order if exists
@@ -137,7 +146,10 @@ void pushOrder(order* head, char dir, int delay) {
while (current->next != NULL) while (current->next != NULL)
current = current->next; current = current->next;
order* newOrder = (order*) malloc(sizeof(order)); order* newOrder = malloc(sizeof(order));
if (!newOrder) {
mallocError("newOrder", "snake.c", "pushOrder()");
}
newOrder->dir = dir; newOrder->dir = dir;
newOrder->delay = delay; newOrder->delay = delay;
newOrder->next = NULL; newOrder->next = NULL;
@@ -179,7 +191,10 @@ void copyOrders(order* srcHead, order* destHead) {
while (srcCurrent->next != NULL) { while (srcCurrent->next != NULL) {
srcCurrent = srcCurrent->next; srcCurrent = srcCurrent->next;
destPrev = destCurrent; destPrev = destCurrent;
destCurrent = (order*) malloc(sizeof(order)); destCurrent = malloc(sizeof(order));
if (!destCurrent) {
mallocError("destCurrent", "snake.c", "copyOrders()");
}
destPrev->next = destCurrent; destPrev->next = destCurrent;
destCurrent->dir = srcCurrent->dir; destCurrent->dir = srcCurrent->dir;
destCurrent->delay = srcCurrent->delay + 1; destCurrent->delay = srcCurrent->delay + 1;

View File

@@ -1,6 +1,8 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "utils.h" #include "utils.h"
extern boardInfo brdInfo; extern boardInfo brdInfo;
@@ -37,3 +39,8 @@ int randomY(const clock_t initClock) {
void sleep_ms(const int ms) { void sleep_ms(const int ms) {
usleep(ms * 1000); usleep(ms * 1000);
} }
void mallocError(const char* varName, const char* fileName, const char* functionName) {
printf("Ran out of memory to allocate to %s in %s/%s\n", varName, fileName, functionName);
exit(1);
}

View File

@@ -2,6 +2,7 @@
#define UTILS_H_ #define UTILS_H_
#include <time.h> #include <time.h>
#include "../globals.h" #include "../globals.h"
extern boardInfo brdInfo; extern boardInfo brdInfo;
@@ -10,5 +11,6 @@ int randomInt(const int start, const int end, const unsigned int seed);
int randomX(const clock_t initClock); int randomX(const clock_t initClock);
int randomY(const clock_t initClock); int randomY(const clock_t initClock);
void sleep_ms(const int ms); void sleep_ms(const int ms);
void mallocError(const char* varName, const char* fileName, const char* functionName);
#endif // UTILS_H_ #endif // UTILS_H_