OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 3 // Use of this source code is governed by a BSD-style license |
4 // that can be found in the COPYING file in the root of the source | 4 // that can be found in the COPYING file in the root of the source |
5 // tree. An additional intellectual property rights grant can be found | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Misc. common utility functions | 10 // Misc. common utility functions |
11 // | 11 // |
12 // Author: Skal (pascal.massimino@gmail.com) | 12 // Author: Skal (pascal.massimino@gmail.com) |
13 | 13 |
14 #include <stdlib.h> | 14 #include <stdlib.h> |
15 #include "./utils.h" | 15 #include "./utils.h" |
16 | 16 |
17 #if defined(__cplusplus) || defined(c_plusplus) | |
18 extern "C" { | |
19 #endif | |
20 | |
21 //------------------------------------------------------------------------------ | 17 //------------------------------------------------------------------------------ |
22 // Checked memory allocation | 18 // Checked memory allocation |
23 | 19 |
24 // Returns 0 in case of overflow of nmemb * size. | 20 // Returns 0 in case of overflow of nmemb * size. |
25 static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) { | 21 static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) { |
26 const uint64_t total_size = nmemb * size; | 22 const uint64_t total_size = nmemb * size; |
27 if (nmemb == 0) return 1; | 23 if (nmemb == 0) return 1; |
28 if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0; | 24 if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0; |
29 if (total_size != (size_t)total_size) return 0; | 25 if (total_size != (size_t)total_size) return 0; |
30 return 1; | 26 return 1; |
31 } | 27 } |
32 | 28 |
33 void* WebPSafeMalloc(uint64_t nmemb, size_t size) { | 29 void* WebPSafeMalloc(uint64_t nmemb, size_t size) { |
34 if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; | 30 if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; |
35 assert(nmemb * size > 0); | 31 assert(nmemb * size > 0); |
36 return malloc((size_t)(nmemb * size)); | 32 return malloc((size_t)(nmemb * size)); |
37 } | 33 } |
38 | 34 |
39 void* WebPSafeCalloc(uint64_t nmemb, size_t size) { | 35 void* WebPSafeCalloc(uint64_t nmemb, size_t size) { |
40 if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; | 36 if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; |
41 assert(nmemb * size > 0); | 37 assert(nmemb * size > 0); |
42 return calloc((size_t)nmemb, size); | 38 return calloc((size_t)nmemb, size); |
43 } | 39 } |
44 | 40 |
45 //------------------------------------------------------------------------------ | 41 //------------------------------------------------------------------------------ |
46 | 42 |
47 #if defined(__cplusplus) || defined(c_plusplus) | |
48 } // extern "C" | |
49 #endif | |
OLD | NEW |