]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
Add os_calloc() wrapper
authorJouni Malinen <j@w1.fi>
Mon, 13 Aug 2012 17:42:24 +0000 (20:42 +0300)
committerJouni Malinen <j@w1.fi>
Mon, 13 Aug 2012 17:42:24 +0000 (20:42 +0300)
This function can be used as a wrapper for os_zalloc(nmemb * size) when
an allocation is used for an array. The main benefit over os_zalloc() is
in having an extra check to catch integer overflows in multiplication.

Signed-hostap: Jouni Malinen <j@w1.fi>

src/utils/os.h

index 3058b882ef3416e5438e6a8c341859b0625b1e92..468f66f459bad4e3b7ff620eaff9721179713936 100644 (file)
@@ -180,6 +180,25 @@ char * os_readfile(const char *name, size_t *len);
  */
 void * os_zalloc(size_t size);
 
+/**
+ * os_calloc - Allocate and zero memory for an array
+ * @nmemb: Number of members in the array
+ * @size: Number of bytes in each member
+ * Returns: Pointer to allocated and zeroed memory or %NULL on failure
+ *
+ * This function can be used as a wrapper for os_zalloc(nmemb * size) when an
+ * allocation is used for an array. The main benefit over os_zalloc() is in
+ * having an extra check to catch integer overflows in multiplication.
+ *
+ * Caller is responsible for freeing the returned buffer with os_free().
+ */
+static inline void * os_calloc(size_t nmemb, size_t size)
+{
+       if (size && nmemb > (~(size_t) 0) / size)
+               return NULL;
+       return os_zalloc(nmemb * size);
+}
+
 
 /*
  * The following functions are wrapper for standard ANSI C or POSIX functions.