summaryrefslogtreecommitdiff
path: root/src/cairo-malloc-private.h
diff options
context:
space:
mode:
authorVladimir Vukicevic <vladimir@pobox.com>2007-09-21 11:02:42 -0700
committerVladimir Vukicevic <vladimir@pobox.com>2007-09-21 11:02:42 -0700
commit6020f67f1a49cfe3844c4938d4af24c63c8424cc (patch)
tree4c637afefdaa6a3881d97e20bc5e20d1b36a2d67 /src/cairo-malloc-private.h
parentc79fc9af334fd6f2d1078071d64178125561b187 (diff)
downloadcairo-6020f67f1a49cfe3844c4938d4af24c63c8424cc.tar.gz
Avoid divide-by-zero when trying to allocate a 0-sized array
Fix up the _cairo_malloc_* wrappers to avoid blindly dividing by zero; any attempt to allocate a zero-sized chunk of memory will result in NULL.
Diffstat (limited to 'src/cairo-malloc-private.h')
-rw-r--r--src/cairo-malloc-private.h35
1 files changed, 25 insertions, 10 deletions
diff --git a/src/cairo-malloc-private.h b/src/cairo-malloc-private.h
index f503b3951..ad22851b1 100644
--- a/src/cairo-malloc-private.h
+++ b/src/cairo-malloc-private.h
@@ -40,11 +40,26 @@
#include "cairo-wideint-private.h"
/**
+ * _cairo_malloc:
+ * @size: size in bytes
+ *
+ * Allocate @size memory using malloc().
+ * The memory should be freed using free().
+ * malloc is skipped, if 0 bytes are requested, and %NULL will be returned.
+ *
+ * Return value: A pointer to the newly allocated memory, or %NULL in
+ * case of malloc() failure or size is 0.
+ */
+
+#define _cairo_malloc(size) \
+ ((size) ? malloc((unsigned) (size)) : NULL)
+
+/**
* _cairo_malloc_ab:
* @n: number of elements to allocate
* @size: size of each element
*
- * Allocates @a*@size memory using malloc(), taking care to not
+ * Allocates @a*@size memory using _cairo_malloc(), taking care to not
* overflow when doing the multiplication. Behaves much like
* calloc(), except that the returned memory is not set to zero.
* The memory should be freed using free().
@@ -57,8 +72,8 @@
*/
#define _cairo_malloc_ab(a, size) \
- ((unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
- malloc((unsigned) (a) * (unsigned) (size)))
+ ((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
+ _cairo_malloc((unsigned) (a) * (unsigned) (size)))
/**
* _cairo_malloc_abc:
@@ -66,7 +81,7 @@
* @b: second factor of number of elements to allocate
* @size: size of each element
*
- * Allocates @a*@b*@size memory using malloc(), taking care to not
+ * Allocates @a*@b*@size memory using _cairo_malloc(), taking care to not
* overflow when doing the multiplication. Behaves like
* _cairo_malloc_ab(). The memory should be freed using free().
*
@@ -78,9 +93,9 @@
*/
#define _cairo_malloc_abc(a, b, size) \
- ((unsigned) (a) >= INT32_MAX / (unsigned) (b) ? NULL : \
- (unsigned) ((a)*(b)) >= INT32_MAX / (unsigned) (size) ? NULL : \
- malloc((unsigned) (a) * (unsigned) (b) * (unsigned) size))
+ ((b) && (unsigned) (a) >= INT32_MAX / (unsigned) (b) ? NULL : \
+ (size) && (unsigned) ((a)*(b)) >= INT32_MAX / (unsigned) (size) ? NULL : \
+ _cairo_malloc((unsigned) (a) * (unsigned) (b) * (unsigned) (size)))
/**
* _cairo_malloc_ab_plus_c:
@@ -88,7 +103,7 @@
* @size: size of each element
* @k: additional size to allocate
*
- * Allocates @a*@ksize+@k memory using malloc(), taking care to not
+ * Allocates @a*@ksize+@k memory using _cairo_malloc(), taking care to not
* overflow when doing the arithmetic. Behaves like
* _cairo_malloc_ab(). The memory should be freed using free().
*
@@ -97,8 +112,8 @@
*/
#define _cairo_malloc_ab_plus_c(n, size, k) \
- ((unsigned) (n) >= INT32_MAX / (unsigned) (size) ? NULL : \
+ ((size) && (unsigned) (n) >= INT32_MAX / (unsigned) (size) ? NULL : \
(unsigned) (k) >= INT32_MAX - (unsigned) (n) * (unsigned) (size) ? NULL : \
- malloc((unsigned) (n) * (unsigned) (size) + (unsigned) (k)))
+ _cairo_malloc((unsigned) (n) * (unsigned) (size) + (unsigned) (k)))
#endif /* CAIRO_MALLOC_PRIVATE_H */