public
/
ack.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
36ab903
)
Implement calloc() (accidentally got dropped with the malloc rewrite).
author
David Given
<dg@cowlark.com>
Wed, 23 Nov 2016 21:16:25 +0000
(22:16 +0100)
committer
David Given
<dg@cowlark.com>
Wed, 23 Nov 2016 21:16:25 +0000
(22:16 +0100)
lang/cem/libcc.ansi/malloc/calloc.c
[new file with mode: 0644]
patch
|
blob
diff --git a/lang/cem/libcc.ansi/malloc/calloc.c
b/lang/cem/libcc.ansi/malloc/calloc.c
new file mode 100644
(file)
index 0000000..
2e9d3be
--- /dev/null
+++ b/
lang/cem/libcc.ansi/malloc/calloc.c
@@ -0,0
+1,23
@@
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+
+void* calloc(size_t nmemb, size_t size)
+{
+ size_t bytes = nmemb * size;
+ void* ptr;
+
+ /* Test for overflow.
+ * See http://stackoverflow.com/questions/1815367/multiplication-of-large-numbers-how-to-catch-overflow
+ */
+
+ if ((nmemb == 0) || (size == 0) || (nmemb > (SIZE_MAX / size)))
+ return NULL;
+
+ ptr = malloc(bytes);
+ if (!ptr)
+ return NULL;
+
+ memset(ptr, 0, bytes);
+ return ptr;
+}