Fix issues reported by cppcheck in scramble.c
authorMarius Gavrilescu <marius@ieval.ro>
Sat, 12 Dec 2015 19:31:39 +0000 (21:31 +0200)
committerJoey Hess <joeyh@joeyh.name>
Sun, 13 Dec 2015 18:51:57 +0000 (14:51 -0400)
scramble.c

index fc35ff8caefaa3c4d256732b1a0660e4e78ed1a1..a82e5932368c4bac559c856b08a1c2815a71dfad 100644 (file)
@@ -120,7 +120,7 @@ char *clear_string(char *string) {
 
 int main(int argc, char **argv) {
   int word_length;
 
 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
   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");
   
   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)) {
     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++;
       word[word_length] = c;
       word[word_length + 1] = '\0'; // duplicate addition with the next line, but possibly more readable
       word_length++;
This page took 0.012262 seconds and 4 git commands to generate.