diff mbox series

[U-Boot,v1] malloc_simple: calloc: don't call memset if malloc failed

Message ID 20180816075032.18346-1-simon.k.r.goldschmidt@gmail.com
State Accepted
Delegated to: Tom Rini
Headers show
Series [U-Boot,v1] malloc_simple: calloc: don't call memset if malloc failed | expand

Commit Message

Simon Goldschmidt Aug. 16, 2018, 7:50 a.m. UTC
malloc_simple() can return 0 if out of memory. Don't call memset
from calloc() in this case but rely on the caller checking
the return value.

Signed-off-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
Reviewed-by: Marek Vasut <marex@denx.de>

---

Changes in v1:
- extracted this patch from series "Get socfpga gen5 SPL working again"
  as it is not socfpga specific

 common/malloc_simple.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Tom Rini Aug. 24, 2018, 8:10 p.m. UTC | #1
On Thu, Aug 16, 2018 at 09:50:32AM +0200, Simon Goldschmidt wrote:

> malloc_simple() can return 0 if out of memory. Don't call memset
> from calloc() in this case but rely on the caller checking
> the return value.
> 
> Signed-off-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
> Reviewed-by: Marek Vasut <marex@denx.de>

Applied to u-boot/master, thanks!
diff mbox series

Patch

diff --git a/common/malloc_simple.c b/common/malloc_simple.c
index c14f8b59c1..871b5444bd 100644
--- a/common/malloc_simple.c
+++ b/common/malloc_simple.c
@@ -57,7 +57,8 @@  void *calloc(size_t nmemb, size_t elem_size)
 	void *ptr;
 
 	ptr = malloc(size);
-	memset(ptr, '\0', size);
+	if (ptr)
+		memset(ptr, '\0', size);
 
 	return ptr;
 }