Question: C Code secure coding * 1. Write a short routine to clear *ptr. * 2. Use freePointer() throughout the code * to remove all memory

C Code secure coding * 1. Write a short routine to clear *ptr. * 2. Use freePointer() throughout the code * to remove all memory leaks. #include "qotd.h" static qotd_t *qotd = NULL; static size_t count = 0; static char *buffer = NULL; void freePointer(void *ptr) { /* * * 1. Write a short routine to clear *ptr. * 2. Use freePointer() throughout the code * to remove all memory leaks. * */ } buffersize_t getBufferSize(ssize_t new_size) { /* * * Calculate a new buffer size, being * sure to prevent against integer overflow * and/or wrap. * */ return 0; } int addQuote(uint16_t pos, uint16_t len) { qotd = (qotd_t*)realloc(qotd, sizeof(qotd_t) * (count + 1)); if (qotd == NULL) { return ERROR_CONDITION; } qotd[count].pos = pos; qotd[count].len = len; count++; return SUCCESS; } void quoteOfTheDay() { char *str = NULL; srand((unsigned)time(NULL)); uint16_t index = ((rand() % count) + 1) - 1; str = malloc(qotd[index].len + 1); if (str == NULL) { fprintf(stderr, "quoteOfTheDay() %s", ERROR_MESSAGE); } else { strncpy(str, (char*)(buffer + qotd[index].pos), qotd[index].len); str[qotd[index].len] = '\0'; printf("%s ", str); } } int load(char *file) { FILE *stream; char *line = NULL; size_t length = 0; ssize_t lineSize; stream = fopen(file, "r"); if (stream == NULL) { return ERROR_CONDITION; } while ((lineSize = getline(&line, &length, stream)) != -1) { char* pos; buffersize_t bufferSize; if ((pos = strchr(line, ' ')) != NULL) { *pos = '\0'; } bufferSize = getBufferSize(lineSize); if (bufferSize == 0) { fclose(stream); fprintf(stderr, "getBufferSize() %s", ERROR_MESSAGE); return ERROR_CONDITION; } if ((buffer = realloc(buffer, bufferSize * sizeof(char))) == NULL) { fclose(stream); fprintf(stderr, "realloc() %s", ERROR_MESSAGE); return ERROR_CONDITION; } if (addQuote(strlen(buffer), lineSize - 1) == ERROR_CONDITION) { fclose(stream); fprintf(stderr, "addQuote() %s", ERROR_MESSAGE); return ERROR_CONDITION; } strncat(buffer, line, lineSize - 1); } fclose(stream); return strlen(buffer); } int main (int argc, char** argv) { if (load(QOTD_FILE) == ERROR_CONDITION) { return ERROR_CONDITION; } quoteOfTheDay(); freePointer(buffer); freePointer(qotd); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!