From dd1b852e97cfac158883a3456a5dea9abbf31297 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 12 Dec 2015 21:31:39 +0200 Subject: [PATCH] Fix issues reported by cppcheck in scramble.c --- scramble.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/scramble.c b/scramble.c index fc35ff8..a82e593 100644 --- a/scramble.c +++ b/scramble.c @@ -120,7 +120,7 @@ char *clear_string(char *string) { int main(int argc, char **argv) { int word_length; - char c, tempchar, *word; + char c, tempchar, *word, *rword; FILE *infile, *outfile; #if ALLOW_FILE_IO @@ -172,12 +172,19 @@ int main(int argc, char **argv) { if(feof(infile)) { printf("Reached EOF while reading the first character of the input file!\n"); + free(word); return 4; } while(!feof(infile)) { if(isalpha(c)) { - word = realloc(word, word_length+2); // one for the new character, one for the null + rword = realloc(word, word_length+2); // one for the new character, one for the null + if(rword == NULL){ + free(word); + fprintf(stderr, "Unable to allocate memory\n"); + return 5; + } + word = rword; word[word_length] = c; word[word_length + 1] = '\0'; // duplicate addition with the next line, but possibly more readable word_length++; -- 2.30.2