| OLD | NEW |
| (Empty) |
| 1 /* Copyright 2013 Google Inc. All Rights Reserved. | |
| 2 | |
| 3 Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 you may not use this file except in compliance with the License. | |
| 5 You may obtain a copy of the License at | |
| 6 | |
| 7 http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | |
| 9 Unless required by applicable law or agreed to in writing, software | |
| 10 distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 See the License for the specific language governing permissions and | |
| 13 limitations under the License. | |
| 14 | |
| 15 Size-checked memory allocation. | |
| 16 */ | |
| 17 | |
| 18 #include <stdlib.h> | |
| 19 #include "./safe_malloc.h" | |
| 20 | |
| 21 #if defined(__cplusplus) || defined(c_plusplus) | |
| 22 extern "C" { | |
| 23 #endif | |
| 24 | |
| 25 /* Returns 0 in case of overflow of nmemb * size. */ | |
| 26 static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) { | |
| 27 const uint64_t total_size = nmemb * size; | |
| 28 if (nmemb == 0) return 1; | |
| 29 if ((uint64_t)size > BROTLI_MAX_ALLOCABLE_MEMORY / nmemb) return 0; | |
| 30 if (total_size != (size_t)total_size) return 0; | |
| 31 return 1; | |
| 32 } | |
| 33 | |
| 34 void* BrotliSafeMalloc(uint64_t nmemb, size_t size) { | |
| 35 if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; | |
| 36 assert(nmemb * size > 0); | |
| 37 return malloc((size_t)(nmemb * size)); | |
| 38 } | |
| 39 | |
| 40 #if defined(__cplusplus) || defined(c_plusplus) | |
| 41 } /* extern "C" */ | |
| 42 #endif | |
| OLD | NEW |