Skip to contentSkip to conversation

realloc fix memory leak

PATCH Applied Maninder Singh 2 participants 2 messages May 6, 2015 – Sep 11, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 realloc fix memory leak

EP-F6AA0618C49C4AEDA73BFF1B39950BAB

Hi Mark,

Free previously allocated memory if realloc fails.

---
 trunk/src/doit.c    |    4 +++-
 trunk/src/gather.c  |    4 +++-
 trunk/src/undoall.c |    4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)
trunk/src/doit.c +3 -1
index c94fc53..0995fce 100644
@@ -53,7 +53,7 @@ prelink_ent (struct prelink_entry *ent)
   DSO *dso;
   struct stat64 st;
   struct prelink_link *hardlink;
-  char *move = NULL;
+  char *move = NULL, *move_temp;
   size_t movelen = 0;
 
   for (i = 0; i < ent->ndepends; ++i)
@@ -176,9 +176,11 @@ prelink_ent (struct prelink_entry *ent)
       if (len + sizeof (".#prelink#") > movelen)
 	{
 	  movelen = len + sizeof (".#prelink#");
+	  move_temp = move;
 	  move = realloc (move, movelen);
 	  if (move == NULL)
 	    {
+		  free(move_temp);
 	      error (0, ENOMEM, "Could not hardlink %s to %s",
 		     hardlink->canon_filename, ent->canon_filename);
 	      movelen = 0;
trunk/src/gather.c +3 -1
index c3d3128..733b49f 100644
@@ -64,7 +64,7 @@ gather_deps (DSO *dso, struct prelink_entry *ent)
   const char *argv[6];
   const char *envp[5];
   char *line = NULL, *p, *q = NULL;
-  const char **depends = NULL;
+  const char **depends = NULL, **depends_temp;
   size_t ndepends = 0, ndepends_alloced = 0;
   size_t len = 0;
   ssize_t n;
@@ -261,11 +261,13 @@ gather_deps (DSO *dso, struct prelink_entry *ent)
       if (ndepends == ndepends_alloced)
 	{
 	  ndepends_alloced += 10;
+	  depends_temp = depends;
 	  depends =
 	    (const char **) realloc (depends,
 				     ndepends_alloced * sizeof (char *));
 	  if (depends == NULL)
 	    {
+		  free(depends_temp);
 	      error (0, ENOMEM, "%s: Could not record dependencies",
 		     ent->filename);
 	      goto error_out;
trunk/src/undoall.c +3 -1
index 90e9240..6163da3 100644
@@ -33,7 +33,7 @@ undo_one (void **p, void *info)
   DSO *dso;
   struct stat64 st;
   struct prelink_link *hardlink;
-  char *move = NULL;
+  char *move = NULL, *move_temp;
   size_t movelen = 0;
 
   if (ent->done != 2)
@@ -116,9 +116,11 @@ undo_one (void **p, void *info)
       if (len + sizeof (".#prelink#") > movelen)
 	{
 	  movelen = len + sizeof (".#prelink#");
+	  move_temp = move;
 	  move = realloc (move, movelen);
 	  if (move == NULL)
 	    {
+		  free(move_temp);
 	      error (0, ENOMEM, "Could not hardlink %s to %s",
 		     hardlink->canon_filename, ent->canon_filename);
 	      movelen = 0;
Signature · 5 lines
1.7.1


Thanks and Regards,
Maninder Singh
Applied
#1 lore raw

Merged to the staging branch.

Quoted 93 lines · Maninder Singh

EP-F6AA0618C49C4AEDA73BFF1B39950BAB

Hi Mark,

Free previously allocated memory if realloc fails.

---
 trunk/src/doit.c    |    4 +++-
 trunk/src/gather.c  |    4 +++-
 trunk/src/undoall.c |    4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)
trunk/src/doit.c +3 -1
index c94fc53..0995fce 100644
@@ -53,7 +53,7 @@ prelink_ent (struct prelink_entry *ent)
   DSO *dso;
   struct stat64 st;
   struct prelink_link *hardlink;
-  char *move = NULL;
+  char *move = NULL, *move_temp;
   size_t movelen = 0;
 
   for (i = 0; i < ent->ndepends; ++i)
@@ -176,9 +176,11 @@ prelink_ent (struct prelink_entry *ent)
       if (len + sizeof (".#prelink#") > movelen)
 	{
 	  movelen = len + sizeof (".#prelink#");
+	  move_temp = move;
 	  move = realloc (move, movelen);
 	  if (move == NULL)
 	    {
+		  free(move_temp);
 	      error (0, ENOMEM, "Could not hardlink %s to %s",
 		     hardlink->canon_filename, ent->canon_filename);
 	      movelen = 0;
trunk/src/gather.c +3 -1
index c3d3128..733b49f 100644
@@ -64,7 +64,7 @@ gather_deps (DSO *dso, struct prelink_entry *ent)
   const char *argv[6];
   const char *envp[5];
   char *line = NULL, *p, *q = NULL;
-  const char **depends = NULL;
+  const char **depends = NULL, **depends_temp;
   size_t ndepends = 0, ndepends_alloced = 0;
   size_t len = 0;
   ssize_t n;
@@ -261,11 +261,13 @@ gather_deps (DSO *dso, struct prelink_entry *ent)
       if (ndepends == ndepends_alloced)
 	{
 	  ndepends_alloced += 10;
+	  depends_temp = depends;
 	  depends =
 	    (const char **) realloc (depends,
 				     ndepends_alloced * sizeof (char *));
 	  if (depends == NULL)
 	    {
+		  free(depends_temp);
 	      error (0, ENOMEM, "%s: Could not record dependencies",
 		     ent->filename);
 	      goto error_out;
trunk/src/undoall.c +3 -1
index 90e9240..6163da3 100644
@@ -33,7 +33,7 @@ undo_one (void **p, void *info)
   DSO *dso;
   struct stat64 st;
   struct prelink_link *hardlink;
-  char *move = NULL;
+  char *move = NULL, *move_temp;
   size_t movelen = 0;
 
   if (ent->done != 2)
@@ -116,9 +116,11 @@ undo_one (void **p, void *info)
       if (len + sizeof (".#prelink#") > movelen)
 	{
 	  movelen = len + sizeof (".#prelink#");
+	  move_temp = move;
 	  move = realloc (move, movelen);
 	  if (move == NULL)
 	    {
+		  free(move_temp);
 	      error (0, ENOMEM, "Could not hardlink %s to %s",
 		     hardlink->canon_filename, ent->canon_filename);
 	      movelen = 0;