git-svn-id: http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn@3068 e7ae566f-a301-0410-adde-c780ea21d3b5
james authored on 2008/07/18 05:10:18... | ... |
@@ -32,6 +32,16 @@ |
32 | 32 |
|
33 | 33 |
#include "memdbg.h" |
34 | 34 |
|
35 |
+size_t |
|
36 |
+array_mult_safe (const size_t m1, const size_t m2) |
|
37 |
+{ |
|
38 |
+ const unsigned long long limit = 0xFFFFFFFF; |
|
39 |
+ unsigned long long res = (unsigned long long)m1 * (unsigned long long)m2; |
|
40 |
+ if (unlikely(m1 > limit) || unlikely(m2 > limit) || unlikely(res > limit)) |
|
41 |
+ msg (M_FATAL, "attemped allocation of excessively large array"); |
|
42 |
+ return (size_t) res; |
|
43 |
+} |
|
44 |
+ |
|
35 | 45 |
struct buffer |
36 | 46 |
#ifdef DMALLOC |
37 | 47 |
alloc_buf_debug (size_t size, const char *file, int line) |
... | ... |
@@ -88,6 +88,8 @@ bool buf_assign (struct buffer *dest, const struct buffer *src); |
88 | 88 |
void string_clear (char *str); |
89 | 89 |
int string_array_len (const char **array); |
90 | 90 |
|
91 |
+size_t array_mult_safe (const size_t m1, const size_t m2); |
|
92 |
+ |
|
91 | 93 |
#define PA_BRACKET (1<<0) |
92 | 94 |
char *print_argv (const char **p, struct gc_arena *gc, const unsigned int flags); |
93 | 95 |
|
... | ... |
@@ -725,23 +727,23 @@ void out_of_memory (void); |
725 | 725 |
|
726 | 726 |
#define ALLOC_ARRAY(dptr, type, n) \ |
727 | 727 |
{ \ |
728 |
- check_malloc_return ((dptr) = (type *) malloc (sizeof (type) * (n))); \ |
|
728 |
+ check_malloc_return ((dptr) = (type *) malloc (array_mult_safe (sizeof (type), (n)))); \ |
|
729 | 729 |
} |
730 | 730 |
|
731 | 731 |
#define ALLOC_ARRAY_GC(dptr, type, n, gc) \ |
732 | 732 |
{ \ |
733 |
- (dptr) = (type *) gc_malloc (sizeof (type) * (n), false, (gc)); \ |
|
733 |
+ (dptr) = (type *) gc_malloc (array_mult_safe (sizeof (type), (n)), false, (gc)); \ |
|
734 | 734 |
} |
735 | 735 |
|
736 | 736 |
#define ALLOC_ARRAY_CLEAR(dptr, type, n) \ |
737 | 737 |
{ \ |
738 | 738 |
ALLOC_ARRAY (dptr, type, n); \ |
739 |
- memset ((dptr), 0, (sizeof(type) * (n))); \ |
|
739 |
+ memset ((dptr), 0, (array_mult_safe (sizeof(type), (n)))); \ |
|
740 | 740 |
} |
741 | 741 |
|
742 | 742 |
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \ |
743 | 743 |
{ \ |
744 |
- (dptr) = (type *) gc_malloc (sizeof (type) * (n), true, (gc)); \ |
|
744 |
+ (dptr) = (type *) gc_malloc (array_mult_safe (sizeof (type), (n)), true, (gc)); \ |
|
745 | 745 |
} |
746 | 746 |
|
747 | 747 |
#define ALLOC_OBJ_GC(dptr, type, gc) \ |