ソースを参照

Fixed two memory leaks (#1120)

* Fixed two memory leaks

One pointer to allocated memory wasn't freed and realloc didn't have proper error handling. Additionally UB has been identified, because a pointer to a local variable is returned.

* Memory leak removed, cause be setting reference to NULL instead of deleting the object
Robert Gützkow 6 年 前
コミット
7677e66587
1 ファイル変更14 行追加2 行削除
  1. 14 2
      include/igl/ply.h

+ 14 - 2
include/igl/ply.h

@@ -539,9 +539,12 @@ inline PlyFile *ply_open_for_writing(
 
 
   fp = fopen (name, "w");
   fp = fopen (name, "w");
   if (fp == NULL) {
   if (fp == NULL) {
+    free(name);
     return (NULL);
     return (NULL);
   }
   }
 
 
+  free(name);
+
   /* create the actual PlyFile structure */
   /* create the actual PlyFile structure */
 
 
   plyfile = ply_write (fp, nelems, elem_names, file_type);
   plyfile = ply_write (fp, nelems, elem_names, file_type);
@@ -2253,6 +2256,7 @@ inline char **get_words(FILE *fp, int *nwords, char **orig_line)
   if (result == NULL) {
   if (result == NULL) {
     *nwords = 0;
     *nwords = 0;
     *orig_line = NULL;
     *orig_line = NULL;
+    free(words);
     return (NULL);
     return (NULL);
   }
   }
 
 
@@ -2316,7 +2320,15 @@ inline char **get_words(FILE *fp, int *nwords, char **orig_line)
     /* save pointer to beginning of word */
     /* save pointer to beginning of word */
     if (num_words >= max_words) {
     if (num_words >= max_words) {
       max_words += 10;
       max_words += 10;
-      words = (char **) realloc (words, sizeof (char *) * max_words);
+      char **temp = (char **) realloc (words, sizeof (char *) * max_words);
+
+      if(temp){
+          words = temp;
+      }
+      else{
+          free(words);
+          return NULL;
+      }
     }
     }
     words[num_words++] = ptr;
     words[num_words++] = ptr;
 
 
@@ -2330,7 +2342,7 @@ inline char **get_words(FILE *fp, int *nwords, char **orig_line)
 
 
   /* return the list of words */
   /* return the list of words */
   *nwords = num_words;
   *nwords = num_words;
-  *orig_line = str_copy;
+  *orig_line = str_copy; // ToDo: This looks like UB, returns pointer to local variable on stack.
   return (words);
   return (words);
 }
 }