realloc fix memory leak
PATCH 1/1 realloc fix memory leak
EP-F6AA0618C49C4AEDA73BFF1B39950BAB
Hi Mark,
Free previously allocated memory if realloc fails.
Signed-off-by Maninder Singh <[email protected]>
Signed-off-by Vaneet narang <[email protected]>
Reviewed-by Akhilesh Kumar <[email protected]>
--- 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
@@ -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
@@ -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
@@ -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
Mark Hatle
AppliedMerged to the staging branch.
Quoted 93 lines · Maninder Singh
EP-F6AA0618C49C4AEDA73BFF1B39950BAB
Hi Mark,
Free previously allocated memory if realloc fails.
Signed-off-by Maninder Singh <[email protected]>Signed-off-by Vaneet narang <[email protected]>Reviewed-by Akhilesh Kumar <[email protected]>--- 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@@ -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@@ -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@@ -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 ;