All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 1/1] sandbox: avoid memory leak in os_dirent_ls
@ 2017-09-21 10:56 Heinrich Schuchardt
  2017-09-25  2:13 ` Simon Glass
  2017-09-29 21:00 ` sjg at google.com
  0 siblings, 2 replies; 3+ messages in thread
From: Heinrich Schuchardt @ 2017-09-21 10:56 UTC (permalink / raw)
  To: u-boot

Realloc does not free the old memory area if it fails.

Identified by cppcheck.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 arch/sandbox/cpu/os.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 22d6aab534..c524957b6c 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -319,6 +319,7 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
 	DIR *dir;
 	int ret;
 	char *fname;
+	char *old_fname;
 	int len;
 	int dirlen;
 
@@ -344,16 +345,23 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
 			break;
 		}
 		next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
-		if (dirlen + strlen(entry->d_name) > len) {
-			len = dirlen + strlen(entry->d_name);
-			fname = realloc(fname, len);
-		}
-		if (!next || !fname) {
-			free(next);
+		if (!next) {
 			os_dirent_free(head);
 			ret = -ENOMEM;
 			goto done;
 		}
+		if (dirlen + strlen(entry->d_name) > len) {
+			len = dirlen + strlen(entry->d_name);
+			old_fname = fname;
+			fname = realloc(fname, len);
+			if (!fname) {
+				free(old_fname);
+				free(next);
+				os_dirent_free(head);
+				ret = -ENOMEM;
+				goto done;
+			}
+		}
 		next->next = NULL;
 		strcpy(next->name, entry->d_name);
 		switch (entry->d_type) {
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [U-Boot] [PATCH 1/1] sandbox: avoid memory leak in os_dirent_ls
  2017-09-21 10:56 [U-Boot] [PATCH 1/1] sandbox: avoid memory leak in os_dirent_ls Heinrich Schuchardt
@ 2017-09-25  2:13 ` Simon Glass
  2017-09-29 21:00 ` sjg at google.com
  1 sibling, 0 replies; 3+ messages in thread
From: Simon Glass @ 2017-09-25  2:13 UTC (permalink / raw)
  To: u-boot

On 21 September 2017 at 04:56, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
> Realloc does not free the old memory area if it fails.
>
> Identified by cppcheck.
>
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
>  arch/sandbox/cpu/os.c | 20 ++++++++++++++------
>  1 file changed, 14 insertions(+), 6 deletions(-)

Reviewed-by: Simon Glass <sjg@chromium.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [U-Boot] [PATCH 1/1] sandbox: avoid memory leak in os_dirent_ls
  2017-09-21 10:56 [U-Boot] [PATCH 1/1] sandbox: avoid memory leak in os_dirent_ls Heinrich Schuchardt
  2017-09-25  2:13 ` Simon Glass
@ 2017-09-29 21:00 ` sjg at google.com
  1 sibling, 0 replies; 3+ messages in thread
From: sjg at google.com @ 2017-09-29 21:00 UTC (permalink / raw)
  To: u-boot

On 21 September 2017 at 04:56, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
> Realloc does not free the old memory area if it fails.
>
> Identified by cppcheck.
>
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
>  arch/sandbox/cpu/os.c | 20 ++++++++++++++------
>  1 file changed, 14 insertions(+), 6 deletions(-)

Reviewed-by: Simon Glass <sjg@chromium.org>

Applied to u-boot-dm thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-09-29 21:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-21 10:56 [U-Boot] [PATCH 1/1] sandbox: avoid memory leak in os_dirent_ls Heinrich Schuchardt
2017-09-25  2:13 ` Simon Glass
2017-09-29 21:00 ` sjg at google.com

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.