Skip to content
Snippets Groups Projects
Commit f8f27547 authored by dawehr's avatar dawehr
Browse files

Properly check return value of malloc() in log_data.c

parent 6dadef3e
No related branches found
No related tags found
No related merge requests found
...@@ -80,13 +80,17 @@ void initialize_logging(log_t* log_struct, parameter_t* ps) { ...@@ -80,13 +80,17 @@ void initialize_logging(log_t* log_struct, parameter_t* ps) {
// TODO: Make this not stupid. Adding 6 for IMU and 1 for timestamp // TODO: Make this not stupid. Adding 6 for IMU and 1 for timestamp
row_size = n_outputs + n_params + 6 + 1; row_size = n_outputs + n_params + 6 + 1;
logArray = malloc(sizeof(float) * row_size * LOG_STARTING_SIZE); size_t needed_memory = sizeof(float) * row_size * LOG_STARTING_SIZE;
logArray = malloc(needed_memory);
if (!logArray) { // malloc failed
arraySize = 0;
}
} }
int log_data(log_t* log_struct, parameter_t* ps) int log_data(log_t* log_struct, parameter_t* ps)
{ {
if(arrayIndex >= arraySize - 1) if(arrayIndex >= arraySize)
{ {
return 1; return 1;
} }
...@@ -126,6 +130,13 @@ void printLogging(log_t* log_struct, parameter_t* ps){ ...@@ -126,6 +130,13 @@ void printLogging(log_t* log_struct, parameter_t* ps){
char header1[256] = {}; char header1[256] = {};
char header2[1024] = {}; char header2[1024] = {};
// Let user know that allocation failed
if (arraySize != LOG_STARTING_SIZE) {
size_t send_len = sprintf(header1, "Failed to allocate enough log memory\n");
send_data(LOG_ID, 0, header1, send_len);
return;
}
sprintf(header1, "time,accel_x,accel_y,accel_z,gyro_x,gyro_y,gyro_z"); sprintf(header1, "time,accel_x,accel_y,accel_z,gyro_x,gyro_y,gyro_z");
int end = 0; int end = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment