OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // This code is licensed under the same terms as WebM: | 3 // This code is licensed under the same terms as WebM: |
4 // Software License Agreement: http://www.webmproject.org/license/software/ | 4 // Software License Agreement: http://www.webmproject.org/license/software/ |
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ | 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
6 // ----------------------------------------------------------------------------- | 6 // ----------------------------------------------------------------------------- |
7 // | 7 // |
8 // Misc. common utility functions | 8 // Misc. common utility functions |
9 // | 9 // |
10 // Author: Skal (pascal.massimino@gmail.com) | 10 // Author: Skal (pascal.massimino@gmail.com) |
11 | 11 |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 #include "./utils.h" | 13 #include "./utils.h" |
14 | 14 |
15 #if defined(__cplusplus) || defined(c_plusplus) | 15 #if defined(__cplusplus) || defined(c_plusplus) |
16 extern "C" { | 16 extern "C" { |
17 #endif | 17 #endif |
18 | 18 |
19 //------------------------------------------------------------------------------ | 19 //------------------------------------------------------------------------------ |
20 // Checked memory allocation | 20 // Checked memory allocation |
21 | 21 |
22 static int CheckSizeArguments(uint64_t nmemb, size_t size) { | 22 // Returns 0 in case of overflow of nmemb * size. |
| 23 static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) { |
23 const uint64_t total_size = nmemb * size; | 24 const uint64_t total_size = nmemb * size; |
24 if (nmemb == 0) return 1; | 25 if (nmemb == 0) return 1; |
25 if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0; | 26 if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0; |
26 if (total_size != (size_t)total_size) return 0; | 27 if (total_size != (size_t)total_size) return 0; |
27 return 1; | 28 return 1; |
28 } | 29 } |
29 | 30 |
30 void* WebPSafeMalloc(uint64_t nmemb, size_t size) { | 31 void* WebPSafeMalloc(uint64_t nmemb, size_t size) { |
31 if (!CheckSizeArguments(nmemb, size)) return NULL; | 32 if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; |
| 33 assert(nmemb * size > 0); |
32 return malloc((size_t)(nmemb * size)); | 34 return malloc((size_t)(nmemb * size)); |
33 } | 35 } |
34 | 36 |
35 void* WebPSafeCalloc(uint64_t nmemb, size_t size) { | 37 void* WebPSafeCalloc(uint64_t nmemb, size_t size) { |
36 if (!CheckSizeArguments(nmemb, size)) return NULL; | 38 if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; |
| 39 assert(nmemb * size > 0); |
37 return calloc((size_t)nmemb, size); | 40 return calloc((size_t)nmemb, size); |
38 } | 41 } |
39 | 42 |
40 //------------------------------------------------------------------------------ | 43 //------------------------------------------------------------------------------ |
41 | 44 |
42 #if defined(__cplusplus) || defined(c_plusplus) | 45 #if defined(__cplusplus) || defined(c_plusplus) |
43 } // extern "C" | 46 } // extern "C" |
44 #endif | 47 #endif |
OLD | NEW |