cmake: Do not build the chef_wrap example on OSX.
[platform/upstream/cmocka.git] / example / allocate_module.c
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 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 #ifdef HAVE_MALLOC_H
20 #include <malloc.h>
21 #endif
22 #include <sys/types.h>
23 #include <stdlib.h>
24
25 #if UNIT_TESTING
26 extern void* _test_malloc(const size_t size, const char* file, const int line);
27 extern void* _test_calloc(const size_t number_of_elements, const size_t size,
28                           const char* file, const int line);
29 extern void _test_free(void* const ptr, const char* file, const int line);
30
31 #define malloc(size) _test_malloc(size, __FILE__, __LINE__)
32 #define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
33 #define free(ptr) _test_free(ptr, __FILE__, __LINE__)
34 #endif // UNIT_TESTING
35
36 void leak_memory(void);
37 void buffer_overflow(void);
38 void buffer_underflow(void);
39
40 void leak_memory(void) {
41     int * const temporary = (int*)malloc(sizeof(int));
42     *temporary = 0;
43 }
44
45 void buffer_overflow(void) {
46     char * const memory = (char*)malloc(sizeof(int));
47     memory[sizeof(int)] = '!';
48     free(memory);
49 }
50
51 void buffer_underflow(void) {
52     char * const memory = (char*)malloc(sizeof(int));
53     memory[-1] = '!';
54     free(memory);
55 }