1 /* 2 * Copyright 2008 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include <stdlib.h> 17 18 #if UNIT_TESTING 19 extern void* _test_malloc(const size_t size, const char* file, const int line); 20 extern void* _test_calloc(const size_t number_of_elements, const size_t size, 21 const char* file, const int line); 22 extern void _test_free(void* const ptr, const char* file, const int line); 23 24 #define malloc(size) _test_malloc(size, __FILE__, __LINE__) 25 #define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) 26 #define free(ptr) _test_free(ptr, __FILE__, __LINE__) 27 #endif // UNIT_TESTING 28 29 void leak_memory() { 30 int * const temporary = (int*)malloc(sizeof(int)); 31 *temporary = 0; 32 } 33 34 void buffer_overflow() { 35 char * const memory = (char*)malloc(sizeof(int)); 36 memory[sizeof(int)] = '!'; 37 free(memory); 38 } 39 40 void buffer_underflow() { 41 char * const memory = (char*)malloc(sizeof(int)); 42 memory[-1] = '!'; 43 free(memory); 44 } 45