File 0002-calloc.patch of Package qemu

From 0d8b4ce740e8e2c894f8a7f163ad9dfd93463bde Mon Sep 17 00:00:00 2001
From: Daniel Axtens <dja@axtens.net>
Date: Fri, 10 Apr 2020 01:20:03 +1000
Subject: [PATCH 02/12] calloc

Signed-off-by: Daniel Axtens <dja@axtens.net>
---
 lib/libc/include/stdlib.h    |  1 +
 lib/libc/stdlib/Makefile.inc |  2 +-
 lib/libc/stdlib/calloc.c     | 30 ++++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)
 create mode 100644 lib/libc/stdlib/calloc.c

diff --git a/roms/SLOF/lib/libc/include/stdlib.h b/roms/SLOF/lib/libc/include/stdlib.h
index 5e0eda9..0d1f5ba 100644
--- a/roms/SLOF/lib/libc/include/stdlib.h
+++ b/roms/SLOF/lib/libc/include/stdlib.h
@@ -19,6 +19,7 @@
 
 
 void *malloc(size_t size);
+void *calloc(size_t nmemb, size_t size);
 void *realloc(void *ptr, size_t size);
 void free(void *ptr);
 void *memalign(size_t boundary, size_t size);
diff --git a/roms/SLOF/lib/libc/stdlib/Makefile.inc b/roms/SLOF/lib/libc/stdlib/Makefile.inc
index 702f6d7..61c5933 100644
--- a/roms/SLOF/lib/libc/stdlib/Makefile.inc
+++ b/roms/SLOF/lib/libc/stdlib/Makefile.inc
@@ -12,7 +12,7 @@
 
 
 STDLIB_SRC_C =  error.c atoi.c atol.c strtoul.c strtol.c rand.c \
-		 malloc.c memalign.c realloc.c free.c
+		 malloc.c calloc.c memalign.c realloc.c free.c
 		
 STDLIB_SRC_ASM = 
 STDLIB_SRCS = $(STDLIB_SRC_C:%=$(STDLIBCMNDIR)/%) $(STDLIB_SRC_ASM:%=$(STDLIBCMNDIR)/%)
diff --git a/roms/SLOF/lib/libc/stdlib/calloc.c b/roms/SLOF/lib/libc/stdlib/calloc.c
new file mode 100644
index 0000000..fabb3e0
--- /dev/null
+++ b/roms/SLOF/lib/libc/stdlib/calloc.c
@@ -0,0 +1,30 @@
+/******************************************************************************
+ * Copyright (c) 2020 IBM Corporation
+ * All rights reserved.
+ * This program and the accompanying materials
+ * are made available under the terms of the BSD License
+ * which accompanies this distribution, and is available at
+ * http://www.opensource.org/licenses/bsd-license.php
+ *
+ * Contributors:
+ *     IBM Corporation - initial implementation
+ *****************************************************************************/
+
+
+#include "stddef.h"
+#include "stdlib.h"
+#include "string.h"
+
+void *calloc(size_t nmemb, size_t size) {
+	size_t alloc_size;
+	void *ret;
+	if (__builtin_mul_overflow(nmemb, size, &alloc_size)) {
+		return NULL;
+	}
+
+	ret = malloc(alloc_size);
+
+	if (ret)
+		memset(ret, 0, alloc_size);
+	return ret;
+}
-- 
2.33.1

openSUSE Build Service is sponsored by